public override HttpContent GetHttpContent(MediaTypeHeaderValue contentType, IFeignOptions options)
        {
            Type type = typeof(T);

            if (!type.IsValueType && Content == null)
            {
                return(null);
            }
            if (type == typeof(byte[]))
            {
                //throw new NotSupportedException();
                return(null);
            }
            if (typeof(Stream).IsAssignableFrom(type))
            {
                //throw new NotSupportedException();
                return(null);
            }

            if (Type.GetTypeCode(type) != TypeCode.Object)
            {
                return(new StringContent(Content.ToString()));
            }

            List <KeyValuePair <string, string> > nameValueCollection = FeignClientUtils.GetObjectStringParameters(Name, Content, options.Converters, options.PropertyNamingPolicy).ToList();
            FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent(nameValueCollection);

            return(formUrlEncodedContent);
        }
 private TResult GetResultInternal <TResult>(IMediaTypeFormatter mediaTypeFormatter, Stream stream, System.Net.Http.Headers.MediaTypeHeaderValue mediaTypeHeaderValue, Type resultType)
 {
     if (resultType != null)
     {
         return((TResult)mediaTypeFormatter.GetResult(resultType, stream, FeignClientUtils.GetEncoding(mediaTypeHeaderValue)));
     }
     return(mediaTypeFormatter.GetResult <TResult>(stream, FeignClientUtils.GetEncoding(mediaTypeHeaderValue)));
 }
Ejemplo n.º 3
0
 protected string ReplaceStringRequestQuery(string uri, string name, string value)
 {
     if (value == null)
     {
         return(uri);
     }
     return(FeignClientUtils.ReplaceRequestQuery(uri, name, value, FeignOptions.UseUrlEncode));
 }
Ejemplo n.º 4
0
 protected string ReplaceNullableRequestQuery <T>(string uri, string name, T?value) where T : struct
 {
     if (!value.HasValue)
     {
         return(uri);
     }
     return(FeignClientUtils.ReplaceRequestQuery(uri, name, value.Value.ToString(), FeignOptions.UseUrlEncode));
 }
Ejemplo n.º 5
0
        public override HttpContent GetHttpContent(MediaTypeHeaderValue contentType)
        {
            Type type = typeof(T);

            if (type == typeof(byte[]) || typeof(Stream).IsAssignableFrom(type))
            {
                //throw new NotSupportedException($"不支持{type.FullName}类型的参数");
                return(null);
            }
            Encoding encoding = FeignClientUtils.GetEncoding(contentType);

            //return new ObjectContent(Content, encoding ?? Encoding.UTF8, contentType);
            return(new ObjectStringContent(Content, encoding ?? Encoding.UTF8, contentType.MediaType));
        }
        private async Task <TResult> GetResultAsyncInternal <TResult>(IMediaTypeFormatter mediaTypeFormatter, Stream stream, System.Net.Http.Headers.MediaTypeHeaderValue mediaTypeHeaderValue, Type resultType)
        {
            if (resultType != null)
            {
                return((TResult)await mediaTypeFormatter.GetResultAsync(resultType, stream, FeignClientUtils.GetEncoding(mediaTypeHeaderValue))
#if CONFIGUREAWAIT_FALSE
                       .ConfigureAwait(false)
#endif
                       );
            }
            return(await mediaTypeFormatter.GetResultAsync <TResult>(stream, FeignClientUtils.GetEncoding(mediaTypeHeaderValue))
#if CONFIGUREAWAIT_FALSE
                   .ConfigureAwait(false)
#endif
                   );
        }
        public override HttpContent GetHttpContent(MediaTypeHeaderValue contentType, IFeignOptions options)
        {
            if (RequestFileForm == null)
            {
                return(null);
            }
            string boundary = contentType?.Parameters.FirstOrDefault(s => s.Name == "boundary")?.Value;

            if (string.IsNullOrWhiteSpace(boundary))
            {
                boundary = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString("N")));
            }
            MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent(boundary);

            if (RequestFileForm.RequestFiles != null)
            {
                foreach (var requestFile in RequestFileForm.RequestFiles)
                {
                    HttpContent httpContent = requestFile?.GetHttpContent();
                    if (httpContent != null)
                    {
                        multipartFormDataContent.Add(httpContent);
                    }
                }
            }

            //other property
            foreach (var property in RequestFileForm.GetType().GetProperties())
            {
                if (property.GetMethod == null)
                {
                    continue;
                }
                if (typeof(IHttpRequestFile).IsAssignableFrom(property.PropertyType) || property.PropertyType.IsGenericType && property.PropertyType.GenericTypeArguments.Any(s => typeof(IHttpRequestFile).IsAssignableFrom(s)))
                {
                    continue;
                }
                object value = property.GetValue(RequestFileForm);
                if (value == null)
                {
                    continue;
                }
                HttpContent httpContent = new StringContent(value.ToString());
                multipartFormDataContent.Add(httpContent, FeignClientUtils.GetName(property, options.PropertyNamingPolicy));
            }
            return(multipartFormDataContent);
        }
Ejemplo n.º 8
0
 protected string ReplaceRequestQuery <T>(string uri, string name, T value)
 {
     return(FeignClientUtils.ReplaceRequestQuery <T>(_feignOptions.Converters, uri, name, value));
 }
Ejemplo n.º 9
0
 protected string ReplacePathVariable <T>(string uri, string name, T value)
 {
     return(FeignClientUtils.ReplacePathVariable <T>(_feignOptions.Converters, uri, name, value));
 }
Ejemplo n.º 10
0
        private async Task <TResult> GetResultAsync <TResult>(FeignClientRequest request, HttpResponseMessage responseMessage)
        {
            if (responseMessage == null)
            {
                return(default(TResult));
            }
            #region ReceivingResponse
            ReceivingResponseEventArgs <TResult> receivingResponseEventArgs = new ReceivingResponseEventArgs <TResult>(this, responseMessage);
            _globalFeignClientPipeline?.GetServicePipeline(this.ServiceId)?.OnReceivingResponse(this, receivingResponseEventArgs);
            _globalFeignClientPipeline?.OnReceivingResponse(this, receivingResponseEventArgs);
            //if (receivingResponseEventArgs.Result != null)
            if (receivingResponseEventArgs._isSetResult)
            {
                return(receivingResponseEventArgs.GetResult <TResult>());
            }
            #endregion
            await EnsureSuccessAsync(request, responseMessage);

            if (typeof(TResult) == typeof(Task))
            {
#if NET45
                return((TResult)(object)Task.FromResult <object>(null));
#endif
#if NETSTANDARD
                return((TResult)(object)Task.CompletedTask);
#endif
            }
            if (typeof(TResult) == typeof(string))
            {
                return((TResult)(object)await responseMessage.Content.ReadAsStringAsync());
            }
            IMediaTypeFormatter mediaTypeFormatter = _feignOptions.MediaTypeFormatters.FindFormatter(responseMessage.Content.Headers.ContentType?.MediaType);
            if (mediaTypeFormatter == null)
            {
                throw new FeignHttpRequestException(this,
                                                    responseMessage.RequestMessage as FeignHttpRequestMessage,
                                                    new HttpRequestException($"Content type '{responseMessage.Content.Headers.ContentType.ToString()}' not supported"));
            }
            return(mediaTypeFormatter.GetResult <TResult>(await responseMessage.Content.ReadAsByteArrayAsync(), FeignClientUtils.GetEncoding(responseMessage.Content.Headers.ContentType)));
        }
Ejemplo n.º 11
0
 protected string ReplaceToStringRequestQuery <T>(string uri, string name, T value) where T : struct
 {
     return(FeignClientUtils.ReplaceRequestQuery(uri, name, value.ToString(), FeignOptions.UseUrlEncode));
 }
Ejemplo n.º 12
0
 protected string ReplaceRequestQuery <T>(string uri, string name, T value)
 {
     return(FeignClientUtils.ReplaceRequestQuery <T>(FeignOptions.Converters, FeignOptions.PropertyNamingPolicy, uri, name, value, FeignOptions.UseUrlEncode));
 }
Ejemplo n.º 13
0
 protected string ReplaceNullablePathVariable <T>(string uri, string name, T?value) where T : struct
 {
     return(FeignClientUtils.ReplacePathVariable(uri, name, value.ToString(), FeignOptions.UseUrlEncode));
 }
Ejemplo n.º 14
0
 protected string ReplaceStringPathVariable(string uri, string name, string value)
 {
     return(FeignClientUtils.ReplacePathVariable(uri, name, value, FeignOptions.UseUrlEncode));
 }