예제 #1
0
        /// <summary>
        /// Deserializes the response content using a specified deserializer
        /// </summary>
        /// <typeparam name="T">The type to deserialize to</typeparam>
        /// <param name="request">The ISolidHttpRequest</param>
        /// <param name="deserialize">The deserialization method</param>
        /// <returns>Task of type T</returns>
        public static async Task <T> As <T>(this ISolidHttpRequest request, Func <HttpContent, Task <T> > deserialize)
        {
            var content = await request.GetContentAsync();

            if (content == null)
            {
                return(default(T));                 // should we maybe throw an exception if there is no content?
            }
            if (request.BaseRequest.Properties.ContainsKey(IgnoreSerializationErrorKey))
            {
                return(await SafeDeserializeAsync(() => deserialize(content)));
            }
            return(await deserialize(content));
        }
예제 #2
0
        /// <summary>
        /// Deserializes the response content
        /// </summary>
        /// <typeparam name="T">The type to deserialize to</typeparam>
        /// <param name="request">The ISolidHttpRequest</param>
        /// <returns>Task of type T</returns>
        public static async Task <T> As <T>(this ISolidHttpRequest request)
        {
            var content = await request.GetContentAsync();

            if (content == null)
            {
                return(default(T));                 // should we maybe throw an exception if there is no content?
            }
            var mime = content?.Headers?.ContentType?.MediaType;

            var deserializer = request.Client.Deserializers.FirstOrDefault(d => d.CanDeserialize(mime));

            if (deserializer == null)
            {
                throw new InvalidOperationException($"Cannot deserialize {mime} response as {typeof(T).FullName}");
            }
            if (request.BaseRequest.Properties.ContainsKey(IgnoreSerializationErrorKey))
            {
                return(await SafeDeserializeAsync(() => deserializer.DeserializeAsync <T>(content)));
            }
            return(await deserializer.DeserializeAsync <T>(content));
        }