public static async Task <TResult> PostAsync <TRequest, TResult>(this HttpClient httpClient, string url, TRequest request) { var requestJson = SystemTextJsonSerializer.Serialize(request); var requestContent = GetJsonStringContent(requestJson); HttpResponseMessage httpResponseMessage; try { httpResponseMessage = await httpClient.PostAsync(url, requestContent).ConfigureAwait(false); } catch (HttpRequestException e) { throw BankIdApiException.Unknown(e); } await BankIdApiErrorHandler.EnsureSuccessAsync(httpResponseMessage).ConfigureAwait(false); var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false); var deserializedContent = await SystemTextJsonSerializer.DeserializeAsync <TResult>(contentStream).ConfigureAwait(false); if (deserializedContent == null) { throw new Exception("Could not deserialize JSON response"); } return(deserializedContent); }
private static async Task <HttpResponseMessage> GetHttpResponseAsync <TRequest>(string url, TRequest request, Func <string, HttpContent, Task <HttpResponseMessage> > httpRequest, IJsonSerializer jsonSerializer) { var requestJson = jsonSerializer.Serialize(request); var requestContent = GetJsonStringContent(requestJson); var httpResponseMessage = await httpRequest(CleanUrl(url), requestContent).ConfigureAwait(false); await BankIdApiErrorHandler.EnsureSuccessAsync(httpResponseMessage, jsonSerializer).ConfigureAwait(false); return(httpResponseMessage); }
public static async Task <TResult> PostAsync <TRequest, TResult>(this HttpClient httpClient, string url, TRequest request) { var requestJson = SystemRuntimeJsonSerializer.Serialize(request); var requestContent = GetJsonStringContent(requestJson); var httpResponseMessage = await httpClient.PostAsync(url, requestContent).ConfigureAwait(false); await BankIdApiErrorHandler.EnsureSuccessAsync(httpResponseMessage).ConfigureAwait(false); var content = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false); return(SystemRuntimeJsonSerializer.Deserialize <TResult>(content)); }