Ejemplo n.º 1
0
        /// <summary>
        /// Converts the specified <see cref="IRestResponse"/> into a strongly typed REST API response.
        /// </summary>
        /// <typeparam name="T">The type to cast the response to. See remarks.</typeparam>
        /// <param name="this">The <see cref="IRestResponse"/> which will be strongly typed.</param>
        /// <returns>
        /// A strongly typed REST API response.
        /// </returns>
        /// <remarks>
        /// This method will attempt to deserialize the response data using either <see cref="DefaultXmlSerialization"/>
        /// or <see cref="DefaultJsonSerialization"/>. The type used is determined by <see cref="GetDataFormat(IRestResponse)"/>.
        /// </remarks>
        public static IRestResponse <T> ToIRestResponse <T>(this IRestResponse @this)
        {
            IRestResponse <T> result = null;

            if (@this != null)
            {
                result = @this.toAsyncResponse <T>();
                var format = @this.GetDataFormat();

                if (@this.Content != null)
                {
                    if (format == DataFormat.Json)
                    {
                        result.Data = JsonDeserializer.Deserialize <T>(@this.Content);
                    }
                    else if (format == DataFormat.Xml)
                    {
                        result.Data = XmlDeserializer.Deserialize <T>(@this.Content);
                    }
                }
            }

            return(result);
        }