RestBaseApiClient
Abstract base class for creating REST API clients. Provides functionality to execute HTTP requests with retry and caching support.
Construtores
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.
Propriedades
protected Uri BaseAddress { get; set; }
value = restBaseApiClient.BaseAddress
restBaseApiClient.BaseAddress = value
Gets or sets the base address for the API.
public IRestApiClientCache Cache { get; set; }
value = restBaseApiClient.Cache
restBaseApiClient.Cache = value
Gets or sets the cache used for storing REST API client responses.
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.
protected HttpMessageInvoker Http { get; }
value = restBaseApiClient.Http
Gets the underlying HTTP message invoker used for sending requests.
public IDictionary<string, string> PerRequestHeaders { get; }
value = restBaseApiClient.PerRequestHeaders
Gets the collection of headers to add per request.
protected virtual bool PlainSingleArg { get; }
value = restBaseApiClient.PlainSingleArg
Gets a value indicating whether a single argument should be sent as a plain value.
protected IMediaTypeFormatter RequestFormatter { get; }
value = restBaseApiClient.RequestFormatter
Gets the media type formatter used for request serialization.
protected IMediaTypeFormatter ResponseFormatter { get; }
value = restBaseApiClient.ResponseFormatter
Gets the media type formatter used for response deserialization.
public RetryPolicyInfo RetryPolicy { get; set; }
value = restBaseApiClient.RetryPolicy
restBaseApiClient.RetryPolicy = value
Gets the retry policy configuration for API calls.
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 .
protected virtual bool ThrowIfNonSuccessStatusCode { get; }
value = restBaseApiClient.ThrowIfNonSuccessStatusCode
Gets a value indicating whether an exception should be thrown for non-success status codes.
public bool Tracing { get; set; }
value = restBaseApiClient.Tracing
restBaseApiClient.Tracing = value
Gets or sets a value indicating whether API calls should be traced.
Métodos
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.
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.
protected void AddAuthBearer(string token)
restBaseApiClient.AddAuthBearer(token)
Adds a Bearer authentication header with the specified token.
- token
- The Bearer token.
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.
Retorna: The constructed HttpRequestMessage.
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.
Retorna: A task representing the asynchronous operation with the result.
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.
Retorna: A task representing the asynchronous operation with the result.
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.
Retorna: A task representing the asynchronous operation with the result.
protected virtual string FormatArgName(string argName)
result = restBaseApiClient.FormatArgName(argName)
Formats the argument name.
- argName
- The original argument name.
Retorna: The formatted argument name.
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.
Retorna: The formatted string representation.
protected virtual object FormatRequest(IDictionary<string, object> parameters)
result = restBaseApiClient.FormatRequest(parameters)
Formats the request parameters.
- parameters
- A dictionary of parameter names and values.
Retorna: The formatted request object.
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.
Retorna: The formatted URI string.
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.
Retorna: The absolute Url.
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.
Retorna: A task representing the asynchronous operation with the result.
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.
Retorna: The current method name.
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.
Retorna: A tuple containing the absolute URL, parameters, and caller method info.
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.
Retorna: The HttpRequestMessage ready to be sent.
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.
Retorna: A task representing the asynchronous operation.
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.
Retorna: A task representing the asynchronous operation with the result.
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.
Retorna: A task representing the asynchronous operation with the result.
protected virtual string ToRequestUri(MethodInfo callerMethod)
result = restBaseApiClient.ToRequestUri(callerMethod)
Converts the caller method information into a request URI.
- callerMethod
- The caller method info.
Retorna: The formatted request URI string.
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.
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.
Retorna: The formatted argument.
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.
Eventos
public event Action<HttpMethod, Uri, object> LogRequest
restBaseApiClient.LogRequest += handler
Occurs when a request is about to be executed.