﻿namespace System.Net.Http
{
    using Newtonsoft.Json;

    public static class HttpResponseMessageExtensions
    {
        #region Methods

        public static string TryReadErrorMessage(this HttpResponseMessage response)
        {
            try
            {
                var message = response.Content.ReadAsStringAsync().Result;
                return message;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static T Deserialize<T>(this HttpResponseMessage response) where T : class
        {
            var stream = response.Content.ReadAsStringAsync().Result;
            var item = JsonConvert.DeserializeObject<T>(stream);

            return item;
        }

        #endregion Methods
    }
}