public void Proceed(IInvocation invocation) { var baseUri = new Uri(ServiceInformation.BaseUrl); var uriBuilder = new UriBuilder(new Uri(baseUri, _targetMethodInfo.Url)); var parameters = ParametersUtils.ExtractHttpRequestParameters(invocation.Arguments, _targetMethodInfo.MethodInfo); // add query params uriBuilder.AddQueryParameters(parameters.QueryParameters); var headers = CombineParamHeadersWithGlobalHeaders(Headers, parameters.HeadersParameters); var url = uriBuilder.ToString(); var req = new InvokeHttpRequest(url, parameters, _targetMethodInfo.HttpMethod); var invocationMethodReturnType = invocation.MethodInvocationTarget.ReturnType; if (IsTask(invocationMethodReturnType)) { invocation.InvokeHttpMethodAsync(Serializer, headers, req); } else { invocation.InvokeHttpMethod(Serializer, headers, req); } }
internal static void Post(ISerializer serializer, Headers headers, InvokeHttpRequest req) { using (HttpClient client = new HttpClient()) { PerformPostAsync(serializer, headers, req, client).GetAwaiter().GetResult(); } }
internal static Task GetAsync(ISerializer serializer, Headers headers, InvokeHttpRequest req) { return(Task.Run(async() => { using (HttpClient client = new HttpClient()) { await PerformGetAsync(headers, req, client); } })); }
private static StringContent CreateJsonContent(ISerializer serializer, InvokeHttpRequest req) { if (req.Parameters.BodyParameters.Count == 1 && req.Parameters.HeadersParameters.Count == 0 && req.Parameters.QueryParameters.Count == 0) { return(new StringContent(serializer.Serialize(req.Parameters.BodyParameters.First().Value), Encoding.UTF8, "application/json")); } return(new StringContent(serializer.Serialize(req.Parameters.BodyParameters), Encoding.UTF8, "application/json")); }
internal static Task PostAsync(ISerializer serializer, Headers headers, InvokeHttpRequest req) { return(Task.Run(async() => { using (HttpClient client = new HttpClient()) { client.AddHeaders(headers); StringContent requestContent = CreateJsonContent(serializer, req); await client.PostAsync(req.Url, requestContent); } })); }
private static async Task <HttpResponseMessage> PerformPostAsync(ISerializer serializer, Headers headers, InvokeHttpRequest req, HttpClient client) { client.AddHeaders(headers); var content = CreateJsonContent(serializer, req); var respose = await client.PostAsync(req.Url, content); if (!respose.IsSuccessStatusCode) { throw new ProxyHttpException(respose.StatusCode, respose.ReasonPhrase); } return(respose); }
internal static object Post(Type returnType, ISerializer serializer, Headers headers, InvokeHttpRequest req) { using (HttpClient client = new HttpClient()) { var content = PerformPostAsync(serializer, headers, req, client).Result.Content; var jsonResponse = content.ReadAsStringAsync().Result; return(ProcessResult(serializer, jsonResponse, returnType)); } }
private static async Task <HttpResponseMessage> PerformGetAsync(Headers headers, InvokeHttpRequest req, HttpClient client) { client.AddHeaders(headers); var httpResponse = await client.GetAsync(req.Url); if (!httpResponse.IsSuccessStatusCode) { throw new ProxyHttpException(httpResponse.StatusCode, httpResponse.ReasonPhrase); } return(httpResponse); }
internal static Task <T> GetAsync <T>(Task <T> originalTask, ISerializer serializer, Headers headers, InvokeHttpRequest req) { return(Task.Run(async() => { using (HttpClient client = new HttpClient()) { var httpResponse = await PerformGetAsync(headers, req, client); var jsonResponse = await httpResponse.Content.ReadAsStringAsync(); return ProcessResult <T>(serializer, jsonResponse); } })); }
internal static void InvokeHttpMethod(this IInvocation invocation, ISerializer serializer, Headers headers, InvokeHttpRequest req) { var returnType = invocation.Method.ReturnType.IsGenericType ? invocation.Method.ReturnType.GetGenericTypeDefinition() : invocation.Method.ReturnType; switch (req.HttpMethod) { case Attributes.HttpMethod.Get: if (invocation.MethodInvocationTarget.ReturnType == typeof(void)) { RestInterceptorUtils.Get(serializer, headers, req); } else { invocation.ReturnValue = RestInterceptorUtils.Get(invocation.MethodInvocationTarget.ReturnType, serializer, headers, req); } return; case Attributes.HttpMethod.Post: if (invocation.MethodInvocationTarget.ReturnType == typeof(void)) { RestInterceptorUtils.Post(serializer, headers, req); } else { invocation.ReturnValue = RestInterceptorUtils.Post(invocation.MethodInvocationTarget.ReturnType, serializer, headers, req); } return; default: break; } }
internal static void InvokeHttpMethodAsync(this IInvocation invocation, ISerializer serializer, Headers headers, InvokeHttpRequest req) { switch (req.HttpMethod) { case Attributes.HttpMethod.Get: if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = RestInterceptorUtils.GetAsync(serializer, headers, req); } else { invocation.ReturnValue = RestInterceptorUtils.GetAsync((dynamic)invocation.ReturnValue, serializer, headers, req); } break; case Attributes.HttpMethod.Post: if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = RestInterceptorUtils.PostAsync(serializer, headers, req); } else { invocation.ReturnValue = RestInterceptorUtils.PostAsync((dynamic)invocation.ReturnValue, serializer, headers, req); } break; default: break; } }