async Task <Response <TResponse> > PostJsonAsync <TResponse>(string url, object request, CancellationToken cancellationToken) { var content = _jsonSerializer.Serialize(request); var requestMessage = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri(_httpClient.BaseAddress, url), Content = new StringContent(content, Encoding.UTF8, "application/json") }; var rawResponse = await _httpClient.SendAsync(requestMessage, cancellationToken); var json = await rawResponse.Content.ReadAsStringAsync(); var response = _jsonSerializer.Deserialize <Response <TResponse> >(json); if (rawResponse.IsSuccessStatusCode) { return(response); } throw new OnePasswordApiClientException( statusCode: (int)rawResponse.StatusCode, response); }
public static async Task WriteJsonAsync(this HttpResponse response, IAppyJsonSerializer serializer, HttpStatusCode statusCode, object value, string contentType = null !) { var json = serializer.Serialize(value); await response.WriteJsonAsync(statusCode, json, contentType); await response.Body.FlushAsync(); }