RestBaseApiClient

Ecng.Net

Abstract base class for creating REST API clients. Provides functionality to execute HTTP requests with retry and caching support.

构造函数

RestBaseApiClient
protected RestBaseApiClient(HttpMessageInvoker http, IMediaTypeFormatter request, IMediaTypeFormatter response)
restBaseApiClient = RestBaseApiClient(http, request, response)

Abstract base class for creating REST API clients. Provides functionality to execute HTTP requests with retry and caching support.

属性

BaseAddress
protected Uri BaseAddress { get; set; }
value = restBaseApiClient.BaseAddress
restBaseApiClient.BaseAddress = value

Gets or sets the base address for the API.

Cache
public IRestApiClientCache Cache { get; set; }
value = restBaseApiClient.Cache
restBaseApiClient.Cache = value

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

ExtractBadResponse
public bool ExtractBadResponse { get; set; }
value = restBaseApiClient.ExtractBadResponse
restBaseApiClient.ExtractBadResponse = value

Gets or sets a value indicating whether detailed error information should be extracted from non-success responses.

Http
protected HttpMessageInvoker Http { get; }
value = restBaseApiClient.Http

Gets the underlying HTTP message invoker used for sending requests.

PerRequestHeaders
public IDictionary<string, string> PerRequestHeaders { get; }
value = restBaseApiClient.PerRequestHeaders

Gets the collection of headers to add per request.

PlainSingleArg
protected virtual bool PlainSingleArg { get; }
value = restBaseApiClient.PlainSingleArg

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

RequestFormatter
protected IMediaTypeFormatter RequestFormatter { get; }
value = restBaseApiClient.RequestFormatter

Gets the media type formatter used for request serialization.

ResponseFormatter
protected IMediaTypeFormatter ResponseFormatter { get; }
value = restBaseApiClient.ResponseFormatter

Gets the media type formatter used for response deserialization.

RetryPolicy
public RetryPolicyInfo RetryPolicy { get; set; }
value = restBaseApiClient.RetryPolicy
restBaseApiClient.RetryPolicy = value

Gets the retry policy configuration for API calls.

SerializeNullBody
public bool SerializeNullBody { get; set; }
value = restBaseApiClient.SerializeNullBody
restBaseApiClient.SerializeNullBody = value

Gets or sets a value indicating whether POST/PUT requests with a null body should serialize JSON null to set Content-Type header. This prevents 415 Unsupported Media Type when the server expects [FromBody]. Default is .

ThrowIfNonSuccessStatusCode
protected virtual bool ThrowIfNonSuccessStatusCode { get; }
value = restBaseApiClient.ThrowIfNonSuccessStatusCode

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

Tracing
public bool Tracing { get; set; }
value = restBaseApiClient.Tracing
restBaseApiClient.Tracing = value

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

方法

AddAuth
protected void AddAuth(AuthenticationSchemes schema, string value)
restBaseApiClient.AddAuth(schema, value)

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

schema
The authentication scheme.
value
The authentication value.
AddAuth
protected void AddAuth(string schema, string value)
restBaseApiClient.AddAuth(schema, value)

Adds an authentication header with the specified schema and value.

schema
The authentication scheme.
value
The authentication value.
AddAuthBearer
protected void AddAuthBearer(string token)
restBaseApiClient.AddAuthBearer(token)

Adds a Bearer authentication header with the specified token.

token
The Bearer token.
CreateRequest
protected HttpRequestMessage CreateRequest(HttpMethod method, Uri uri)
result = restBaseApiClient.CreateRequest(method, uri)

Creates an HttpRequestMessage for the specified HTTP method and URI. Adds the accepted media type and per-request headers.

method
The HTTP method.
uri
The request URI.

返回值: The constructed HttpRequestMessage.

DeleteAsync``1
protected Task<TResult> DeleteAsync<TResult>(string methodName, CancellationToken cancellationToken, object[] args)
result = restBaseApiClient.DeleteAsync(methodName, cancellationToken, args)

Executes a DELETE request for the specified method name and arguments.

methodName
The name of the method corresponding to the API endpoint.
cancellationToken
A cancellation token.
args
The arguments for the API method.

返回值: A task representing the asynchronous operation with the result.

DoAsync``1
protected Task<TResult> DoAsync<TResult>(HttpRequestMessage request, CancellationToken cancellationToken)
result = restBaseApiClient.DoAsync(request, cancellationToken)

Sends the specified HTTP request message and processes the response.

request
The HTTP request message.
cancellationToken
A cancellation token.

返回值: A task representing the asynchronous operation with the result.

DoAsync``1
protected Task<TResult> DoAsync<TResult>(HttpMethod method, Uri uri, object body, CancellationToken cancellationToken)
result = restBaseApiClient.DoAsync(method, uri, body, cancellationToken)

Executes an API call using the specified HTTP method, URI, and request body. Applies caching and retries as configured.

method
The HTTP method to use.
uri
The request URI.
body
The request body.
cancellationToken
A cancellation token.

返回值: A task representing the asynchronous operation with the result.

FormatArgName
protected virtual string FormatArgName(string argName)
result = restBaseApiClient.FormatArgName(argName)

Formats the argument name.

argName
The original argument name.

返回值: The formatted argument name.

FormatQueryValue
protected virtual string FormatQueryValue(object value)
result = restBaseApiClient.FormatQueryValue(value)

Formats a value for use in a query string parameter.

value
The value to format.

返回值: The formatted string representation.

FormatRequest
protected virtual object FormatRequest(IDictionary<string, object> parameters)
result = restBaseApiClient.FormatRequest(parameters)

Formats the request parameters.

parameters
A dictionary of parameter names and values.

返回值: The formatted request object.

FormatRequestUri
protected virtual string FormatRequestUri(string requestUri)
result = restBaseApiClient.FormatRequestUri(requestUri)

Formats the request URI by removing "Async" suffix and converting to lower case.

requestUri
The original request URI.

返回值: The formatted URI string.

GetAbsolute
protected Url GetAbsolute(string relative)
result = restBaseApiClient.GetAbsolute(relative)

Combines the base address with a relative URI to produce an absolute URL.

relative
The relative URI.

返回值: The absolute Url.

GetAsync``1
protected Task<TResult> GetAsync<TResult>(string methodName, CancellationToken cancellationToken, object[] args)
result = restBaseApiClient.GetAsync(methodName, cancellationToken, args)

Executes a GET request for the specified method name and arguments.

methodName
The name of the method corresponding to the API endpoint.
cancellationToken
A cancellation token.
args
The arguments for the API method.

返回值: A task representing the asynchronous operation with the result.

GetCurrentMethod
protected static string GetCurrentMethod(string methodName)
result = RestBaseApiClient.GetCurrentMethod(methodName)

Gets the current method name. Intended for use with caller information.

methodName
The caller method name automatically provided.

返回值: The current method name.

GetInfo
protected virtual ValueTuple<Url, ValueTuple<string, object>[], MethodInfo> GetInfo(HttpMethod method, string methodName, object[] args)
result = restBaseApiClient.GetInfo(method, methodName, args)

Retrieves information for the API method call, including the absolute URL, parameter list, and caller method info.

method
The HTTP method to use.
methodName
The method name corresponding to the API endpoint.
args
The arguments to pass to the API method.

返回值: A tuple containing the absolute URL, parameters, and caller method info.

GetRequest
protected virtual HttpRequestMessage GetRequest(HttpMethod method, Uri uri, object body)
result = restBaseApiClient.GetRequest(method, uri, body)

Constructs an HTTP request message with the specified method, URI, and optional body.

method
The HTTP method to use.
uri
The request URI.
body
The request body.

返回值: The HttpRequestMessage ready to be sent.

GetResultAsync``1
protected virtual Task<TResult> GetResultAsync<TResult>(HttpResponseMessage response, CancellationToken cancellationToken)
result = restBaseApiClient.GetResultAsync(response, cancellationToken)

Retrieves the result from the HTTP response content.

response
The HTTP response message.
cancellationToken
A cancellation token.

返回值: A task representing the asynchronous operation.

PostAsync``1
protected Task<TResult> PostAsync<TResult>(string methodName, CancellationToken cancellationToken, object[] args)
result = restBaseApiClient.PostAsync(methodName, cancellationToken, args)

Executes a POST request for the specified method name and arguments.

methodName
The name of the method corresponding to the API endpoint.
cancellationToken
A cancellation token.
args
The arguments for the API method.

返回值: A task representing the asynchronous operation with the result.

PutAsync``1
protected Task<TResult> PutAsync<TResult>(string methodName, CancellationToken cancellationToken, object[] args)
result = restBaseApiClient.PutAsync(methodName, cancellationToken, args)

Executes a PUT request for the specified method name and arguments.

methodName
The name of the method corresponding to the API endpoint.
cancellationToken
A cancellation token.
args
The arguments for the API method.

返回值: A task representing the asynchronous operation with the result.

ToRequestUri
protected virtual string ToRequestUri(MethodInfo callerMethod)
result = restBaseApiClient.ToRequestUri(callerMethod)

Converts the caller method information into a request URI.

callerMethod
The caller method info.

返回值: The formatted request URI string.

TraceCall
protected virtual void TraceCall(HttpMethod method, Uri uri, TimeSpan elapsed)
restBaseApiClient.TraceCall(method, uri, elapsed)

Traces the API call by writing the HTTP method, URI, and elapsed time.

method
The HTTP method used.
uri
The request URI.
elapsed
The elapsed time for the call.
TryFormat
protected virtual object TryFormat(object arg, MethodInfo callerMethod, HttpMethod method)
result = restBaseApiClient.TryFormat(arg, callerMethod, method)

Tries to format the argument based on its type. Converts enumerations and booleans to their numeric representation.

arg
The argument to format.
callerMethod
The caller method information.
method
The HTTP method.

返回值: The formatted argument.

ValidateResponseAsync
protected virtual Task ValidateResponseAsync(HttpResponseMessage response, CancellationToken cancellationToken)
result = restBaseApiClient.ValidateResponseAsync(response, cancellationToken)

Validates the HTTP response. Throws an exception if the response is not successful and ThrowIfNonSuccessStatusCode is true.

response
The HTTP response message.
cancellationToken
A cancellation token.

事件

LogRequest
public event Action<HttpMethod, Uri, object> LogRequest
restBaseApiClient.LogRequest += handler

Occurs when a request is about to be executed.