dlang-requests
dlang-requests copied to clipboard
Alternative flexible request API
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.
Hello Jan!
Thanks for your idea! I'll see how this can be done. I'm thinking about arguments type safety and it would be definitely nice to implement. It is easy and I will do it.
Related to the other proposals I have to look closely what can I do.
Thanks!
чт, 28 мар. 2019 г., 14:58 Jan Jurzitza [email protected]:
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.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ikod/dlang-requests/issues/98, or mute the thread https://github.com/notifications/unsubscribe-auth/ABNuScWMz75Flz6fcqHTZT-xCGT274wRks5vbLyIgaJpZM4cQGKk .
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.
Any update on this?