public static async Task <TResponse> Put <TResponse>(this IOpenApiClient client, string url, object data, CancellationToken cancellationToken)
        {
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }

            using var httpClient = client.CreateHttpClient();
            using var request    = new HttpRequestMessage(HttpMethod.Put, url)
                  {
                      Content = new ObjectContent <object>(data, Json)
                  };
            using var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);

            await AssertResponse(response).ConfigureAwait(false);

            return(await response.Content.ReadAsAsync <TResponse>(cancellationToken).ConfigureAwait(false));
        }
Esempio n. 2
0
    public static async Task <bool> Delete(this IOpenApiClient client, string url, CancellationToken cancellationToken)
    {
        if (url == null)
        {
            throw new ArgumentNullException(nameof(url));
        }

        using var httpClient = client.CreateHttpClient();
        using var response   = await httpClient.DeleteAsync(url, cancellationToken).ConfigureAwait(false);

        if (response.StatusCode == HttpStatusCode.NotFound)
        {
            return(false);
        }

        await AssertResponse(response).ConfigureAwait(false);

        return(true);
    }
        public static async Task <TResponse?> Get <TResponse>(this IOpenApiClient client, string url, CancellationToken cancellationToken) where TResponse : class
        {
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }

            using var httpClient = client.CreateHttpClient();
            using var response   = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(null);
            }

            await AssertResponse(response).ConfigureAwait(false);

            return(await response.Content.ReadAsAsync <TResponse>(cancellationToken).ConfigureAwait(false));
        }