Table of Contents

Class RestSharpHelper

Namespace
Ecng.Net
Assembly
Ecng.Net.SocketIO.dll

Provides helper methods to work with RestSharp requests and responses.

public static class RestSharpHelper
Inheritance
RestSharpHelper
Inherited Members

Methods

AddBodyAsStr(RestRequest, string)

Adds a string body to the request with JSON format.

public static void AddBodyAsStr(this RestRequest request, string bodyStr)

Parameters

request RestRequest

The RestRequest to which the body will be added.

bodyStr string

The string representation of the body.

Exceptions

ArgumentNullException

Thrown when request is null.

DecodeJWT(string)

Decodes a JSON Web Token (JWT) into its component parts.

public static IEnumerable<string> DecodeJWT(this string jwt)

Parameters

jwt string

The JWT string.

Returns

IEnumerable<string>

An enumerable of strings representing the decoded parts of the JWT.

Invoke(RestRequest, Uri, object, Action<string, object[]>, Func<string, string>, bool)

Invokes the RestRequest synchronously.

public static object Invoke(this RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, Func<string, string> contentConverter = null, bool throwIfEmptyResponse = true)

Parameters

request RestRequest

The RestRequest to invoke.

url Uri

The URI endpoint for the request.

caller object

The caller object for logging context.

logVerbose Action<string, object[]>

An action to log verbose messages.

contentConverter Func<string, string>

An optional function to convert the response content.

throwIfEmptyResponse bool

Indicates whether to throw an exception if the response is empty.

Returns

object

The deserialized object response.

Invoke2<T>(RestRequest, Uri, object, Action<string, object[]>, Func<string, string>, bool)

Invokes the RestRequest synchronously and returns the full RestResponse of type T.

public static RestResponse<T> Invoke2<T>(this RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, Func<string, string> contentConverter = null, bool throwIfEmptyResponse = true)

Parameters

request RestRequest

The RestRequest to invoke.

url Uri

The URI endpoint for the request.

caller object

The caller object for logging context.

logVerbose Action<string, object[]>

An action to log verbose messages.

contentConverter Func<string, string>

An optional function to convert the response content.

throwIfEmptyResponse bool

Indicates whether to throw an exception if the response is empty.

Returns

RestResponse<T>

The full RestSharp.RestResponse<> response.

Type Parameters

T

The expected type of the deserialized response.

InvokeAsync(RestRequest, Uri, object, Action<string, object[]>, CancellationToken, Func<string, string>, bool)

Invokes the RestRequest asynchronously.

public static Task<object> InvokeAsync(this RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, CancellationToken token, Func<string, string> contentConverter = null, bool throwIfEmptyResponse = true)

Parameters

request RestRequest

The RestRequest to invoke.

url Uri

The URI endpoint for the request.

caller object

The caller object for logging context.

logVerbose Action<string, object[]>

An action to log verbose messages.

token CancellationToken

The cancellation token.

contentConverter Func<string, string>

An optional function to convert the response content.

throwIfEmptyResponse bool

Indicates whether to throw an exception if the response is empty.

Returns

Task<object>

A task representing the asynchronous operation, returning the deserialized object response.

InvokeAsync2<T>(RestRequest, Uri, object, Action<string, object[]>, CancellationToken, Func<string, string>, IAuthenticator, bool)

Asynchronously invokes the RestRequest and returns the full RestSharp.RestResponse<> response.

public static Task<RestResponse<T>> InvokeAsync2<T>(this RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, CancellationToken token, Func<string, string> contentConverter = null, IAuthenticator auth = null, bool throwIfEmptyResponse = true)

Parameters

request RestRequest

The RestRequest to invoke.

url Uri

The URI endpoint for the request.

caller object

The caller object for logging context.

logVerbose Action<string, object[]>

An action to log verbose messages.

token CancellationToken

The cancellation token.

contentConverter Func<string, string>

An optional function to convert the response content.

auth IAuthenticator

An optional authenticator for the request.

throwIfEmptyResponse bool

Indicates whether to throw an exception if the response is empty.

Returns

Task<RestResponse<T>>

A task representing the asynchronous operation, returning the full RestSharp.RestResponse<> response.

Type Parameters

T

The expected type of the deserialized response.

InvokeAsync3<T>(RestRequest, Uri, object, Action<string, object[]>, CancellationToken, Func<string, string>, IAuthenticator, bool, Func<HttpStatusCode, bool>)

Asynchronously invokes the RestRequest with extended error handling and returns the full RestSharp.RestResponse<> response.

public static Task<RestResponse<T>> InvokeAsync3<T>(this RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, CancellationToken token, Func<string, string> contentConverter = null, IAuthenticator auth = null, bool throwIfEmptyResponse = true, Func<HttpStatusCode, bool> handleErrorStatus = null)

Parameters

request RestRequest

The RestRequest to invoke.

url Uri

The URI endpoint for the request.

caller object

The caller object for logging context.

logVerbose Action<string, object[]>

An action to log verbose messages.

token CancellationToken

The cancellation token.

contentConverter Func<string, string>

An optional function to convert the response content.

auth IAuthenticator

An optional authenticator for the request.

throwIfEmptyResponse bool

Indicates whether to throw an exception if the response is empty.

handleErrorStatus Func<HttpStatusCode, bool>

An optional function to handle error HTTP status codes.

Returns

Task<RestResponse<T>>

A task representing the asynchronous operation, returning the full RestSharp.RestResponse<> response.

Type Parameters

T

The expected type of the deserialized response.

InvokeAsync<T>(RestRequest, Uri, object, Action<string, object[]>, CancellationToken, Func<string, string>, bool)

Asynchronously invokes the RestRequest and returns a deserialized response of type T.

public static Task<T> InvokeAsync<T>(this RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, CancellationToken token, Func<string, string> contentConverter = null, bool throwIfEmptyResponse = true)

Parameters

request RestRequest

The RestRequest to invoke.

url Uri

The URI endpoint for the request.

caller object

The caller object for logging context.

logVerbose Action<string, object[]>

An action to log verbose messages.

token CancellationToken

The cancellation token.

contentConverter Func<string, string>

An optional function to convert the response content.

throwIfEmptyResponse bool

Indicates whether to throw an exception if the response is empty.

Returns

Task<T>

A task representing the asynchronous operation, returning the deserialized response of type T.

Type Parameters

T

The expected type of the deserialized response.

Invoke<T>(RestRequest, Uri, object, Action<string, object[]>, Func<string, string>, bool)

Invokes the RestRequest synchronously and returns a deserialized response of type T.

public static T Invoke<T>(this RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, Func<string, string> contentConverter = null, bool throwIfEmptyResponse = true)

Parameters

request RestRequest

The RestRequest to invoke.

url Uri

The URI endpoint for the request.

caller object

The caller object for logging context.

logVerbose Action<string, object[]>

An action to log verbose messages.

contentConverter Func<string, string>

An optional function to convert the response content.

throwIfEmptyResponse bool

Indicates whether to throw an exception if the response is empty.

Returns

T

The deserialized response of type T.

Type Parameters

T

The expected type of the deserialized response.

RemoveWhere(RestRequest, Func<Parameter, bool>)

Removes parameters from the request that match the specified filter.

public static void RemoveWhere(this RestRequest request, Func<Parameter, bool> filter)

Parameters

request RestRequest

The RestRequest to modify.

filter Func<Parameter, bool>

The filter function to determine which parameters to remove.

SetBearer(RestRequest, SecureString)

Adds a Bearer authorization header to the RestRequest.

public static RestRequest SetBearer(this RestRequest client, SecureString token)

Parameters

client RestRequest

The RestRequest to modify.

token SecureString

The secure token used for Bearer authentication.

Returns

RestRequest

The modified RestRequest.

ToError(RestResponse, string)

Converts a RestResponse into a RestSharpException with additional error details.

public static RestSharpException ToError(this RestResponse response, string message = null)

Parameters

response RestResponse

The RestResponse instance.

message string

An optional error message.

Returns

RestSharpException

A new instance of RestSharpException.

ToQueryString(IEnumerable<Parameter>, bool)

Converts a collection of parameters into a query string representation.

public static string ToQueryString(this IEnumerable<Parameter> parameters, bool encodeValue = true)

Parameters

parameters IEnumerable<Parameter>

The collection of parameters.

encodeValue bool

Indicates whether the parameter values should be encoded.

Returns

string

A query string representing the parameters.