1 module mars.clients.RequestsHttpClient; 2 3 import core.thread; 4 import std.net.curl; 5 import std.stdio; 6 7 import mars.HttpResponseHandler; 8 import mars.HttpClient; 9 import mars.HttpClientOptions; 10 import mars.HttpRequest; 11 import mars.HttpResponse; 12 import mars.RequestParams; 13 import mars.ServerException; 14 import mars.StatusCode; 15 import mars.UrlHelper; 16 17 import requests; 18 19 class RequestsHttpClient : HttpClient { 20 21 private HttpClientOptions mOptions; 22 23 private this(HttpClientOptions options) { 24 mOptions = options; 25 } 26 27 static HttpClient init(HttpClientOptions options) { 28 return new this(options); 29 } 30 31 void post(HttpRequest request, HttpResponseHandler responseHandler) { 32 doRequest(HTTP.Method.post, request, responseHandler); 33 } 34 35 HttpResponse post(HttpRequest request) { 36 return doRequest(HTTP.Method.post, request); 37 } 38 39 void get(HttpRequest request, HttpResponseHandler responseHandler) { 40 doRequest(HTTP.Method.get, request, responseHandler); 41 } 42 43 HttpResponse get(HttpRequest request) { 44 return doRequest(HTTP.Method.get, request); 45 } 46 47 void put(HttpRequest request, HttpResponseHandler responseHandler) { 48 doRequest(HTTP.Method.put, request, responseHandler); 49 } 50 51 HttpResponse put(HttpRequest request) { 52 return doRequest(HTTP.Method.put, request); 53 } 54 55 void del(HttpRequest request, HttpResponseHandler responseHandler) { 56 doRequest(HTTP.Method.del, request, responseHandler); 57 } 58 59 HttpResponse del(HttpRequest request) { 60 return doRequest(HTTP.Method.del, request); 61 } 62 63 void patch(HttpRequest request, HttpResponseHandler responseHandler) { 64 doRequest(HTTP.Method.patch, request, responseHandler); 65 } 66 67 HttpResponse patch(HttpRequest request) { 68 return doRequest(HTTP.Method.patch, request); 69 } 70 71 void upload(HttpRequest request, File file, HttpResponseHandler responseHandler) { 72 // TODO move to thread 73 responseHandler.onResponse(upload(request, file)); 74 } 75 76 HttpResponse upload(HttpRequest request, File file) { 77 return null; 78 } 79 80 HttpResponse doRequest(HTTP.Method httpMethod, HttpRequest request) { 81 auto rq = Request(); 82 83 // TODO 84 85 /*Response response = new Response.Builder() 86 .url(requestUrl) 87 .statusCode(statusCode) 88 .headers(responseHeaders) 89 .responseBody(responseBody) 90 .exception(StatusCode.isSuccess(statusCode) ? null : new ServerException(statusCode)) 91 .build(); 92 93 return response;*/ 94 return null; 95 } 96 97 private void doRequest(HTTP.Method httpMethod, HttpRequest request, HttpResponseHandler responseHandler) { 98 // TODO move to thread 99 responseHandler.onResponse(doRequest(httpMethod, request)); 100 } 101 }