Skip to content

Commit a24d561

Browse files
committed
add patch and delete rest methods
1 parent c364ec7 commit a24d561

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ Simple REST client on D language
1212
* ~~GET request~~
1313

1414
[0.2]
15-
* ~~add builder for HTTP Client~~
15+
* ~~Add builder for HTTP Client~~
1616
* ~~Content Type~~
1717
* ~~User Agent~~
18+
* ~~DELETE request~~
19+
* ~~PATCH request~~
20+
* Set args and data in builder
1821
* PUT request
19-
* PATCH request
20-
* DELETE request
2122

2223
[0.3]
2324
* VK sample
@@ -28,6 +29,7 @@ Simple REST client on D language
2829
* Multipart/form-data
2930
* flatbuffers support
3031
* Timeout
32+
* Optional logs (terminal or file)
3133

3234
## Backlog
3335
* Async requests

source/app.d

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import mars.Request;
99
import mars.RequestParams;
1010
import mars.ServerException;
1111

12-
string BASE_URL = "http://httpbin.org/";
12+
string BASE_URL = "https://httpbin.org/";
1313

1414
void main()
1515
{
@@ -20,21 +20,43 @@ void main()
2020
HttpClientOptions options = new HttpClientOptions;
2121
options.baseUrl(BASE_URL);
2222
options.headers = ["DefaultHeader" : "header value", "User-Agent" : "Custom User Agent", "Accept" : "Custom Accept"];
23-
HttpClient client = new AsyncHttpClient(options);
23+
HttpClient client = AsyncHttpClient.init(options);
2424

2525
Request postRequest = new Request.Builder()
2626
.url("post?name=Eugene")
2727
.params(params)
28-
.headers(["CustomHeader1" : "value1", "CustomHeader2" : "value2"])
28+
.headers(["Custom-Header1" : "value1", "Custom-Header2" : "value2"])
2929
.build();
3030
client.post(postRequest, new CredentialsHttpResponseHandler);
3131

3232
Request getRequest = new Request.Builder()
3333
.url("get?name=Eugene&key1=value1&key2=value2")
3434
.params(params)
35-
.headers(["CustomHeader3" : "value3", "CustomHeader4" : "value4", "Content-Type" : "Custon Content Type"])
35+
.headers(["Custom-Header3" : "value3", "Custom-Header4" : "value4", "Content-Type" : "Custon Content Type"])
3636
.build();
3737
client.get(getRequest, new CredentialsHttpResponseHandler);
38+
39+
Request delRequest = new Request.Builder()
40+
.url("delete?name=Eugene&key1=value1&key2=value2")
41+
.params(params)
42+
.headers(["Custom-Header5" : "value5"])
43+
.build();
44+
client.del(delRequest, new CredentialsHttpResponseHandler);
45+
46+
Request patchRequest = new Request.Builder()
47+
.url("patch?name=Eugene&key1=value1&key2=value2")
48+
.params(params)
49+
.headers(["Custom-Header5" : "value5"])
50+
.build();
51+
client.patch(patchRequest, new CredentialsHttpResponseHandler);
52+
53+
Request putRequest = new Request.Builder()
54+
.url("put")
55+
.params(params)
56+
.headers(["Custom-Header6" : "value6"])
57+
.build();
58+
//client.put(putRequest, new CredentialsHttpResponseHandler);
59+
//writeln(put("https://httpbin.org/put", "Hi!"));
3860
}
3961

4062
private class CredentialsHttpResponseHandler : HttpResponseHandler {

source/mars/AsyncHttpClient.d

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ class AsyncHttpClient : HttpClient {
1515
private HttpClientOptions mOptions;
1616
private HTTP mHttp;
1717

18-
this(HttpClientOptions options) {
18+
private this(HttpClientOptions options) {
1919
mOptions = options;
2020
mHttp = HTTP();
2121

2222
loadDefaultHeaders();
2323
}
2424

25+
static HttpClient init(HttpClientOptions options) {
26+
return new this(options);
27+
}
28+
2529
void post(Request request, HttpResponseHandler responseHandler) {
2630
doRequest(HTTP.Method.post, request, responseHandler);
2731
}
@@ -46,20 +50,22 @@ class AsyncHttpClient : HttpClient {
4650
import std.stdio;
4751
import std.conv;
4852

53+
string url = getCorrectUrl(request.getUrl);
54+
writeln("url: ", url);
55+
4956
int statusCode = StatusCode.BAD_REQUEST;
5057
string[string] responseHeaders;
5158
ubyte[] responseBody;
5259

5360
mHttp.method = httpMethod;
54-
if (httpMethod == HTTP.Method.post) {
55-
mHttp.url = getCorrectUrl(request.getUrl);
61+
if (httpMethod == HTTP.Method.post || httpMethod == HTTP.Method.del || httpMethod == HTTP.Method.patch || httpMethod == HTTP.Method.put) {
62+
mHttp.url = url;
5663
string contentType = "Content-Type" in mOptions.headers ? mOptions.headers["Content-Type"] : null;
5764
if (contentType is null) {
5865
contentType = "Content-Type" in request.getHeaders ? request.getHeaders["Content-Type"] : "application/json";
5966
}
6067
mHttp.setPostData(request.getParams.toJson, contentType);
6168
} else if (httpMethod == HTTP.Method.get) {
62-
string getUrl = getCorrectUrl(request.getUrl);
6369
string[string] paramsToSend = request.getParams.getParams;
6470

6571
if (paramsToSend !is null && paramsToSend.length > 0) {
@@ -68,14 +74,13 @@ class AsyncHttpClient : HttpClient {
6874
import std.algorithm.iteration;
6975

7076
string urlParams = paramsToSend.keys.map!(k => k ~ "=" ~ paramsToSend[k]).join("&");
71-
if (canFind(getUrl, "?")) {
72-
mHttp.url = getUrl ~ "&" ~ urlParams;
77+
if (canFind(url, "?")) {
78+
mHttp.url = url ~ "&" ~ urlParams;
7379
} else {
74-
string finalUrl = getUrl ~ "?" ~ urlParams;
75-
mHttp.url = finalUrl;
80+
mHttp.url = url ~ "?" ~ urlParams;
7681
}
7782
} else {
78-
mHttp.url = getCorrectUrl(request.getUrl);
83+
mHttp.url = url;
7984
}
8085
}
8186
if (request.getHeaders != null && request.getHeaders.length > 0) {

source/mars/HttpClient.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
module mars.HttpClient;
22

3+
import mars.HttpClientOptions;
34
import mars.HttpResponseHandler;
45
import mars.Request;
56

67
interface HttpClient {
8+
9+
static HttpClient init(HttpClientOptions options);
10+
711
void post(Request request, HttpResponseHandler responseHandler);
812

913
void get(Request request, HttpResponseHandler responseHandler);

0 commit comments

Comments
 (0)