示例#1
0
        async Task <string> ManageBackendResponse(string endpoint, HttpResponseMessage response)
        {
            if (response == null)
            {
                throw new Exception("Null response received");
            }

            var task         = response.Content?.ReadAsStringAsync();
            var responseText = await(task ?? Task.FromResult <string>(null));

#if DEBUG
            System.Diagnostics.Debug.WriteLine($"{endpoint}");
            System.Diagnostics.Debug.WriteLine($"Resonse Status: {response.StatusCode}");
            System.Diagnostics.Debug.WriteLine($"Backend Response: {responseText}");
            System.Diagnostics.Debug.WriteLine($"Auth headers: {GetBearer()}");
#endif
            if (response.IsSuccessStatusCode || response.StatusCode == HttpStatusCode.NoContent || response.StatusCode == HttpStatusCode.NotModified)
            {
                return(responseText);
            }
            else
            {
                var ex = await HttpConnectorException.Create(response.RequestMessage, response.RequestMessage.Method, response)
                         .ConfigureAwait(false);

                throw ex;
            }
        }
示例#2
0
        async Task <TOutput> ManageBackendResponse <TOutput>(string endpoint, HttpResponseMessage response)
        {
            if (response == null)
            {
                throw new Exception("Null response received");
            }

            var task         = response.Content?.ReadAsStringAsync();
            var responseText = await(task ?? Task.FromResult <string>(null));

#if DEBUG
            System.Diagnostics.Debug.WriteLine($"{endpoint}");
            System.Diagnostics.Debug.WriteLine($"Auth headers: {GetBearer()}");
            System.Diagnostics.Debug.WriteLine($"Resonse Status: {response.StatusCode}");
            System.Diagnostics.Debug.WriteLine($"Backend Response: {responseText}");
#endif
            if (response.IsSuccessStatusCode || response.StatusCode == HttpStatusCode.NoContent || response.StatusCode == HttpStatusCode.NotModified)
            {
                if (typeof(TOutput) == typeof(string))
                {
                    return((TOutput)Convert.ChangeType(responseText, typeof(string)));
                }
                var result = JsonConvert.DeserializeObject <TOutput>(responseText, new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.Auto
                });
                return(result);
            }
            else if (response.StatusCode == HttpStatusCode.NotModified)
            {
                return(default(TOutput));
            }
            else
            {
                var ex = await HttpConnectorException.Create(response.RequestMessage, response.RequestMessage.Method, response)
                         .ConfigureAwait(false);

                throw ex;
            }
        }