Skip to content

Commit

Permalink
added headers, parameters, complete post/get requests
Browse files Browse the repository at this point in the history
  • Loading branch information
eresid committed Dec 29, 2016
1 parent 05ffebf commit 768d674
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 34 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@ Simple REST client on D language
* ~~HTTP Client~~
* ~~HTTP Client options~~
* ~~Response handler~~
* Headers
* Parameters
* POST request
* GET request
* ~~Headers~~
* ~~Parameters~~
* ~~POST request~~
* ~~GET request~~

[0.2]
* Timeout
* Json serialization/deserialization
* VK sample
* httpbin.org sample

[0.3]
* Dynamic parameters in URL
* PUT request
* PATCH request
* DELETE request
* Content Type

[0.3]
[0.4]
* Multipart/form-data
* Progress
* PATCH request
* DELETE request
* flatbuffers support
* Timeout

[0.4]
## Backlog
* Async requests

[0.5]
* Progress
* Cache
* Documentation
* wiki
* javadoc

[0.6]
* Unit Tests
* RX support
* Json serialization/deserialization

[0.7]
* OAuth 2
* Support dlang-requests
* https://github.com/ikod/dlang-requests
Expand Down
Binary file removed marsrestclient
Binary file not shown.
15 changes: 4 additions & 11 deletions source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ void main()

HttpClientOptions options = new HttpClientOptions;
options.baseUrl(BASE_URL);

options.headers = ["DefaultHeader" : "header value"];
HttpClient client = new AsyncHttpClient(options);
client.get("get", params, new CredentialsHttpResponseHandler);

client.post("post?name=Eugene", params, ["CustomHeader1" : "value1", "CustomHeader2" : "value2"], new CredentialsHttpResponseHandler);
client.get("get?key1=value1&key2=value2", params, ["CustomHeader3" : "value3", "CustomHeader4" : "value4"], new CredentialsHttpResponseHandler);
}

private class CredentialsHttpResponseHandler : HttpResponseHandler {
Expand All @@ -44,12 +46,3 @@ private class CredentialsHttpResponseHandler : HttpResponseHandler {
writeln(cast(string)responseBody);
}
}

void getHeader() {
// GET with custom data receivers
auto http = HTTP("dlang.org");
http.onReceiveHeader =
(in char[] key, in char[] value) { writeln(key, ": ", value); };
http.onReceive = (ubyte[] data) { /+ drop +/ return data.length; };
http.perform();
}
57 changes: 52 additions & 5 deletions source/mars/AsyncHttpClient.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class AsyncHttpClient : HttpClient {
this(HttpClientOptions options) {
mOptions = options;
mHttp = HTTP();

loadDefaultHeaders();
}

void post(string url, HttpResponseHandler responseHandler) {
Expand Down Expand Up @@ -52,7 +54,35 @@ class AsyncHttpClient : HttpClient {
ubyte[] responseBody;

mHttp.method = httpMethod;
mHttp.url = getCorrectUrl(url);
if (httpMethod == HTTP.Method.post) {
mHttp.url = getCorrectUrl(url);
mHttp.setPostData(params.toJson, "application/json");
} else if (httpMethod == HTTP.Method.get) {
string getUrl = getCorrectUrl(url);
string[string] paramsToSend = params.getParams;

if (paramsToSend !is null && paramsToSend.length > 0) {
import std.string;
import std.algorithm.searching;
import std.algorithm.iteration;

string urlParams = paramsToSend.keys.map!(k => k ~ "=" ~ paramsToSend[k]).join("&");
if (canFind(getUrl, "?")) {
mHttp.url = getUrl ~ "&" ~ urlParams;
} else {
string finalUrl = getUrl ~ "?" ~ urlParams;
mHttp.url = finalUrl;
}
} else {
mHttp.url = getCorrectUrl(url);
}
}
if (headers != null && headers.length > 0) {
foreach (name, value; headers) {
mHttp.addRequestHeader(name, value);
}
}

mHttp.onReceiveStatusLine = (HTTP.StatusLine status) {
statusCode = status.code;
};
Expand All @@ -69,24 +99,41 @@ class AsyncHttpClient : HttpClient {
};
mHttp.perform();

loadDefaultHeaders();

if (StatusCode.isSuccess(statusCode)) {
responseHandler.onSuccess(statusCode, responseHeaders, responseBody);
} else {
responseHandler.onFailure(statusCode, responseHeaders, responseBody, new ServerException(statusCode));
}
}

/**
* Remove all custom headers except the default headers
*/
private void loadDefaultHeaders() {
mHttp.clearRequestHeaders;

string[string] headers = mOptions.headers;

if (headers != null && headers.length > 0) {
foreach (name, value; headers) {
mHttp.addRequestHeader(name, value);
}
}
}

private string getCorrectUrl(string url) {
import std.exception;

enforce(mOptions.baseUrl !is null, new Exception("Cannot get URL, please, set base URL!"));

import std.algorithm.searching;

if (url.startsWith("http")) {
return url;
}

if (mOptions.baseUrl is null) {
throw new Exception("Cannot get URL, please, set base URL!");
}

return mOptions.baseUrl ~ url;
}
}
12 changes: 11 additions & 1 deletion source/mars/RequestParams.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ import std.conv;

class RequestParams {

string[string] params;
private string[string] params;

string[string] getParams() {
return params;
}

string toJson() {
import std.json;

return JSONValue(params).toString;
}

/**
* string value
Expand Down

0 comments on commit 768d674

Please sign in to comment.