internal static async Task <ProcessEntity <T> > ProcessResponse <T>(this HttpResponseMessage response)
        {
            if (null == response)
            {
                throw new ArgumentNullException("response");
            }

            if (!response.IsSuccessStatusCode)
            {
                ZsErrorJson errorResponse = null;
                try
                {
                    var rawErrorResponse = await response.Content.ReadAsStringAsync();

                    errorResponse = JsonConvert.DeserializeObject <ZsErrorJson>(rawErrorResponse);
                }
                catch (Exception exception)
                {
                    return(new ProcessEntity <T> {
                        Error = new InvalidOperationException("API call did not completed successfully or response parse error occurred", exception)
                    });
                }

                if (null == errorResponse || string.IsNullOrWhiteSpace(errorResponse.Message))
                {
                    return(new ProcessEntity <T> {
                        Error = new InvalidOperationException("API call did not completed successfully or response parse error occurred")
                    });
                }

                return(new ProcessEntity <T> {
                    Error = new InvalidOperationException(errorResponse.Message)
                });
            }
            else
            {
                if (typeof(T) == (typeof(bool)))
                {
                    return(new ProcessEntity <T> {
                        Data = (T)(object)response.IsSuccessStatusCode
                    });
                }

                try
                {
                    var rawResponseContent = await response.Content.ReadAsStringAsync();

                    return(new ProcessEntity <T> {
                        Data = JsonConvert.DeserializeObject <T>(rawResponseContent)
                    });
                }
                catch (Exception exception)
                {
                    return(new ProcessEntity <T> {
                        Error = new InvalidOperationException("API call did not completed successfully or response parse error occurred", exception)
                    });
                }
            }
        }
Exemple #2
0
        public async Task <Stream> GetPdfAsync(string apiBaseUrl, string authToken, string organizationId, string id)
        {
            apiBaseUrl.CheckConfigApiBaseUrl();
            authToken.CheckConfigAuthToken();
            organizationId.CheckConfigOrganizationId();

            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException("id");
            }

            using (var httpClient = new HttpClient())
            {
                httpClient.Configure(apiBaseUrl, organizationId, authToken, isPdf: true);
                var response = await httpClient.GetAsync(string.Format(CultureInfo.InvariantCulture, ApiResources.ZsGetInvoiceAsPdf, id));

                if (null == response)
                {
                    throw new ArgumentNullException("response");
                }

                if (!response.IsSuccessStatusCode)
                {
                    ZsErrorJson errorResponse = null;
                    try
                    {
                        var rawErrorResponse = await response.Content.ReadAsStringAsync();

                        errorResponse = JsonConvert.DeserializeObject <ZsErrorJson>(rawErrorResponse);
                    }
                    catch (Exception exception)
                    {
                        throw new InvalidOperationException("API call did not completed successfully or response parse error occurred", exception);
                    }

                    if (null == errorResponse || string.IsNullOrWhiteSpace(errorResponse.Message))
                    {
                        throw new InvalidOperationException("API call did not completed successfully or response parse error occurred");
                    }

                    throw new InvalidOperationException(errorResponse.Message);
                }
                else
                {
                    try
                    {
                        return(await response.Content.ReadAsStreamAsync());
                    }
                    catch (Exception exception)
                    {
                        throw new InvalidOperationException("API call did not completed successfully or response parse error occurred", exception);
                    }
                }
            }
        }