Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative flexible request API #98

Open
WebFreak001 opened this issue Mar 28, 2019 · 3 comments
Open

Alternative flexible request API #98

WebFreak001 opened this issue Mar 28, 2019 · 3 comments

Comments

@WebFreak001
Copy link
Contributor

I would like to suggest a generic high-level API that could be used for requests instead of getContent/postContent:

Response request(Args...)(HTTPMethod method, URL url, Args args);

basically it should allow any kind of request manipulating parameter and incoperate it into the request.

So for example to set headers, query string, form data and cookies in a high level request:

MultipartForm form;
form.add(formData("file", File("test.bin", "rb"), ["filename":"test.txt", "Content-Type": "application/octet-stream"]));
request(HTTPMethod.POST, URL("https://example.org/upload"),
    httpHeader("Authorization", "Basic 123"),
    queryParams("name", "any name", "age", 42),
    form,
    Cookies([Cookie("/", null, "sessionId", "abc")])
);

I used URL here to make it more typesafe so you don't accidentally pass other data as URL. (it exists in vibe and could be added as light wrapper here too)

Basically it should accept any arguments which could manipulate the request and apply them all on the request. If basic wrappers around simple values are used this is extensible, readable, typesafe and emulates named arguments with it. It would be cool if Json body would also be supported using JSONValue or vibe.d json here.

It should also handle redirects transparently. (not sure how well this would integrate with still allowing the user to read the response using ranges)

The response object should probably get an additional utility function converting the response to a string (instead of bytes), but all the other stuff should exist and be accessible. It could also contain helper functions to parse the content as Json or JSONValue.

It would be a fairly high-level function that still has optional access to a lot of the low level stuff, which I think would be very useful.

@ikod
Copy link
Owner

ikod commented Mar 29, 2019 via email

@ikod
Copy link
Owner

ikod commented Apr 2, 2019

Hello @WebFreak001

It would be nice to be able to write high level API calls in pythonic way:

MultipartForm form;
form.add(formData("file", File("test.bin", "rb"), ["filename":"test.txt", "Content-Type": "application/octet-stream"]));

auto content = request(method=HTTPMethod.POST, url=URL("https://example.org/upload"),
    headers=["Authorization": "Basic 123"],
    params = queryParams("name", "any name", "age", 42),
    form = form,
    cookies = Cookies([Cookie("/", null, "sessionId", "abc")])
);

where each parameter can take any position and is optional. I'll play and see what is the best way to implement this.

@zkxjzmswkwl
Copy link

Any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants