dlang-requests icon indicating copy to clipboard operation
dlang-requests copied to clipboard

working smoothly with serialization

Open wilzbach opened this issue 8 years ago • 4 comments

@JackStouffer correctly pointed out that Python's requests library should be a inspiration point (e.g. read the user testimonials for the reason).

One important feature is automatic serialization which is quite handy, so maybe we can keep this in mind that plugin a serializer / deserializer is an important use case.

  • https://github.com/s-ludwig/std_data_json (json serialization from vibed, proposed to be the new standard lib)
  • https://github.com/jacob-carlborg/orange (multiple output formats)
  • https://github.com/tamediadigital/asdf (optimized for speed)

wilzbach avatar May 25 '16 19:05 wilzbach

@JackStouffer what other key features of request would you highlight?

wilzbach avatar May 25 '16 19:05 wilzbach

The key thing about requests (and it's also the thing I love about std.range and std.algorithm) is that the API is so simple that you can keep the day to day stuff in your head.

For example, I know without looking it up that the call to download the text of a page is

requests.get("url").text()

No protocol is simple, especially HTTP. But, there should be a simple high level with the ability to drop down into low level stuff, which is one of the key selling points of D.

JackStouffer avatar May 25 '16 20:05 JackStouffer

@JackStouffer exactly, the simple, high level API is my goal - everyday things should be done in single simple API call.

This code is simple and do what you expect:

import requests;
import std.stdio;

void main() {
    writeln(getContent("http://dlang.org")); // write response body
    writeln(Request().get("http://dlang.org").responseBody); // same, in more verbose way
    writeln(Request().get("http://dlang.org").code); // write response code
}

ikod avatar May 26 '16 02:05 ikod

its already fairly simple: https://github.com/yannick/requests-asdf-example/blob/master/source/app.d

string endpoint = "https://api.github.com/search/repositories?order=desc&sort=updated&q=language:D";

Item[] items = getContent(endpoint).to!string.parseJson["items"].deserialize!(Item[]);

it could be made even faster and nicer by allowing direct access to the buffer, see https://github.com/ikod/dlang-requests/issues/19

yannick avatar Jun 08 '16 09:06 yannick