Table of Contents

Class RestBaseApiClient

Namespace
Ecng.Net
Assembly
Ecng.Net.Clients.dll

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 HttpMessageInvoker
request MediaTypeFormatter
response MediaTypeFormatter

Properties

BaseAddress

Gets or sets the base address for the API.

protected Uri BaseAddress { get; set; }

Property Value

Uri

Cache

Gets or sets the cache used for storing REST API client responses.

public IRestApiClientCache Cache { get; set; }

Property Value

IRestApiClientCache

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

bool

Http

Gets the underlying HTTP message invoker used for sending requests.

protected HttpMessageInvoker Http { get; }

Property Value

HttpMessageInvoker

PerRequestHeaders

Gets the collection of headers to add per request.

public IDictionary<string, string> PerRequestHeaders { get; }

Property Value

IDictionary<string, string>

PlainSingleArg

Gets a value indicating whether a single argument should be sent as a plain value.

protected virtual bool PlainSingleArg { get; }

Property Value

bool

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

RetryPolicyInfo

ThrowIfNonSuccessStatusCode

Gets a value indicating whether an exception should be thrown for non-success status codes.

protected virtual bool ThrowIfNonSuccessStatusCode { get; }

Property Value

bool

Tracing

Gets or sets a value indicating whether API calls should be traced.

public bool Tracing { get; set; }

Property Value

bool

Methods

AddAuth(AuthenticationSchemes, string)

Adds an authentication header using the specified authentication scheme and value.

protected void AddAuth(AuthenticationSchemes schema, string value)

Parameters

schema AuthenticationSchemes

The authentication scheme.

value string

The authentication value.

AddAuth(string, string)

Adds an authentication header with the specified schema and value.

protected void AddAuth(string schema, string value)

Parameters

schema string

The authentication scheme.

value string

The authentication value.

AddAuthBearer(string)

Adds a Bearer authentication header with the specified token.

protected void AddAuthBearer(string token)

Parameters

token string

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

The HTTP method.

uri Uri

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

The name of the method corresponding to the API endpoint.

cancellationToken CancellationToken

A 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 HttpMethod

The HTTP method to use.

uri Uri

The request URI.

body object

The request body.

cancellationToken CancellationToken

A 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 HttpRequestMessage

The HTTP request message.

cancellationToken CancellationToken

A 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 string

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

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

The relative URI.

Returns

Url

The absolute Url.

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 string

The name of the method corresponding to the API endpoint.

cancellationToken CancellationToken

A 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 string

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

The HTTP method to use.

methodName string

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

The HTTP method to use.

uri Uri

The request URI.

body object

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

The HTTP response message.

cancellationToken CancellationToken

A 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 string

The name of the method corresponding to the API endpoint.

cancellationToken CancellationToken

A 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 string

The name of the method corresponding to the API endpoint.

cancellationToken CancellationToken

A 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 MethodInfo

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

The HTTP method used.

uri Uri

The request URI.

elapsed TimeSpan

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

The argument to format.

callerMethod MethodInfo

The caller method information.

method HttpMethod

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

The HTTP response message.

cancellationToken CancellationToken

A cancellation token.

Returns

Task

Events

LogRequest

Occurs when a request is about to be executed.

public event Action<HttpMethod, Uri, object> LogRequest

Event Type

Action<HttpMethod, Uri, object>