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 <T> ReadJsonAsync <T>(this HttpRequest request, IAppyJsonSerializer serializer) { using var reader = new StreamReader(request.Body); var body = await reader.ReadToEndAsync(); return(serializer.Deserialize <T>(body)); }
internal static TResult DeserializeAndThrow <TResult>(this IAppyJsonSerializer serializer, string output) { try { return(serializer.Deserialize <TResult>(output)); } catch (Exception ex) { throw new OnePasswordToolException("1Password Tool failed deserialization", ex); } }