/// <inheritdoc /> public bool CanConvertFromHttpContent(Type typeToConvertTo, HttpContent httpContent) { // Check if the return-type can be assigned if (typeToConvertTo == typeof(object) || !typeToConvertTo.IsAssignableFrom(typeof(IEnumerable <KeyValuePair <string, string> >))) { return(false); } return(httpContent.GetContentType() == MediaTypes.WwwFormUrlEncoded.EnumValueOf()); }
/// <summary> /// Check <see cref="HttpContent.Headers"/> /// for <see cref="HeaderNames.ContentType"/> /// and map accordingly. /// </summary> public static async Task <TResponse> DeserializeAsync <TResponse>(this HttpContent httpContent, CancellationToken cancellationToken = default) where TResponse : class { _ = httpContent ?? throw new ArgumentNullException(nameof(httpContent)); //Get content type var contentType = httpContent.GetContentType(); if (contentType is null) { throw new KeyNotFoundException($"{HeaderNames.ContentType} header missing from the response."); } //Check content type return(contentType.MediaType switch { MediaTypeNames.Application.Json => await httpContent.FromJsonAsync <TResponse>(cancellationToken), MediaTypeNamesX.Application.ProblemJson => await httpContent.FromJsonAsync <TResponse>(cancellationToken), MediaTypeNames.Application.Xml => await httpContent.FromXmlAsync <TResponse>(cancellationToken), MediaTypeNamesX.Application.ProblemXml => await httpContent.FromXmlAsync <TResponse>(cancellationToken), _ => throw new NotSupportedException($"`{contentType.MediaType}` is not supported for automatic deserialization. Please use a more specific method."), });
/// <summary> /// Extension method reading the httpContent to a Typed object, depending on the returned content-type /// Currently we support: /// Json objects which are annotated with the DataContract/DataMember attributes /// </summary> /// <param name="httpContent">HttpContent</param> /// <param name="resultType">The Type to read into</param> /// <param name="httpStatusCode">HttpStatusCode</param> /// <param name="cancellationToken">CancellationToken</param> /// <returns>the deserialized object of type T</returns> public static async Task <object> GetAsAsync(this HttpContent httpContent, Type resultType, HttpStatusCode httpStatusCode, CancellationToken cancellationToken = default(CancellationToken)) { // Quick exit when the requested type is from HttpContent if (typeof(HttpContent).IsAssignableFrom(resultType)) { return(httpContent); } // Quick exit with empty value if the status code has a NoContent if (httpStatusCode == HttpStatusCode.NoContent) { if (resultType.GetTypeInfo().IsValueType) { return(Activator.CreateInstance(resultType)); } return(null); } var httpBehaviour = HttpBehaviour.Current; var converter = httpBehaviour.HttpContentConverters.OrderBy(x => x.Order).FirstOrDefault(x => x.CanConvertFromHttpContent(resultType, httpContent)); if (converter != null) { return(await converter.ConvertFromHttpContentAsync(resultType, httpContent, cancellationToken).ConfigureAwait(false)); } // For everything that comes here, a fitting converter should be written, or the ValidateResponseContentType can be set to false var contentType = httpContent.GetContentType(); Log.Error().WriteLine($"Unsupported result type {resultType} & {contentType} combination."); // Only write when the result is something readable if (ReadableContentTypes.Contains(contentType)) { Log.Error().WriteLine("Unprocessable result: {0}", await httpContent.ReadAsStringAsync().ConfigureAwait(false)); } return(null); }
/// <summary> /// This checks if the HttpContent can be converted to a Bitmap and is assignable to the specified Type /// </summary> /// <param name="typeToConvertTo">This should be something we can assign Bitmap to</param> /// <param name="httpContent">HttpContent to process</param> /// <returns>true if it can convert</returns> public bool CanConvertFromHttpContent(Type typeToConvertTo, HttpContent httpContent) { if ((typeToConvertTo == typeof(object)) || !typeToConvertTo.IsAssignableFrom(typeof(Bitmap))) { return(false); } var httpBehaviour = HttpBehaviour.Current; var configuration = httpBehaviour.GetConfig <SvgConfiguration>(); return(!httpBehaviour.ValidateResponseContentType || configuration.SupportedContentTypes.Contains(httpContent.GetContentType())); }
/// <inheritdoc /> public bool CanConvertFromHttpContent(Type typeToConvertTo, HttpContent httpContent) { var httpBehaviour = HttpBehaviour.Current; if (httpBehaviour.JsonSerializer == null || !httpBehaviour.JsonSerializer.CanDeserializeFrom(typeToConvertTo)) { return(false); } if (!typeToConvertTo.GetTypeInfo().IsClass&& !typeToConvertTo.GetTypeInfo().IsInterface) { return(false); } return(!httpBehaviour.ValidateResponseContentType || SupportedContentTypes.Contains(httpContent.GetContentType())); }
/// <inheritdoc /> public bool CanConvertFromHttpContent(Type typeToConvertTo, HttpContent httpContent) { if (typeToConvertTo != typeof(string)) { return(false); } var httpBehaviour = HttpBehaviour.Current; var configuration = httpBehaviour.GetConfig <StringConfiguration>(); // Set ValidateResponseContentType to false to "catch" all return(!httpBehaviour.ValidateResponseContentType || configuration.SupportedContentTypes.Contains(httpContent.GetContentType())); }