コード例 #1
0
ファイル: HttpApiEndpoint.cs プロジェクト: kswoll/sexy-http
        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);
        }
コード例 #2
0
 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);
 }
コード例 #3
0
 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));
 }
コード例 #4
0
ファイル: HttpApiEndpoint.cs プロジェクト: kswoll/sexy-http
        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));
        }
コード例 #5
0
        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);
        }
コード例 #6
0
 public virtual Task ApplyArgument(HttpApiRequest request, string name, object argument)
 {
     return TaskConstants.Completed;
 }
コード例 #7
0
 protected abstract Task<object> ProvideResult(HttpApiRequest request, HttpApiResponse response);
コード例 #8
0
 public virtual object AggregateResult(HttpApiRequest request, HttpHandlerResponse response, object lastResult, object result)
 {
     return(result);
 }
コード例 #9
0
 public virtual Task <object> GetResult(HttpApiRequest request, HttpHandlerResponse response)
 {
     return(getResult(request, response));
 }
コード例 #10
0
 public virtual Task <HttpHandlerResponse> GetResponse(HttpApiRequest request)
 {
     return(getResponse(request));
 }
コード例 #11
0
 protected abstract Task <object> ProvideResult(HttpApiRequest request, HttpApiResponse response);
コード例 #12
0
 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;
 }
コード例 #13
0
 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;
 }
コード例 #14
0
 public virtual Task ApplyArgument(HttpApiRequest request, string name, object argument)
 {
     return(TaskConstants.Completed);
 }