Class RestBaseApiClient
Abstract base class for creating REST API clients. Provides functionality to execute HTTP requests with retry and caching support.
public abstract class RestBaseApiClient
- Inheritance
-
RestBaseApiClient
- Inherited Members
- Extension Methods
Constructors
RestBaseApiClient(HttpMessageInvoker, MediaTypeFormatter, MediaTypeFormatter)
Abstract base class for creating REST API clients. Provides functionality to execute HTTP requests with retry and caching support.
protected RestBaseApiClient(HttpMessageInvoker http, MediaTypeFormatter request, MediaTypeFormatter response)
Parameters
http
HttpMessageInvokerrequest
MediaTypeFormatterresponse
MediaTypeFormatter
Properties
BaseAddress
Gets or sets the base address for the API.
protected Uri BaseAddress { get; set; }
Property Value
Cache
Gets or sets the cache used for storing REST API client responses.
public IRestApiClientCache Cache { get; set; }
Property Value
ExtractBadResponse
Gets or sets a value indicating whether detailed error information should be extracted from non-success responses.
public bool ExtractBadResponse { get; set; }
Property Value
Http
Gets the underlying HTTP message invoker used for sending requests.
protected HttpMessageInvoker Http { get; }
Property Value
PerRequestHeaders
Gets the collection of headers to add per request.
public IDictionary<string, string> PerRequestHeaders { get; }
Property Value
PlainSingleArg
Gets a value indicating whether a single argument should be sent as a plain value.
protected virtual bool PlainSingleArg { get; }
Property Value
RequestFormatter
Gets the media type formatter used for request serialization.
protected MediaTypeFormatter RequestFormatter { get; }
Property Value
- MediaTypeFormatter
ResponseFormatter
Gets the media type formatter used for response deserialization.
protected MediaTypeFormatter ResponseFormatter { get; }
Property Value
- MediaTypeFormatter
RetryPolicy
Gets the retry policy configuration for API calls.
public RetryPolicyInfo RetryPolicy { get; }
Property Value
ThrowIfNonSuccessStatusCode
Gets a value indicating whether an exception should be thrown for non-success status codes.
protected virtual bool ThrowIfNonSuccessStatusCode { get; }
Property Value
Tracing
Gets or sets a value indicating whether API calls should be traced.
public bool Tracing { get; set; }
Property Value
Methods
AddAuth(AuthenticationSchemes, string)
Adds an authentication header using the specified authentication scheme and value.
protected void AddAuth(AuthenticationSchemes schema, string value)
Parameters
schema
AuthenticationSchemesThe authentication scheme.
value
stringThe authentication value.
AddAuth(string, string)
Adds an authentication header with the specified schema and value.
protected void AddAuth(string schema, string value)
Parameters
AddAuthBearer(string)
Adds a Bearer authentication header with the specified token.
protected void AddAuthBearer(string token)
Parameters
token
stringThe Bearer token.
CreateRequest(HttpMethod, Uri)
Creates an HttpRequestMessage for the specified HTTP method and URI. Adds the accepted media type and per-request headers.
protected HttpRequestMessage CreateRequest(HttpMethod method, Uri uri)
Parameters
method
HttpMethodThe HTTP method.
uri
UriThe request URI.
Returns
- HttpRequestMessage
The constructed HttpRequestMessage.
DeleteAsync<TResult>(string, CancellationToken, params object[])
Executes a DELETE request for the specified method name and arguments.
protected Task<TResult> DeleteAsync<TResult>(string methodName, CancellationToken cancellationToken, params object[] args)
Parameters
methodName
stringThe name of the method corresponding to the API endpoint.
cancellationToken
CancellationTokenA cancellation token.
args
object[]The arguments for the API method.
Returns
- Task<TResult>
A task representing the asynchronous operation with the result.
Type Parameters
TResult
The type of the result expected.
DoAsync<TResult>(HttpMethod, Uri, object, CancellationToken)
Executes an API call using the specified HTTP method, URI, and request body. Applies caching and retries as configured.
protected Task<TResult> DoAsync<TResult>(HttpMethod method, Uri uri, object body, CancellationToken cancellationToken)
Parameters
method
HttpMethodThe HTTP method to use.
uri
UriThe request URI.
body
objectThe request body.
cancellationToken
CancellationTokenA cancellation token.
Returns
- Task<TResult>
A task representing the asynchronous operation with the result.
Type Parameters
TResult
The type of the result expected.
DoAsync<TResult>(HttpRequestMessage, CancellationToken)
Sends the specified HTTP request message and processes the response.
protected Task<TResult> DoAsync<TResult>(HttpRequestMessage request, CancellationToken cancellationToken)
Parameters
request
HttpRequestMessageThe HTTP request message.
cancellationToken
CancellationTokenA cancellation token.
Returns
- Task<TResult>
A task representing the asynchronous operation with the result.
Type Parameters
TResult
The type of the result expected.
FormatArgName(string)
Formats the argument name.
protected virtual string FormatArgName(string argName)
Parameters
argName
stringThe original argument name.
Returns
- string
The formatted argument name.
FormatRequest(IDictionary<string, object>)
Formats the request parameters.
protected virtual object FormatRequest(IDictionary<string, object> parameters)
Parameters
parameters
IDictionary<string, object>A dictionary of parameter names and values.
Returns
- object
The formatted request object.
FormatRequestUri(string)
Formats the request URI by removing "Async" suffix and converting to lower case.
protected virtual string FormatRequestUri(string requestUri)
Parameters
requestUri
stringThe original request URI.
Returns
- string
The formatted URI string.
GetAbsolute(string)
Combines the base address with a relative URI to produce an absolute URL.
protected Url GetAbsolute(string relative)
Parameters
relative
stringThe relative URI.
Returns
GetAsync<TResult>(string, CancellationToken, params object[])
Executes a GET request for the specified method name and arguments.
protected Task<TResult> GetAsync<TResult>(string methodName, CancellationToken cancellationToken, params object[] args)
Parameters
methodName
stringThe name of the method corresponding to the API endpoint.
cancellationToken
CancellationTokenA cancellation token.
args
object[]The arguments for the API method.
Returns
- Task<TResult>
A task representing the asynchronous operation with the result.
Type Parameters
TResult
The type of the result expected.
GetCurrentMethod(string)
Gets the current method name. Intended for use with caller information.
protected static string GetCurrentMethod(string methodName = "")
Parameters
methodName
stringThe caller method name automatically provided.
Returns
- string
The current method name.
GetInfo(HttpMethod, string, object[])
Retrieves information for the API method call, including the absolute URL, parameter list, and caller method info.
protected virtual (Url url, (string name, object value)[] parameters, MethodInfo callerMethod) GetInfo(HttpMethod method, string methodName, object[] args)
Parameters
method
HttpMethodThe HTTP method to use.
methodName
stringThe method name corresponding to the API endpoint.
args
object[]The arguments to pass to the API method.
Returns
- (Url url, (string name, object value)[] parameters, MethodInfo callerMethod)
A tuple containing the absolute URL, parameters, and caller method info.
GetRequest(HttpMethod, Uri, object)
Constructs an HTTP request message with the specified method, URI, and optional body.
protected virtual HttpRequestMessage GetRequest(HttpMethod method, Uri uri, object body)
Parameters
method
HttpMethodThe HTTP method to use.
uri
UriThe request URI.
body
objectThe request body.
Returns
- HttpRequestMessage
The HttpRequestMessage ready to be sent.
GetResultAsync<TResult>(HttpResponseMessage, CancellationToken)
Retrieves the result from the HTTP response content.
protected virtual Task<TResult> GetResultAsync<TResult>(HttpResponseMessage response, CancellationToken cancellationToken)
Parameters
response
HttpResponseMessageThe HTTP response message.
cancellationToken
CancellationTokenA cancellation token.
Returns
- Task<TResult>
A task representing the asynchronous operation.
Type Parameters
TResult
The type of the result.
PostAsync<TResult>(string, CancellationToken, params object[])
Executes a POST request for the specified method name and arguments.
protected Task<TResult> PostAsync<TResult>(string methodName, CancellationToken cancellationToken, params object[] args)
Parameters
methodName
stringThe name of the method corresponding to the API endpoint.
cancellationToken
CancellationTokenA cancellation token.
args
object[]The arguments for the API method.
Returns
- Task<TResult>
A task representing the asynchronous operation with the result.
Type Parameters
TResult
The type of the result expected.
PutAsync<TResult>(string, CancellationToken, params object[])
Executes a PUT request for the specified method name and arguments.
protected Task<TResult> PutAsync<TResult>(string methodName, CancellationToken cancellationToken, params object[] args)
Parameters
methodName
stringThe name of the method corresponding to the API endpoint.
cancellationToken
CancellationTokenA cancellation token.
args
object[]The arguments for the API method.
Returns
- Task<TResult>
A task representing the asynchronous operation with the result.
Type Parameters
TResult
The type of the result expected.
ToRequestUri(MethodInfo)
Converts the caller method information into a request URI.
protected virtual string ToRequestUri(MethodInfo callerMethod)
Parameters
callerMethod
MethodInfoThe caller method info.
Returns
- string
The formatted request URI string.
TraceCall(HttpMethod, Uri, TimeSpan)
Traces the API call by writing the HTTP method, URI, and elapsed time.
protected virtual void TraceCall(HttpMethod method, Uri uri, TimeSpan elapsed)
Parameters
method
HttpMethodThe HTTP method used.
uri
UriThe request URI.
elapsed
TimeSpanThe elapsed time for the call.
TryFormat(object, MethodInfo, HttpMethod)
Tries to format the argument based on its type. Converts enumerations and booleans to their numeric representation.
protected virtual object TryFormat(object arg, MethodInfo callerMethod, HttpMethod method)
Parameters
arg
objectThe argument to format.
callerMethod
MethodInfoThe caller method information.
method
HttpMethodThe HTTP method.
Returns
- object
The formatted argument.
ValidateResponseAsync(HttpResponseMessage, CancellationToken)
Validates the HTTP response. Throws an exception if the response is not successful and ThrowIfNonSuccessStatusCode is true.
protected virtual Task ValidateResponseAsync(HttpResponseMessage response, CancellationToken cancellationToken)
Parameters
response
HttpResponseMessageThe HTTP response message.
cancellationToken
CancellationTokenA cancellation token.
Returns
Events
LogRequest
Occurs when a request is about to be executed.
public event Action<HttpMethod, Uri, object> LogRequest