|
| 1 | +// Copyright (c) .NET Foundation and Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +namespace RestSharp; |
| 17 | + |
| 18 | +public static partial class RestClientExtensions { |
| 19 | + /// <summary> |
| 20 | + /// Executes a DELETE-style request asynchronously, authenticating if needed |
| 21 | + /// </summary> |
| 22 | + /// <param name="client"></param> |
| 23 | + /// <param name="request">Request to be executed</param> |
| 24 | + /// <param name="cancellationToken">Cancellation token</param> |
| 25 | + public static Task<RestResponse> ExecuteDeleteAsync(this IRestClient client, RestRequest request, CancellationToken cancellationToken = default) |
| 26 | + => client.ExecuteAsync(request, Method.Delete, cancellationToken); |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Executes a DELETE-style synchronously, authenticating if needed |
| 30 | + /// </summary> |
| 31 | + /// <param name="client"></param> |
| 32 | + /// <param name="request">Request to be executed</param> |
| 33 | + public static RestResponse ExecuteDelete(this IRestClient client, RestRequest request) |
| 34 | + => AsyncHelpers.RunSync(() => client.ExecuteAsync(request, Method.Delete)); |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Executes a DELETE-style request asynchronously, authenticating if needed. |
| 38 | + /// The response content then gets deserialized to T. |
| 39 | + /// </summary> |
| 40 | + /// <typeparam name="T">Target deserialization type</typeparam> |
| 41 | + /// <param name="client"></param> |
| 42 | + /// <param name="request">Request to be executed</param> |
| 43 | + /// <param name="cancellationToken">The cancellation token</param> |
| 44 | + /// <returns>Deserialized response content</returns> |
| 45 | + public static Task<RestResponse<T>> ExecuteDeleteAsync<T>( |
| 46 | + this IRestClient client, |
| 47 | + RestRequest request, |
| 48 | + CancellationToken cancellationToken = default |
| 49 | + ) |
| 50 | + => client.ExecuteAsync<T>(request, Method.Delete, cancellationToken); |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Executes a DELETE-style request synchronously, authenticating if needed. |
| 54 | + /// The response content then gets deserialized to T. |
| 55 | + /// </summary> |
| 56 | + /// <typeparam name="T">Target deserialization type</typeparam> |
| 57 | + /// <param name="client"></param> |
| 58 | + /// <param name="request">Request to be executed</param> |
| 59 | + /// <returns>Deserialized response content</returns> |
| 60 | + public static RestResponse<T> ExecuteDelete<T>(this IRestClient client, RestRequest request) |
| 61 | + => AsyncHelpers.RunSync(() => client.ExecuteAsync<T>(request, Method.Delete)); |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Execute the request using DELETE HTTP method. Exception will be thrown if the request does not succeed. |
| 65 | + /// The response data is deserialized to the Data property of the returned response object. |
| 66 | + /// </summary> |
| 67 | + /// <param name="client">RestClient instance</param> |
| 68 | + /// <param name="request">The request</param> |
| 69 | + /// <param name="cancellationToken">Cancellation token</param> |
| 70 | + /// <typeparam name="T">Expected result type</typeparam> |
| 71 | + /// <returns></returns> |
| 72 | + public static async Task<T?> DeleteAsync<T>(this IRestClient client, RestRequest request, CancellationToken cancellationToken = default) { |
| 73 | + var response = await client.ExecuteAsync<T>(request, Method.Delete, cancellationToken).ConfigureAwait(false); |
| 74 | + return response.ThrowIfError().Data; |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Execute the request using DELETE HTTP method. Exception will be thrown if the request does not succeed. |
| 79 | + /// The response data is deserialized to the Data property of the returned response object. |
| 80 | + /// </summary> |
| 81 | + /// <param name="client">RestClient instance</param> |
| 82 | + /// <param name="request">The request</param> |
| 83 | + /// <typeparam name="T">Expected result type</typeparam> |
| 84 | + /// <returns></returns> |
| 85 | + public static T? Delete<T>(this IRestClient client, RestRequest request) => AsyncHelpers.RunSync(() => client.DeleteAsync<T>(request)); |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Execute the request using DELETE HTTP method. Exception will be thrown if the request does not succeed. |
| 89 | + /// </summary> |
| 90 | + /// <param name="client">RestClient instance</param> |
| 91 | + /// <param name="request">The request</param> |
| 92 | + /// <param name="cancellationToken">Cancellation token</param> |
| 93 | + /// <returns></returns> |
| 94 | + public static async Task<RestResponse> DeleteAsync(this IRestClient client, RestRequest request, CancellationToken cancellationToken = default) { |
| 95 | + var response = await client.ExecuteAsync(request, Method.Delete, cancellationToken).ConfigureAwait(false); |
| 96 | + return response.ThrowIfError(); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Execute the request using DELETE HTTP method. Exception will be thrown if the request does not succeed. |
| 101 | + /// </summary> |
| 102 | + /// <param name="client">RestClient instance</param> |
| 103 | + /// <param name="request">The request</param> |
| 104 | + /// <returns></returns> |
| 105 | + public static RestResponse Delete(this IRestClient client, RestRequest request) => AsyncHelpers.RunSync(() => client.DeleteAsync(request)); |
| 106 | +} |
0 commit comments