public async Task<object> Call(IHttpHandler httpHandler, string baseUrl, Dictionary<string, object> arguments, HttpApiInstrumenter apiInstrumenter = null) { var request = new HttpApiRequest { Url = Url.CreateUrl(baseUrl), Method = Method, Headers = Headers.ToList() }; Action<Func<IHttpArgumentHandler, string, object, Task>> applyArguments = async applier => { foreach (var item in ArgumentHandlers) { var name = item.Key; object argument; if (arguments.TryGetValue(name, out argument)) { var handler = item.Value; await applier(handler, name, argument); } } }; applyArguments(async (handler, name, argument) => await handler.ApplyArgument(request, name, argument)); Func<HttpApiRequest, Task<HttpApiResponse>> call = async apiRequest => await httpHandler.Call(apiRequest); HttpApiResponse response; if (apiInstrumenter != null) response = await apiInstrumenter(request, call); else response = await call(request); applyArguments(async (handler, name, argument) => await handler.ApplyArgument(response, name, argument)); return await ResponseHandler.HandleResponse(request, response); }
public Task<object> HandleResponse(HttpApiRequest request, HttpApiResponse response) { if (NonSuccessThrowsException && ((int)response.StatusCode < 200 || (int)response.StatusCode >= 300)) { throw new NonSuccessfulResponseException(request, response); } return ProvideResult(request, response); }
public Task <object> HandleResponse(HttpApiRequest request, HttpApiResponse response) { if (NonSuccessThrowsException && ((int)response.StatusCode < 200 || (int)response.StatusCode >= 300)) { throw new NonSuccessfulResponseException(request, response); } return(ProvideResult(request, response)); }
public async Task <object> Call(IHttpHandler httpHandler, string baseUrl, HttpApiArguments arguments, HttpApiInstrumenter apiInstrumenter = null) { async void ApplyArguments(Func <IHttpArgumentHandler, string, object, Task> applier) { foreach (var item in ArgumentHandlers) { var name = item.Key; if (arguments.TryGetValue(name, out var argument)) { var handler = item.Value; await applier(handler, name, argument); } } } HttpApiRequest GetRequest() { var request = new HttpApiRequest { Url = Url.CreateUrl(baseUrl), Method = HttpMethod, Headers = Headers.ToList() }; ApplyArguments(async(handler, name, argument) => await handler.ApplyArgument(request, name, argument)); if (!request.Headers.Exists(x => x.Name == "Content-Type") && request.Body != null) { var contentType = request.Body.Accept(new ContentTypeCalculator()); request.Headers.Add(new HttpHeader("Content-Type", contentType)); } return(request); } async Task <HttpHandlerResponse> GetResponse(HttpApiRequest request) { return(await httpHandler.Call(request)); } async Task <object> GetResult(HttpApiRequest request, HttpHandlerResponse response) { ApplyArguments(async(handler, name, argument) => await handler.ApplyArgument(response.ApiResponse, name, argument)); return(await ResponseHandler.HandleResponse(request, response.ApiResponse)); } IHttpApiInstrumentation instrumentation = new DefaultHttpApiInstrumentation(GetRequest, GetResponse, GetResult); if (apiInstrumenter != null) { instrumentation = apiInstrumenter(this, arguments, instrumentation); } return(await MakeCall(instrumentation)); }
public HttpApiRequest Clone() { var result = new HttpApiRequest { Method = Method, Url = Url.Clone(), Headers = Headers.ToList(), Items = Items.ToDictionary(x => x.Key, x => x.Value), Proxy = Proxy, Body = Body, ResponseContentTypeOverride = ResponseContentTypeOverride }; return(result); }
public virtual Task ApplyArgument(HttpApiRequest request, string name, object argument) { return TaskConstants.Completed; }
protected abstract Task<object> ProvideResult(HttpApiRequest request, HttpApiResponse response);
public virtual object AggregateResult(HttpApiRequest request, HttpHandlerResponse response, object lastResult, object result) { return(result); }
public virtual Task <object> GetResult(HttpApiRequest request, HttpHandlerResponse response) { return(getResult(request, response)); }
public virtual Task <HttpHandlerResponse> GetResponse(HttpApiRequest request) { return(getResponse(request)); }
protected abstract Task <object> ProvideResult(HttpApiRequest request, HttpApiResponse response);
public NonSuccessfulResponseException(HttpApiRequest request, HttpApiResponse response) : base($"Server responded with status: {response.StatusCode} when contacting {request.Url}:\r\n{response.Body}") { Request = request; Response = response; }
public virtual Task ApplyArgument(HttpApiRequest request, string name, object argument) { return(TaskConstants.Completed); }