Пример #1
0
        private static async Task <CommonResponse <TOut> > CreateResponse <TOut>(HttpResponseMessage response)
        {
            IEnumerable <string> asyncOpHeaderList;
            var hasAsyncHeader = response.Headers.TryGetValues("Azure-AsyncOperation", out asyncOpHeaderList);
            var isAsyncOp      = hasAsyncHeader && asyncOpHeaderList.Any() && !string.IsNullOrWhiteSpace(asyncOpHeaderList.First());

            var responseString = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK &&
                response.StatusCode != HttpStatusCode.Created &&
                response.StatusCode != HttpStatusCode.Accepted)
            {
                var azureErrorResponse = JsonConvert.DeserializeObject <AzureErrorResponse>(responseString);
                var result             = CommonResponseHelper.CreateFailResponse <TOut>(azureErrorResponse.Error);
                return(result);
            }
            else
            {
                var result = JsonConvert.DeserializeObject <TOut>(responseString);
                if (isAsyncOp)
                {
                    return(CommonResponseHelper.CreateSuccessAsyncResponse(result, response.StatusCode, asyncOpHeaderList.First()));
                }
                else
                {
                    return(CommonResponseHelper.CreateSuccessResponse(result, response.StatusCode));
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Calls api via GET method.
        /// </summary>
        /// <typeparam name="TOut">Expected result type.</typeparam>
        /// <param name="endpointUrl">Endpoint url.</param>
        /// <param name="accessToken">Access token.</param>
        /// <returns>Response result.</returns>
        public async Task <CommonResponse <IEnumerable <TOut> > > CallGetListAsync <TOut>(string endpointUrl, string accessToken)
        {
            this.PrepareHttpClient(accessToken);

            var response = await this.httpClient.GetAsync(endpointUrl);

            var responseString = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                var errorInfo = JsonConvert.DeserializeObject <ErrorInfo>(responseString);
                var result    = CommonResponseHelper.CreateFailResponse <IEnumerable <TOut> >(errorInfo);
                return(result);
            }
            else
            {
                var result = JsonConvert.DeserializeObject <AzureResponse <TOut> >(responseString);
                return(CommonResponseHelper.CreateSuccessResponse(result.Items));
            }
        }