Table of Contents

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

httpClient HttpClient

The HttpClient to use for requests.

logger ILogger

The logger for this API resource.

Fields

HttpClient

The configured HttpClient for making API requests.

protected readonly HttpClient HttpClient

Field Value

HttpClient

Logger

The logger for this API resource.

protected readonly ILogger Logger

Field Value

ILogger

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

requestUri string

The URI to send the request to.

headers IEnumerable<KeyValuePair<string, string>>

Optional custom headers to include in the request.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

requestUri string

The URI to send the request to.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

requestUri string

The URI to send the request to.

headers IEnumerable<KeyValuePair<string, string>>

Optional custom headers to include in the request.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

requestUri string

The URI to send the request to.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

baseUri string

The base request URI, without any cursor or per_page parameters.

perPage int?

The number of items to request per page.

cancellationToken CancellationToken

A cancellation token.

Returns

IAsyncEnumerable<TItem>

An asynchronous stream of items.

Type Parameters

TItem

The 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

baseUri string

The base request URI, without any cursor or per_page parameters.

perPage int?

The number of items to request per page.

listExtractor Func<TWrapper, IReadOnlyList<TItem>>

A function to extract the list of items from the wrapper object.

headers IEnumerable<KeyValuePair<string, string>>

Optional custom headers to include in each paginated request.

cancellationToken CancellationToken

A cancellation token.

Returns

IAsyncEnumerable<TItem>

An asynchronous stream of items.

Type Parameters

TWrapper

The type of the wrapper object in the 'result' field.

TItem

The 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

baseUri string

The base request URI, without any cursor or per_page parameters.

perPage int?

The number of items to request per page.

listExtractor Func<TWrapper, IReadOnlyList<TItem>>

A function to extract the list of items from the wrapper object.

cancellationToken CancellationToken

A cancellation token.

Returns

IAsyncEnumerable<TItem>

An asynchronous stream of items.

Type Parameters

TWrapper

The type of the wrapper object in the 'result' field.

TItem

The 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

requestUri string
cancellationToken CancellationToken

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

requestUri string

The full request URI for the API endpoint.

listExtractor Func<TWrapper, IReadOnlyList<TItem>>

A function to extract the list of items from the wrapper object.

headers IEnumerable<KeyValuePair<string, string>>

Optional custom headers to include in the request.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<CursorPaginatedResult<TItem>>

A result object containing the items for the page and the cursor metadata.

Type Parameters

TWrapper

The type of the wrapper object in the 'result' field.

TItem

The 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

requestUri string

The full request URI for the API endpoint.

listExtractor Func<TWrapper, IReadOnlyList<TItem>>

A function to extract the list of items from the wrapper object.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<CursorPaginatedResult<TItem>>

A result object containing the items for the page and the cursor metadata.

Type Parameters

TWrapper

The type of the wrapper object in the 'result' field.

TItem

The 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

requestUri string
cancellationToken CancellationToken

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

baseUri string

The base request URI, without any page or per_page parameters.

perPage int?

The number of items to request per page. If null, the API default is used.

cancellationToken CancellationToken

A cancellation token.

Returns

IAsyncEnumerable<TItem>

An asynchronous stream of items.

Type Parameters

TItem

The 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

requestUri string

The URI to send the request to.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<string>

The raw string content of the response body.

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

requestUri string

The URI to send the request to.

payload object

The object to serialize as the JSON request body.

headers IEnumerable<KeyValuePair<string, string>>

Optional custom headers to include in the request.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

requestUri string

The URI to send the request to.

payload object

The object to serialize as the JSON request body.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

requestUri string

The URI to send the request to.

payload object

The object to serialize as the JSON request body.

headers IEnumerable<KeyValuePair<string, string>>

Optional custom headers to include in the request.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

requestUri string

The URI to send the request to.

payload object

The object to serialize as the JSON request body.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

requestUri string

The URI to send the request to.

jsonContent string

The pre-serialized JSON string to send as the request body.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

requestUri string

The URI to send the request to.

stream Stream

The stream content to upload.

fileName string

The name of the file in the form data.

formKey string

The key for the file in the form data.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

response HttpResponseMessage
cancellationToken CancellationToken

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

response HttpResponseMessage

The HttpResponseMessage to process.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<T>

The deserialized "result" object.

Type Parameters

T

The 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

requestUri string

The URI to send the request to.

payload object

The object to serialize as the JSON request body.

headers IEnumerable<KeyValuePair<string, string>>

Optional custom headers to include in the request.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

requestUri string

The URI to send the request to.

payload object

The object to serialize as the JSON request body.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

requestUri string

The URI to send the request to.

jsonContent string

The pre-serialized JSON string to send as the request body.

headers IEnumerable<KeyValuePair<string, string>>

Optional custom headers to include in the request.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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

requestUri string

The URI to send the request to.

jsonContent string

The pre-serialized JSON string to send as the request body.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<TResult>

The deserialized "result" object from the API response.

Type Parameters

TResult

The 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.