public static async Task <ApiRequestException> FromResponseAsync(HttpResponseMessage response, Exception inner = null)
        {
            var content = await response.Content.ReadAsStringAsync();

            var exception = new ApiRequestException(response.StatusCode, inner);

            try
            {
                exception.Content = JsonConvert.DeserializeObject(content);
            }
            catch (Exception)
            {
                exception.Content = content;
            }

            return(exception);
        }
Esempio n. 2
0
        private async Task <TResponse> DeserializeAsync <TResponse>(HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                throw await ApiRequestException.FromResponseAsync(response);
            }

            try
            {
                using var reader     = new StreamReader(await response.Content.ReadAsStreamAsync());
                using var jsonReader = new JsonTextReader(reader);
                return(_serializer.Deserialize <TResponse>(jsonReader));
            }
            catch (Exception e)
            {
                throw await ApiRequestException.FromResponseAsync(response, e);
            }
        }