public void ThrowError(bool throwError, HttpStatusCode status) { // execute IResponse response = this.ConstructResponse("", throwApiError: throwError, status: status); response.AsString(); }
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()); }
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()); }
public void OnSuccess_NoErrorThrown(bool throwError, HttpStatusCode status) { // arrange IResponse response = this.ConstructResponse("", throwApiError: throwError, status: status); // act response.AsString().Wait(); }
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."); }
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."); }
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()), }); }
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); }
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\"}")); }
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 + '"')); }
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()), }); }
/// <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)); }
/// <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)); }
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); }
/// <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)}"); }