Ejemplo n.º 1
0
        public static void AddAngleSharp <TService>(this IFeignClientPipeline <TService> feignClientPipeline)
        {
            feignClientPipeline.ReceivingResponse += async(sender, e) =>
            {
                if (e.ResultType == typeof(IHtmlDocument))
                {
                    var stream = await e.ResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    e.Result = new HtmlParser().ParseDocument(stream);
                }
            };
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 添加授权
 /// </summary>
 /// <typeparam name="TService"></typeparam>
 /// <param name="feignClientPipeline"></param>
 /// <param name="authenticationHeaderValue"></param>
 /// <returns></returns>
 public static IFeignClientPipeline <TService> Authorization <TService>(this IFeignClientPipeline <TService> feignClientPipeline, AuthenticationHeaderValue authenticationHeaderValue)
 {
     if (authenticationHeaderValue == null)
     {
         throw new ArgumentNullException(nameof(authenticationHeaderValue));
     }
     feignClientPipeline.BuildingRequest += (sender, e) =>
     {
         if (!e.Headers.ContainsKey("Authorization"))
         {
             e.Headers["Authorization"] = authenticationHeaderValue.Scheme + " " + authenticationHeaderValue.Parameter;
         }
     };
     return(feignClientPipeline);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 添加授权
 /// </summary>
 /// <typeparam name="TService"></typeparam>
 /// <param name="feignClientPipeline"></param>
 /// <param name="scheme"></param>
 /// <param name="parameter"></param>
 /// <returns></returns>
 public static IFeignClientPipeline <TService> Authorization <TService>(this IFeignClientPipeline <TService> feignClientPipeline, string scheme, string parameter)
 {
     if (scheme == null)
     {
         throw new ArgumentNullException(nameof(scheme));
     }
     if (parameter == null)
     {
         throw new ArgumentNullException(nameof(parameter));
     }
     feignClientPipeline.BuildingRequest += (sender, e) =>
     {
         if (!e.Headers.ContainsKey("Authorization"))
         {
             e.Headers["Authorization"] = scheme + " " + parameter;
         }
     };
     return(feignClientPipeline);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 添加授权
 /// </summary>
 /// <typeparam name="TService"></typeparam>
 /// <param name="feignClientPipeline"></param>
 /// <param name="schemeAndParameterFactory"></param>
 /// <returns></returns>
 public static IFeignClientPipeline <TService> Authorization <TService>(this IFeignClientPipeline <TService> feignClientPipeline, Func <IFeignClient <TService>, (string, string)> schemeAndParameterFactory)
Ejemplo n.º 5
0
        public static void ReceivingQueryResult <T>(this IFeignClientPipeline <T> globalFeignClient)
        {
            globalFeignClient.ReceivingResponse += (sender, e) =>
            {
                if (!typeof(IQueryResult).IsAssignableFrom(e.ResultType))
                {
                    return;
                }
                if (e.ResultType == typeof(IQueryResult))
                {
                    e.Result = new QueryResult()
                    {
                        StatusCode = e.ResponseMessage.StatusCode
                    };
                    return;
                }

                Feign.Request.FeignHttpRequestMessage feignHttpRequestMessage = e.ResponseMessage.RequestMessage as Feign.Request.FeignHttpRequestMessage;

                var resultType = feignHttpRequestMessage.FeignClientRequest.Method.ResultType;


                if (e.ResultType.IsGenericType && e.ResultType.GetGenericTypeDefinition() == typeof(IQueryResult <>))
                {
                    QueryResult queryResult;
                    if (e.ResponseMessage.IsSuccessStatusCode)
                    {
                        var    content = e.ResponseMessage.Content;
                        var    buffer  = content.ReadAsByteArrayAsync().Result;
                        var    json1   = Encoding.GetEncoding("iso-8859-1").GetString(buffer);
                        string json    = content.ReadAsStringAsync().Result;
                        object data    = Newtonsoft.Json.JsonConvert.DeserializeObject(json, e.ResultType.GetGenericArguments()[0]);
                        if (data == null)
                        {
                            queryResult = InvokeQueryResultConstructor(e.ResultType.GetGenericArguments()[0]);
                        }
                        else
                        {
                            queryResult = InvokeQueryResultConstructor(data.GetType(), data);
                        }
                    }
                    else
                    {
                        queryResult = InvokeQueryResultConstructor(e.ResultType.GetGenericArguments()[0]);
                    }
                    queryResult.StatusCode = e.ResponseMessage.StatusCode;
                    e.Result = queryResult;
                }

                else if (e.ResultType.IsGenericType && e.ResultType.GetGenericTypeDefinition() == typeof(QueryResult <>))
                {
                    QueryResult queryResult;
                    if (e.ResponseMessage.IsSuccessStatusCode)
                    {
                        var    content = e.ResponseMessage.Content;
                        var    buffer  = content.ReadAsByteArrayAsync().Result;
                        var    json1   = Encoding.GetEncoding("iso-8859-1").GetString(buffer);
                        string json    = content.ReadAsStringAsync().Result;
                        object data    = Newtonsoft.Json.JsonConvert.DeserializeObject(json, e.ResultType.GetGenericArguments()[0]);
                        if (data == null)
                        {
                            queryResult = InvokeQueryResultConstructor(e.ResultType.GetGenericArguments()[0]);
                        }
                        else
                        {
                            queryResult = InvokeQueryResultConstructor(data.GetType(), data);
                        }
                    }
                    else
                    {
                        queryResult = InvokeQueryResultConstructor(e.ResultType.GetGenericArguments()[0]);
                    }
                    queryResult.StatusCode = e.ResponseMessage.StatusCode;
                    e.Result = queryResult;
                }
            };
        }