Skip to content

Commit

Permalink
set args and data in builder, created UrlHelper, some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
eresid committed Jan 8, 2017
1 parent a24d561 commit 46d0e8e
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 58 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ Simple REST client on D language
* ~~User Agent~~
* ~~DELETE request~~
* ~~PATCH request~~
* Set args and data in builder
* PUT request
* ~~Add package.d file~~
* ~~Set args and data in builder~~
* ~~Add UrlHelper~~

[0.3]
* First release in http://code.dlang.org/
* VK sample
* httpbin.org sample
* Dynamic parameters in URL
* PUT request
* Unit Tests

[0.4]
* Multipart/form-data
Expand All @@ -38,7 +42,6 @@ Simple REST client on D language
* Documentation
* wiki
* javadoc
* Unit Tests
* RX support
* Json serialization/deserialization
* OAuth 2
Expand Down
30 changes: 12 additions & 18 deletions source/app.d
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import std.net.curl;
import std.stdio;

import mars.AsyncHttpClient;
import mars.HttpClient;
import mars.HttpClientOptions;
import mars.HttpResponseHandler;
import mars.Request;
import mars.RequestParams;
import mars.ServerException;
import mars;

string BASE_URL = "https://httpbin.org/";

Expand All @@ -24,42 +18,42 @@ void main()

Request postRequest = new Request.Builder()
.url("post?name=Eugene")
.params(params)
.data(params.toJson)
.headers(["Custom-Header1" : "value1", "Custom-Header2" : "value2"])
.build();
client.post(postRequest, new CredentialsHttpResponseHandler);
client.post(postRequest, new ResponseListener);

Request getRequest = new Request.Builder()
.url("get?name=Eugene&key1=value1&key2=value2")
.params(params)
.data(params.toJson)
.headers(["Custom-Header3" : "value3", "Custom-Header4" : "value4", "Content-Type" : "Custon Content Type"])
.build();
client.get(getRequest, new CredentialsHttpResponseHandler);
client.get(getRequest, new ResponseListener);

Request delRequest = new Request.Builder()
.url("delete?name=Eugene&key1=value1&key2=value2")
.params(params)
.data(params.toJson)
.headers(["Custom-Header5" : "value5"])
.build();
client.del(delRequest, new CredentialsHttpResponseHandler);
client.del(delRequest, new ResponseListener);

Request patchRequest = new Request.Builder()
.url("patch?name=Eugene&key1=value1&key2=value2")
.params(params)
.data(params.toJson)
.headers(["Custom-Header5" : "value5"])
.build();
client.patch(patchRequest, new CredentialsHttpResponseHandler);
client.patch(patchRequest, new ResponseListener);

Request putRequest = new Request.Builder()
.url("put")
.params(params)
.data(params.toJson)
.headers(["Custom-Header6" : "value6"])
.build();
//client.put(putRequest, new CredentialsHttpResponseHandler);
//client.put(putRequest, new ResponseListener);
//writeln(put("https://httpbin.org/put", "Hi!"));
}

private class CredentialsHttpResponseHandler : HttpResponseHandler {
private class ResponseListener : HttpResponseHandler {
void onSuccess(int statusCode, string[string] headers, ubyte[] responseBody) {
import std.conv;

Expand Down
43 changes: 6 additions & 37 deletions source/mars/AsyncHttpClient.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import mars.Request;
import mars.RequestParams;
import mars.ServerException;
import mars.StatusCode;
import mars.UrlHelper;

class AsyncHttpClient : HttpClient {

Expand Down Expand Up @@ -50,39 +51,21 @@ class AsyncHttpClient : HttpClient {
import std.stdio;
import std.conv;

string url = getCorrectUrl(request.getUrl);
writeln("url: ", url);

int statusCode = StatusCode.BAD_REQUEST;
string[string] responseHeaders;
ubyte[] responseBody;

mHttp.method = httpMethod;
if (httpMethod == HTTP.Method.post || httpMethod == HTTP.Method.del || httpMethod == HTTP.Method.patch || httpMethod == HTTP.Method.put) {
mHttp.url = url;
mHttp.url = UrlHelper.createUrl(mOptions.baseUrl, request.getUrl, request.getParams);

if (httpMethod != HTTP.Method.get) {
string contentType = "Content-Type" in mOptions.headers ? mOptions.headers["Content-Type"] : null;
if (contentType is null) {
contentType = "Content-Type" in request.getHeaders ? request.getHeaders["Content-Type"] : "application/json";
}
mHttp.setPostData(request.getParams.toJson, contentType);
} else if (httpMethod == HTTP.Method.get) {
string[string] paramsToSend = request.getParams.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(url, "?")) {
mHttp.url = url ~ "&" ~ urlParams;
} else {
mHttp.url = url ~ "?" ~ urlParams;
}
} else {
mHttp.url = url;
}
mHttp.setPostData(request.getData, contentType);
}

if (request.getHeaders != null && request.getHeaders.length > 0) {
foreach (name, value; request.getHeaders) {
mHttp.addRequestHeader(name, value);
Expand Down Expand Up @@ -128,18 +111,4 @@ class AsyncHttpClient : HttpClient {
}
}
}

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;
}

return mOptions.baseUrl ~ url;
}
}
12 changes: 12 additions & 0 deletions source/mars/Request.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Request {

private string url;
private RequestParams params;
private string data;
private string[string] headers;

string getUrl() {
Expand All @@ -16,19 +17,25 @@ class Request {
return params;
}

string getData() {
return data;
}

string[string] getHeaders() {
return headers;
}

private this(Builder builder) {
url = builder.mUrl;
params = builder.mParams;
data = builder.mData;
headers = builder.mHeaders;
}

static class Builder {
private string mUrl;
private RequestParams mParams;
private string mData;
private string[string] mHeaders;

Builder url(string value) {
Expand All @@ -41,6 +48,11 @@ class Request {
return this;
}

Builder data(string value) {
mData = value;
return this;
}

Builder headers(string[string] value) {
mHeaders = value;
return this;
Expand Down
56 changes: 56 additions & 0 deletions source/mars/UrlHelper.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module mars.UrlHelper;

import std.algorithm.searching;
import std.exception;

version(unittest) {
import std.stdio;
}

import mars.RequestParams;

class UrlHelper {

static string createUrl(string baseUrl, string requestUrl, RequestParams params) {
enforce(requestUrl !is null, new Exception("Cannot get request Url!"));

if (requestUrl.startsWith("http")) {
return addArgsIfNeed(requestUrl, params);
}

enforce(baseUrl !is null, new Exception("Cannot get base Url!"));

return baseUrl ~ addArgsIfNeed(requestUrl, params);
}

private static string addArgsIfNeed(string url, RequestParams params) {
if (params is null || params.getParams.length == 0) {
return url;
}

import std.string;
import std.algorithm.iteration;

string[string] args = params.getParams;

string urlParams = args.keys.map!(k => k ~ "=" ~ args[k]).join("&");
if (canFind(url, "?")) {
url = url ~ "&" ~ urlParams;
} else {
url = url ~ "?" ~ urlParams;
}

return url;
}
}

unittest {
try {
UrlHelper.createUrl(null, null, null);
assert(false);
} catch (Exception e) {
writeln(e.msg);
assert(e !is null);
assert(e.msg == "Cannot get request Url!");
}
}
9 changes: 9 additions & 0 deletions source/mars/package.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module mars;

package import mars.AsyncHttpClient;
package import mars.HttpClient;
package import mars.HttpClientOptions;
package import mars.HttpResponseHandler;
package import mars.Request;
package import mars.RequestParams;
package import mars.ServerException;

0 comments on commit 46d0e8e

Please sign in to comment.