1 module mars.clients.VibedHttpClient; 2 3 import core.thread; 4 import std.stdio; 5 6 import mars.HttpResponseHandler; 7 import mars.HttpClient; 8 import mars.HttpClientOptions; 9 import mars.HttpRequest; 10 import mars.HttpResponse; 11 import mars.RequestParams; 12 import mars.ServerException; 13 import mars.StatusCode; 14 import mars.UrlHelper; 15 16 //import vibe.http.client; 17 18 /* 19 class VibedHttpClient : 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(HTTPMethod.POST, request, responseHandler); 33 } 34 35 HttpResponse post(HttpRequest request) { 36 return doRequest(HTTPMethod.POST, request); 37 } 38 39 void get(HttpRequest request, HttpResponseHandler responseHandler) { 40 doRequest(HTTPMethod.GET, request, responseHandler); 41 } 42 43 HttpResponse get(HttpRequest request) { 44 return doRequest(HTTPMethod.GET, request); 45 } 46 47 void put(HttpRequest request, HttpResponseHandler responseHandler) { 48 doRequest(HTTPMethod.PUT, request, responseHandler); 49 } 50 51 HttpResponse put(HttpRequest request) { 52 return doRequest(HTTPMethod.PUT, request); 53 } 54 55 void del(HttpRequest request, HttpResponseHandler responseHandler) { 56 doRequest(HTTPMethod.DELETE, request, responseHandler); 57 } 58 59 HttpResponse del(HttpRequest request) { 60 return doRequest(HTTPMethod.DELETE, request); 61 } 62 63 void patch(HttpRequest request, HttpResponseHandler responseHandler) { 64 doRequest(HTTPMethod.PATCH, request, responseHandler); 65 } 66 67 HttpResponse patch(HttpRequest request) { 68 return doRequest(HTTPMethod.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(HTTPMethod httpMethod, HttpRequest request) { 81 82 _res.statusCode = 0; 83 string requestUrl = UrlHelper.createUrl(mOptions.baseUrl, request.getUrl, request.getParams); 84 85 try requestHTTP(requestUrl, 86 (scope req) { 87 req.method = httpMethod; 88 89 if (request.getHeaders != null && request.getHeaders.length > 0) { 90 foreach (name, value; request.getHeaders) { 91 req.headers[name] = value; 92 } 93 } 94 95 //logInfo("body: %s",_req.toJson().toString); 96 97 req.writeJsonBody(_req.toJson()); 98 }, 99 (scope HTTPClientResponse res) { 100 101 _res.statusCode = res.statusCode; 102 103 if(res.statusCode == 200) 104 parseSuccessResult(res.readJson(), _req, _res); 105 else 106 logInfo("response: %s", cast(string)res.bodyReader.readAll()); 107 108 Response response = new Response.Builder() 109 .url(requestUrl) 110 .statusCode(res.statusCode) 111 .headers(res.headers) 112 .responseBody(res.bodyReader.readAll()) 113 .exception(StatusCode.isSuccess(statusCode) ? null : new ServerException(statusCode)) 114 .build(); 115 return response; 116 } 117 ); 118 catch(Exception e) 119 { 120 logError("[gmc] request failed: %s",e); 121 } 122 123 // TODO 124 125 return response; 126 return null; 127 } 128 129 private void doRequest(HTTPMethod httpMethod, HttpRequest request, HttpResponseHandler responseHandler) { 130 // TODO move to thread 131 responseHandler.onResponse(doRequest(httpMethod, request)); 132 } 133 }*/