コード例 #1
0
        public void ThrowError(bool throwError, HttpStatusCode status)
        {
            // execute
            IResponse response = this.ConstructResponse("", throwApiError: throwError, status: status);

            response.AsString();
        }
コード例 #2
0
        public void Task_Async_FaultHandled(bool throwError, Type exceptionType)
        {
            // arrange
            IResponse response = this.ConstructResponseFromTask(() => { throw (Exception)Activator.CreateInstance(exceptionType); });

            // act
            Assert.ThrowsAsync <NotSupportedException>(async() => await response.AsString());
        }
コード例 #3
0
        public void OnError_ErrorThrown(bool throwError, HttpStatusCode status)
        {
            // arrange
            IResponse response = this.ConstructResponse("", throwApiError: throwError, status: status);

            // act
            Assert.ThrowsAsync <ApiException>(async() => await response.AsString());
        }
コード例 #4
0
        public void OnSuccess_NoErrorThrown(bool throwError, HttpStatusCode status)
        {
            // arrange
            IResponse response = this.ConstructResponse("", throwApiError: throwError, status: status);

            // act
            response.AsString().Wait();
        }
コード例 #5
0
        public void AsString_MultipleReads(string content)
        {
            // arrange
            IResponse response = this.ConstructResponse(content);

            // act
            string actualA = response
                             .AsString()
                             .VerifyTaskResult();
            string actualB = response
                             .AsString()
                             .VerifyTaskResult();

            // assert
            Assert.That(actualA, Is.EqualTo('"' + content + '"'), "The content is not equal to the input.");
            Assert.That(actualA, Is.EqualTo(actualB), "The second read returned a different result.");
        }
コード例 #6
0
        public async Task AsString_MultipleReads(string content)
        {
            // arrange
            IResponse response = this.ConstructResponse(content);

            // act
            string actualA = await response
                             .AsString()
                             .VerifyTaskResultAsync();

            string actualB = await response
                             .AsString()
                             .VerifyTaskResultAsync();

            // assert
            Assert.That(actualA, Is.EqualTo($"\"{content}\""), "The content is not equal to the input.");
            Assert.That(actualA, Is.EqualTo(actualB), "The second read returned a different result.");
        }
コード例 #7
0
        public static async Task <ApiResponse <T> > AsApiResponse <T>(this IResponse response)
        {
            string responseString = await response.AsString();

            return(new ApiResponse <T>()
            {
                Data = JsonConvert.DeserializeObject <T>(responseString),
                StatusCode = (int)response.Status,
                Headers = response.Message.Headers.ToDictionary(x => x.Key, x => x.Value.FirstOrDefault()),
            });
        }
コード例 #8
0
        protected async Task <string> InternalRequest(AbstractModel request, string actionName)
        {
            if ((this.Profile.HttpProfile.ReqMethod != HttpProfile.REQ_GET) && (this.Profile.HttpProfile.ReqMethod != HttpProfile.REQ_POST))
            {
                throw new TencentCloudSDKException("Method only support (GET, POST)");
            }

            IResponse response = null;

            if (ClientProfile.SIGN_SHA1.Equals(this.Profile.SignMethod) ||
                ClientProfile.SIGN_SHA256.Equals(this.Profile.SignMethod))
            {
                response = await RequestV1(request, actionName);
            }
            else
            {
                response = await RequestV3(request, actionName);
            }

            if ((int)response.Status != HTTP_RSP_OK)
            {
                throw new TencentCloudSDKException(response.Status + await response.Message.Content.ReadAsStringAsync());
            }
            string strResp = null;

            try
            {
                strResp = await response.AsString();
            }
            catch (ApiException ex)
            {
                string responseText = await ex.Response.AsString();

                throw new TencentCloudSDKException($"The API responded with HTTP {ex.Response.Status}: {responseText}");
            }

            JsonResponseModel <JsonResponseErrModel> errResp = null;

            try
            {
                errResp = JsonConvert.DeserializeObject <JsonResponseModel <JsonResponseErrModel> >(strResp);
            }
            catch (JsonSerializationException e)
            {
                throw new TencentCloudSDKException(e.Message);
            }
            if (errResp.Response.Error != null)
            {
                throw new TencentCloudSDKException($"code:{errResp.Response.Error.Code} message:{errResp.Response.Error.Message} ",
                                                   errResp.Response.RequestId);
            }
            return(strResp);
        }
コード例 #9
0
        public async Task AsString_OfModel(string content)
        {
            // arrange
            IResponse response = this.ConstructResponse(new Model <string>(content));

            // act
            string actual = await response
                            .AsString()
                            .VerifyTaskResultAsync();

            // assert
            Assert.That(actual, Is.EqualTo("{\"Value\":\"stream content\"}"));
        }
コード例 #10
0
        public async Task AsString(string content)
        {
            // arrange
            IResponse response = this.ConstructResponse(content);

            // act
            string actual = await response
                            .AsString()
                            .VerifyTaskResultAsync();

            // assert
            Assert.That(actual, Is.EqualTo('"' + content + '"'));
        }
コード例 #11
0
        public static async Task <ApiResponse <T> > AsApiResponse <T>(this IRequest request)
        {
            IResponse response = await request.AsResponse().ConfigureAwait(false);

            if (response.IsSuccessStatusCode == false)
            {
                return(new ApiResponse <T>()
                {
                    StatusCode = (int)response.Status,
                    Headers = response.Message.Headers.ToDictionary(x => x.Key, x => x.Value.FirstOrDefault()),
                });
            }

            string responseString = await response.AsString();

            return(new ApiResponse <T>()
            {
                Data = JsonConvert.DeserializeObject <T>(responseString),
                StatusCode = (int)response.Status,
                Headers = response.Message.Headers.ToDictionary(x => x.Key, x => x.Value.FirstOrDefault()),
            });
        }
コード例 #12
0
ファイル: Extensions.cs プロジェクト: wushian/StrongGrid
        /// <summary>Asynchronously retrieve the response body as a <see cref="string"/>.</summary>
        /// <param name="request">The request</param>
        /// <param name="encoding">The encoding. You can leave this parameter null and the encoding will be
        /// automatically calculated based on the charset in the response. Also, UTF-8
        /// encoding will be used if the charset is absent from the response, is blank
        /// or contains an invalid value.</param>
        /// <returns>Returns the response body, or <c>null</c> if the response has no body.</returns>
        /// <exception cref="ApiException">An error occurred processing the response.</exception>
        public static async Task <string> AsString(this IRequest request, Encoding encoding)
        {
            IResponse response = await request.AsResponse().ConfigureAwait(false);

            return(await response.AsString(encoding).ConfigureAwait(false));
        }
コード例 #13
0
        /// <summary>Asynchronously retrieve the response body as a <see cref="string"/>.</summary>
        /// <returns>Returns the response body, or <c>null</c> if the response has no body.</returns>
        /// <exception cref="ApiException">An error occurred processing the response.</exception>
        public async Task <string> AsString()
        {
            IResponse response = await this.AsResponse().ConfigureAwait(false);

            return(await response.AsString().ConfigureAwait(false));
        }
コード例 #14
0
        protected async Task <string> InternalRequest(AbstractModel request, string actionName)
        {
            IResponse okRsp    = null;
            string    endpoint = this.Endpoint;

            if (!string.IsNullOrEmpty(this.Profile.HttpProfile.Endpoint))
            {
                endpoint = this.Profile.HttpProfile.Endpoint;
            }
            Dictionary <string, string> param = new Dictionary <string, string>();

            request.ToMap(param, "");
            // inplace change
            this.FormatRequestData(actionName, param);

            using (HttpConnection conn = new HttpConnection($"{this.Profile.HttpProfile.Protocol }{endpoint}", this.Profile.HttpProfile.Timeout, this.Profile.HttpProfile.WebProxy))
            {
                if ((this.Profile.HttpProfile.ReqMethod != HttpProfile.REQ_GET) && (this.Profile.HttpProfile.ReqMethod != HttpProfile.REQ_POST))
                {
                    throw new TencentCloudSDKException("Method only support (GET, POST)");
                }
                try
                {
                    if (this.Profile.HttpProfile.ReqMethod == HttpProfile.REQ_GET)
                    {
                        okRsp = await conn.GetRequest($"{this.Path}", param);
                    }
                    else if (this.Profile.HttpProfile.ReqMethod == HttpProfile.REQ_POST)
                    {
                        okRsp = await conn.PostRequest(this.Path, param);
                    }
                }
                catch (Exception ex)
                {
                    throw new TencentCloudSDKException($"The request with exception: {ex.Message }");
                }
            }

            if ((int)okRsp.Status != HTTP_RSP_OK)
            {
                throw new TencentCloudSDKException(okRsp.Status + await okRsp.Message.Content.ReadAsStringAsync());
            }
            string strResp = null;

            try
            {
                strResp = await okRsp.AsString();
            }
            catch (ApiException ex)
            {
                string responseText = await ex.Response.AsString();

                throw new TencentCloudSDKException($"The API responded with HTTP {ex.Response.Status}: {responseText}");
            }

            JsonResponseModel <JsonResponseErrModel> errResp = null;

            try
            {
                errResp = JsonConvert.DeserializeObject <JsonResponseModel <JsonResponseErrModel> >(strResp);
            }
            catch (JsonSerializationException e)
            {
                throw new TencentCloudSDKException(e.Message);
            }
            if (errResp.Response.Error != null)
            {
                throw new TencentCloudSDKException($"code:{errResp.Response.Error.Code} message:{errResp.Response.Error.Message} ",
                                                   errResp.Response.RequestId);
            }
            return(strResp);
        }
コード例 #15
0
        /// <summary>Method invoked just after the HTTP response is received. This method can modify the incoming HTTP response.</summary>
        /// <param name="response">The HTTP response.</param>
        /// <param name="httpErrorAsException">Whether HTTP error responses should be raised as exceptions.</param>
        public void OnResponse(IResponse response, bool httpErrorAsException)
        {
            var debugResponseMsg = string.Format("Response received: {0}", response.AsString().Result);

            Debug.WriteLine($"\r\n{debugResponseMsg}\r\n{new string('=', 25)}");
        }