public async Task <TResponse> Post <TResponse>(IPostApiRequest request) { var result = await PostWithResponseCode <TResponse>(request); if (IsNot200RangeResponseCode(result.StatusCode)) { throw new HttpRequestContentException($"Response status code does not indicate success: {(int)result.StatusCode} ({result.StatusCode})", result.StatusCode, result.ErrorContent); } return(result.Body); }
public async Task Post <TData>(IPostApiRequest <TData> request) { await AddAuthenticationHeader(); AddVersionHeader(request.Version); var stringContent = request.Data != null ? new StringContent(JsonConvert.SerializeObject(request.Data), Encoding.UTF8, "application/json") : null; var response = await HttpClient.PostAsync(request.PostUrl, stringContent) .ConfigureAwait(false); await response.EnsureSuccessStatusCodeIncludeContentInException(); }
public async Task <TResponse> Create <TResponse>(IPostApiRequest request) { var accessToken = await GetAccessTokenAsync(); using (var client = new HttpClient())//not unit testable using directly { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var jsonRequest = JsonConvert.SerializeObject(request); var stringContent = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json"); var response = await client.PostAsync(request.CreateUrl, stringContent).ConfigureAwait(false); response.EnsureSuccessStatusCode(); var jsonResponse = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return(JsonConvert.DeserializeObject <TResponse>(jsonResponse)); } }
public async Task Then_PostRequestIsSent(CreateApprenticeFeedbackCommand command, string errorContent) { var response = new ApiResponse <CreateApprenticeFeedbackResponse>(new CreateApprenticeFeedbackResponse(), HttpStatusCode.Created, errorContent); IPostApiRequest submittedRequest = null; _mockApiClient.Setup(c => c.PostWithResponseCode <CreateApprenticeFeedbackResponse>(It.IsAny <CreateApprenticeFeedbackRequest>())) .Callback <IPostApiRequest>(x => submittedRequest = x) .ReturnsAsync(response); await _handler.Handle(command, CancellationToken.None); _mockApiClient.Verify(c => c.PostWithResponseCode <object>(It.IsAny <IPostApiRequest>())); (submittedRequest.Data).Should().BeEquivalentTo(new { command.ApprenticeFeedbackTargetId, command.OverallRating, command.AllowContact, command.FeedbackAttributes }); }
public Task <ApiResponse <TResponse> > PostWithResponseCode <TResponse>(IPostApiRequest request) { return(_apiClient.PostWithResponseCode <TResponse>(request)); }
public Task Post <TData>(IPostApiRequest <TData> request) { return(_apiClient.Post(request)); }
public Task <TResponse> Post <TResponse>(IPostApiRequest request) { return(_apiClient.Post <TResponse>(request)); }
public async Task <ApiResponse <TResponse> > PostWithResponseCode <TResponse>(IPostApiRequest request) { var stringContent = request.Data != null ? new StringContent(JsonConvert.SerializeObject(request.Data), Encoding.UTF8, "application/json") : null; var requestMessage = new HttpRequestMessage(HttpMethod.Post, request.PostUrl); requestMessage.AddVersion(request.Version); requestMessage.Content = stringContent; await AddAuthenticationHeader(requestMessage); var response = await HttpClient.SendAsync(requestMessage).ConfigureAwait(false); var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var errorContent = ""; var responseBody = (TResponse) default; if (IsNot200RangeResponseCode(response.StatusCode)) { errorContent = json; HandleException(response, json); } else { responseBody = JsonConvert.DeserializeObject <TResponse>(json); } var postWithResponseCode = new ApiResponse <TResponse>(responseBody, response.StatusCode, errorContent); return(postWithResponseCode); }
public Task Post <TData>(IPostApiRequest <TData> request) { throw new NotImplementedException(); }
// Methods Now Obsolete. public Task <TResponse> Post <TResponse>(IPostApiRequest request) { throw new NotImplementedException(); }
public Task <ApiResponse <TResponse> > PostWithResponseCode <TResponse>(IPostApiRequest request) { throw new System.NotImplementedException(); }
public async Task <ApiResponse <TResponse> > PostWithResponseCode <TResponse>(IPostApiRequest request) { await AddAuthenticationHeader(); AddVersionHeader(request.Version); var stringContent = request.Data != null ? new StringContent(JsonConvert.SerializeObject(request.Data), Encoding.UTF8, "application/json") : null; var response = await HttpClient.PostAsync(request.PostUrl, stringContent) .ConfigureAwait(false); await response.EnsureSuccessStatusCodeIncludeContentInException(); var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return(new ApiResponse <TResponse>(JsonConvert.DeserializeObject <TResponse>(json), response.StatusCode)); }
public async Task <TResponse> Post <TResponse>(IPostApiRequest request) { var result = await PostWithResponseCode <TResponse>(request); return(result.Body); }