コード例 #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
ファイル: 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));
        }