public static bool RemoveFormatter(IMediaTypeFormatter formatter) { if (formatter == null) { throw new ArgumentNullException("formatter"); } var mediaTypes = new List<string>(); foreach (var formatterRegistration in formatters) { if (formatterRegistration.Value == formatter) { mediaTypes.Add(formatterRegistration.Key); } } IMediaTypeFormatter tempValue = null; foreach (string mediaType in mediaTypes) { formatters.TryRemove(mediaType, out tempValue); } return tempValue != null; }
private TResult GetResultInternal <TResult>(FeignClientHttpRequest request, HttpResponseMessage responseMessage) { EnsureSuccess(request, responseMessage); var specialResult = SpecialResults.GetSpecialResult <TResult>(responseMessage); if (specialResult.IsSpecialResult) { return(specialResult.Result); } if (responseMessage.Content.Headers.ContentType == null && responseMessage.Content.Headers.ContentLength == 0) { return(default(TResult)); } 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")); } using (var stream = responseMessage.Content.ReadAsStreamAsync().GetResult()) { if (stream.CanSeek) { return(GetResultInternal <TResult>(mediaTypeFormatter, stream, responseMessage.Content.Headers.ContentType, request.Method.ResultType)); } using (Stream seekStream = new MemoryStream()) { stream.CopyTo(seekStream); seekStream.Position = 0; return(GetResultInternal <TResult>(mediaTypeFormatter, seekStream, responseMessage.Content.Headers.ContentType, request.Method.ResultType)); } } }
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))); }
private void ApplyDefaultSettings() { this.UserAgent = ProductInfoHeaderValue.Parse("VisualStudio2015"); this.MsVersion = "2013-11-01"; this.AcceptLanguage = CultureInfo.CurrentUICulture.Name; // AzureRM should use JSON this.Formatter = JsonMediaTypeFormatter.Default; }
public ObjectContent(T content, IMediaTypeFormatter mediaTypeFormatter) { Check.NotNull(mediaTypeFormatter, nameof(mediaTypeFormatter)); this.content = content; this.mediaTypeFormatter = mediaTypeFormatter; this.SetMediaTypeFormatterHeaders(); }
internal RestRequest(HttpMethod httpMethod, IRestClient restClient) { this.httpRequestMessage = new HttpRequestMessage(); this.httpRequestMessage.Method = httpMethod; this.restClient = restClient; this.contentParts = new List <ContentPart>(); this.formFormatter = this.EnsureDefaultValueSet(this.restClient.Settings.FormFormatters); this.mediaTypeFormatter = this.EnsureDefaultValueSet(this.restClient.Settings.MediaTypeFormatters); this.valueFormatter = this.EnsureDefaultValueSet(this.restClient.Settings.UrlParameterFormatters); }
private async Task <TResult> GetResultInternalAsync <TResult>(FeignClientHttpRequest request, HttpResponseMessage responseMessage) { await EnsureSuccessAsync(request, responseMessage) #if CONFIGUREAWAIT_FALSE .ConfigureAwait(false) #endif ; var specialResult = await SpecialResults.GetSpecialResultAsync <TResult>(responseMessage) #if CONFIGUREAWAIT_FALSE .ConfigureAwait(false) #endif ; if (specialResult.IsSpecialResult) { return(specialResult.Result); } if (responseMessage.Content.Headers.ContentType == null && responseMessage.Content.Headers.ContentLength == 0) { return(default(TResult)); } 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")); } using (var stream = await responseMessage.Content.ReadAsStreamAsync() #if CONFIGUREAWAIT_FALSE .ConfigureAwait(false) #endif ) { if (stream.CanSeek) { return(GetResultInternal <TResult>(mediaTypeFormatter, stream, responseMessage.Content.Headers.ContentType, request.Method.ResultType)); } using (Stream seekStream = new MemoryStream()) { await stream.CopyToAsync(seekStream) #if CONFIGUREAWAIT_FALSE .ConfigureAwait(false) #endif ; seekStream.Position = 0; return(GetResultInternal <TResult>(mediaTypeFormatter, seekStream, responseMessage.Content.Headers.ContentType, request.Method.ResultType)); } } }
internal ServiceManagementHttpClient(HttpMessageHandler handler, IMediaTypeFormatter formatter, string subscriptionId) : base(handler) { if (formatter == null) { throw new ArgumentNullException(nameof(formatter)); } if (string.IsNullOrEmpty(subscriptionId)) { throw new ArgumentNullException(nameof(subscriptionId)); } _formatter = formatter; _subscriptionId = subscriptionId; }
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 ); }
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))); }
/// <summary> /// Sets a global formatter for its supported types. /// </summary> /// <param name="formatter">The media type formatter.</param> /// <exception cref="ArgumentException">If media type parameters are provided.</exception> public void Set(IMediaTypeFormatter formatter) { if (formatter == null) { throw new ArgumentNullException("formatter"); } Type formatterType = formatter.GetType(); var supportedMediaTypes = formatterType.GetCustomAttributes<SupportedMediaTypeAttribute>(false).ToList(); if (supportedMediaTypes.Count == 0) { throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, Resources.Global.MissingSupportedMediaTypeForFormatter, formatterType.Name), "formatter"); } foreach (SupportedMediaTypeAttribute supportedMediaType in supportedMediaTypes) { Set(supportedMediaType.MediaType, formatter); } }
public static void AddHandlerFormatter(IServiceContextHandler handler, string mediaType, IMediaTypeFormatter formatter) { handlerFormatters.AddOrUpdate(handler, handlerToAdd => AddHandlerFactory(mediaType, formatter), (handlerToUpdate, formattersToUpdate) => UpdateHandlerFactory(mediaType, formatter, formattersToUpdate)); }
public static void SetFormatter(string mediaType, IMediaTypeFormatter formatter) { formatters.AddOrUpdate(mediaType, type => formatter, (type, previousFormatter) => formatter); }
private static void InitializeFormatter(IMediaTypeFormatter formatter, ConcurrentDictionary<string, IMediaTypeFormatter> defaultFormatters) { var mediaTypes = formatter.GetType().GetTypeInfo().GetCustomAttributes<SupportedMediaTypeAttribute>(false); foreach (SupportedMediaTypeAttribute mediaType in mediaTypes) { defaultFormatters.TryAdd(mediaType.MediaType, formatter); mediaTypePriorities.TryAdd(mediaType.MediaType, mediaType.Priority); } }
private static Dictionary<string, IMediaTypeFormatter> UpdateHandlerFactory(string mediaType, IMediaTypeFormatter formatter, Dictionary<string, IMediaTypeFormatter> formattersToUpdate) { lock (updateSyncRoot) { formattersToUpdate[mediaType] = formatter; return formattersToUpdate; } }
/// <summary> /// Sets a global formatter for the provided media type. /// </summary> /// <param name="mediaType">The media type.</param> /// <param name="formatter">The media type formatter.</param> /// <exception cref="ArgumentException">If media type parameters are provided.</exception> public void Set(string mediaType, IMediaTypeFormatter formatter) { if (formatter == null) { throw new ArgumentNullException("formatter"); } if (String.IsNullOrEmpty(mediaType)) { throw new ArgumentNullException("mediaType"); } if (mediaType.IndexOf(';') >= 0 || mediaType.IndexOf(',') >= 0) { throw new ArgumentException(Resources.Global.DisallowedMediaTypeParameters, "mediaType"); } MediaTypeFormatterRegistry.SetFormatter(mediaType.Trim(), formatter); }
/// <summary> /// Removes the provided media type formatter. /// </summary> /// <param name="formatter">The media formatter.</param> /// <returns>true if the media type formatter was removed; false otherwise.</returns> public bool Remove(IMediaTypeFormatter formatter) { return MediaTypeFormatterRegistry.RemoveFormatter(formatter); }
private static Dictionary<string, IMediaTypeFormatter> AddHandlerFactory(string mediaType, IMediaTypeFormatter formatter) { lock (addSyncRoot) { var formattersToAdd = new Dictionary<string, IMediaTypeFormatter>(StringComparer.OrdinalIgnoreCase) { { mediaType, formatter } }; return formattersToAdd; } }