Class ApiResource
- Namespace
- Cloudflare.NET.Core
- Assembly
- Cloudflare.NET.dll
Base class for API resource clients, providing shared functionality.
public abstract class ApiResource
- Inheritance
-
ApiResource
- Derived
- Inherited Members
Constructors
ApiResource(HttpClient, ILogger)
Initializes a new instance of the ApiResource.
protected ApiResource(HttpClient httpClient, ILogger logger)
Parameters
httpClientHttpClientThe HttpClient to use for requests.
loggerILoggerThe logger for this API resource.
Fields
HttpClient
The configured HttpClient for making API requests.
protected readonly HttpClient HttpClient
Field Value
Logger
The logger for this API resource.
protected readonly ILogger Logger
Field Value
Methods
DeleteAsync<TResult>(string, IEnumerable<KeyValuePair<string, string>>?, CancellationToken)
Sends a DELETE request with optional custom headers to the specified URI.
protected Task<TResult> DeleteAsync<TResult>(string requestUri, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
headersIEnumerable<KeyValuePair<string, string>>Optional custom headers to include in the request.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
DeleteAsync<TResult>(string, CancellationToken)
Sends a DELETE request to the specified URI.
protected Task<TResult> DeleteAsync<TResult>(string requestUri, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
GetAsync<TResult>(string, IEnumerable<KeyValuePair<string, string>>?, CancellationToken)
Sends a GET request with optional custom headers to the specified URI.
protected Task<TResult> GetAsync<TResult>(string requestUri, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
headersIEnumerable<KeyValuePair<string, string>>Optional custom headers to include in the request.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
GetAsync<TResult>(string, CancellationToken)
Sends a GET request to the specified URI.
protected Task<TResult> GetAsync<TResult>(string requestUri, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
GetCursorPaginatedAsync<TItem>(string, int?, CancellationToken)
Gets a paginated list of resources using cursor-based pagination.
protected IAsyncEnumerable<TItem> GetCursorPaginatedAsync<TItem>(string baseUri, int? perPage, CancellationToken cancellationToken)
Parameters
baseUristringThe base request URI, without any cursor or per_page parameters.
perPageint?The number of items to request per page.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TItem>
An asynchronous stream of items.
Type Parameters
TItemThe type of the item in the list.
GetCursorPaginatedAsync<TWrapper, TItem>(string, int?, Func<TWrapper, IReadOnlyList<TItem>>, IEnumerable<KeyValuePair<string, string>>?, CancellationToken)
Gets a paginated list of resources using cursor-based pagination where the item list is nested within a wrapper object, with optional custom headers. This is the core implementation.
protected IAsyncEnumerable<TItem> GetCursorPaginatedAsync<TWrapper, TItem>(string baseUri, int? perPage, Func<TWrapper, IReadOnlyList<TItem>> listExtractor, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken)
Parameters
baseUristringThe base request URI, without any cursor or per_page parameters.
perPageint?The number of items to request per page.
listExtractorFunc<TWrapper, IReadOnlyList<TItem>>A function to extract the list of items from the wrapper object.
headersIEnumerable<KeyValuePair<string, string>>Optional custom headers to include in each paginated request.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TItem>
An asynchronous stream of items.
Type Parameters
TWrapperThe type of the wrapper object in the 'result' field.
TItemThe type of the item in the list.
GetCursorPaginatedAsync<TWrapper, TItem>(string, int?, Func<TWrapper, IReadOnlyList<TItem>>, CancellationToken)
Gets a paginated list of resources using cursor-based pagination where the item list is nested within a wrapper object. This is the core implementation.
protected IAsyncEnumerable<TItem> GetCursorPaginatedAsync<TWrapper, TItem>(string baseUri, int? perPage, Func<TWrapper, IReadOnlyList<TItem>> listExtractor, CancellationToken cancellationToken)
Parameters
baseUristringThe base request URI, without any cursor or per_page parameters.
perPageint?The number of items to request per page.
listExtractorFunc<TWrapper, IReadOnlyList<TItem>>A function to extract the list of items from the wrapper object.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TItem>
An asynchronous stream of items.
Type Parameters
TWrapperThe type of the wrapper object in the 'result' field.
TItemThe type of the item in the list.
GetCursorPaginatedResultAsync<TItem>(string, CancellationToken)
Gets a single page of results from a cursor-based paginated endpoint.
protected Task<CursorPaginatedResult<TItem>> GetCursorPaginatedResultAsync<TItem>(string requestUri, CancellationToken cancellationToken = default)
Parameters
requestUristringcancellationTokenCancellationToken
Returns
- Task<CursorPaginatedResult<TItem>>
A result object containing the items for the page and the cursor metadata.
Type Parameters
TItem
GetCursorPaginatedResultAsync<TWrapper, TItem>(string, Func<TWrapper, IReadOnlyList<TItem>>, IEnumerable<KeyValuePair<string, string>>?, CancellationToken)
Gets a single page of results from a cursor-based paginated endpoint where the item list is nested within a wrapper object in the API response, with optional custom headers.
protected Task<CursorPaginatedResult<TItem>> GetCursorPaginatedResultAsync<TWrapper, TItem>(string requestUri, Func<TWrapper, IReadOnlyList<TItem>> listExtractor, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken = default)
Parameters
requestUristringThe full request URI for the API endpoint.
listExtractorFunc<TWrapper, IReadOnlyList<TItem>>A function to extract the list of items from the wrapper object.
headersIEnumerable<KeyValuePair<string, string>>Optional custom headers to include in the request.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<CursorPaginatedResult<TItem>>
A result object containing the items for the page and the cursor metadata.
Type Parameters
TWrapperThe type of the wrapper object in the 'result' field.
TItemThe type of the item in the list.
GetCursorPaginatedResultAsync<TWrapper, TItem>(string, Func<TWrapper, IReadOnlyList<TItem>>, CancellationToken)
Gets a single page of results from a cursor-based paginated endpoint where the item list is nested within a wrapper object in the API response. This is the core implementation.
protected Task<CursorPaginatedResult<TItem>> GetCursorPaginatedResultAsync<TWrapper, TItem>(string requestUri, Func<TWrapper, IReadOnlyList<TItem>> listExtractor, CancellationToken cancellationToken = default)
Parameters
requestUristringThe full request URI for the API endpoint.
listExtractorFunc<TWrapper, IReadOnlyList<TItem>>A function to extract the list of items from the wrapper object.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<CursorPaginatedResult<TItem>>
A result object containing the items for the page and the cursor metadata.
Type Parameters
TWrapperThe type of the wrapper object in the 'result' field.
TItemThe type of the item in the list.
GetPagePaginatedResultAsync<TItem>(string, CancellationToken)
Gets a single page of results from a page-based paginated endpoint.
protected Task<PagePaginatedResult<TItem>> GetPagePaginatedResultAsync<TItem>(string requestUri, CancellationToken cancellationToken = default)
Parameters
requestUristringcancellationTokenCancellationToken
Returns
- Task<PagePaginatedResult<TItem>>
A result object containing the items for the page and the pagination metadata.
Type Parameters
TItem
GetPaginatedAsync<TItem>(string, int?, CancellationToken)
Gets a paginated list of resources, automatically handling multiple pages of results.
protected IAsyncEnumerable<TItem> GetPaginatedAsync<TItem>(string baseUri, int? perPage = null, CancellationToken cancellationToken = default)
Parameters
baseUristringThe base request URI, without any page or per_page parameters.
perPageint?The number of items to request per page. If null, the API default is used.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TItem>
An asynchronous stream of items.
Type Parameters
TItemThe type of the item in the list.
GetStringAsync(string, CancellationToken)
Sends a GET request to an endpoint that returns a raw string response (e.g., BIND file export).
protected Task<string> GetStringAsync(string requestUri, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
cancellationTokenCancellationTokenA cancellation token.
Returns
Exceptions
- CloudflareApiException
Thrown if the API returns a JSON error envelope.
- HttpRequestException
Thrown if the API returns a non-success status code that is not a JSON error.
PatchAsync<TResult>(string, object?, IEnumerable<KeyValuePair<string, string>>?, CancellationToken)
Sends a PATCH request with a JSON payload and optional custom headers to the specified URI.
protected Task<TResult> PatchAsync<TResult>(string requestUri, object? payload, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
payloadobjectThe object to serialize as the JSON request body.
headersIEnumerable<KeyValuePair<string, string>>Optional custom headers to include in the request.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
PatchAsync<TResult>(string, object?, CancellationToken)
Sends a PATCH request with a JSON payload to the specified URI.
protected Task<TResult> PatchAsync<TResult>(string requestUri, object? payload, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
payloadobjectThe object to serialize as the JSON request body.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
PostAsync<TResult>(string, object?, IEnumerable<KeyValuePair<string, string>>?, CancellationToken)
Sends a POST request with a JSON payload and optional custom headers to the specified URI.
protected Task<TResult> PostAsync<TResult>(string requestUri, object? payload, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
payloadobjectThe object to serialize as the JSON request body.
headersIEnumerable<KeyValuePair<string, string>>Optional custom headers to include in the request.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
PostAsync<TResult>(string, object?, CancellationToken)
Sends a POST request with a JSON payload to the specified URI.
protected Task<TResult> PostAsync<TResult>(string requestUri, object? payload, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
payloadobjectThe object to serialize as the JSON request body.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
PostJsonAsync<TResult>(string, string, CancellationToken)
Sends a POST request with a pre-serialized JSON string to the specified URI.
protected Task<TResult> PostJsonAsync<TResult>(string requestUri, string jsonContent, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
jsonContentstringThe pre-serialized JSON string to send as the request body.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
Remarks
Use this method when you need custom JSON serialization (e.g., camelCase instead of snake_case). The caller is responsible for serializing the payload to JSON.
PostMultipartFileAsync<TResult>(string, Stream, string, string, CancellationToken)
Sends a POST request with a multipart/form-data payload containing a single file stream.
protected Task<TResult> PostMultipartFileAsync<TResult>(string requestUri, Stream stream, string fileName, string formKey = "file", CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
streamStreamThe stream content to upload.
fileNamestringThe name of the file in the form data.
formKeystringThe key for the file in the form data.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
ProcessAndDeserializeAsync<T>(HttpResponseMessage, CancellationToken)
Handles the common logic for processing an API response: checking for non-success status, deserializing, and checking the internal 'success' flag.
protected Task<ApiResponse<T>> ProcessAndDeserializeAsync<T>(HttpResponseMessage response, CancellationToken cancellationToken)
Parameters
responseHttpResponseMessagecancellationTokenCancellationToken
Returns
- Task<ApiResponse<T>>
The full, successfully deserialized ApiResponse<T>.
Type Parameters
T
ProcessResponse<T>(HttpResponseMessage, CancellationToken)
Processes the HttpResponseMessage from a Cloudflare API call.
protected Task<T> ProcessResponse<T>(HttpResponseMessage response, CancellationToken cancellationToken)
Parameters
responseHttpResponseMessageThe HttpResponseMessage to process.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<T>
The deserialized "result" object.
Type Parameters
TThe expected type of the "result" object in the JSON response.
Exceptions
- HttpRequestException
Thrown if the API call returns a non-success status code.
- JsonException
Thrown if the API response body cannot be deserialized.
- CloudflareApiException
Thrown if the API returns a success status code but the response indicates failure (e.g.,
success: false).
PutAsync<TResult>(string, object?, IEnumerable<KeyValuePair<string, string>>?, CancellationToken)
Sends a PUT request with a JSON payload and optional custom headers to the specified URI.
protected Task<TResult> PutAsync<TResult>(string requestUri, object? payload, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
payloadobjectThe object to serialize as the JSON request body.
headersIEnumerable<KeyValuePair<string, string>>Optional custom headers to include in the request.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
PutAsync<TResult>(string, object?, CancellationToken)
Sends a PUT request with a JSON payload to the specified URI.
protected Task<TResult> PutAsync<TResult>(string requestUri, object? payload, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
payloadobjectThe object to serialize as the JSON request body.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
PutJsonAsync<TResult>(string, string, IEnumerable<KeyValuePair<string, string>>?, CancellationToken)
Sends a PUT request with a pre-serialized JSON string and optional custom headers to the specified URI.
protected Task<TResult> PutJsonAsync<TResult>(string requestUri, string jsonContent, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
jsonContentstringThe pre-serialized JSON string to send as the request body.
headersIEnumerable<KeyValuePair<string, string>>Optional custom headers to include in the request.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
Remarks
Use this method when you need custom JSON serialization (e.g., camelCase instead of snake_case). The caller is responsible for serializing the payload to JSON.
PutJsonAsync<TResult>(string, string, CancellationToken)
Sends a PUT request with a pre-serialized JSON string to the specified URI.
protected Task<TResult> PutJsonAsync<TResult>(string requestUri, string jsonContent, CancellationToken cancellationToken = default)
Parameters
requestUristringThe URI to send the request to.
jsonContentstringThe pre-serialized JSON string to send as the request body.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<TResult>
The deserialized "result" object from the API response.
Type Parameters
TResultThe expected type of the "result" object in the JSON response.
Remarks
Use this method when you need custom JSON serialization (e.g., camelCase instead of snake_case). The caller is responsible for serializing the payload to JSON.