Class RestSharpHelper
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
requestRestRequestThe RestRequest to which the body will be added.
bodyStrstringThe 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
jwtstringThe 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
requestRestRequestThe RestRequest to invoke.
urlUriThe URI endpoint for the request.
callerobjectThe caller object for logging context.
logVerboseAction<string, object[]>An action to log verbose messages.
contentConverterFunc<string, string>An optional function to convert the response content.
throwIfEmptyResponseboolIndicates 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
requestRestRequestThe RestRequest to invoke.
urlUriThe URI endpoint for the request.
callerobjectThe caller object for logging context.
logVerboseAction<string, object[]>An action to log verbose messages.
contentConverterFunc<string, string>An optional function to convert the response content.
throwIfEmptyResponseboolIndicates whether to throw an exception if the response is empty.
Returns
- RestResponse<T>
The full RestSharp.RestResponse<> response.
Type Parameters
TThe 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
requestRestRequestThe RestRequest to invoke.
urlUriThe URI endpoint for the request.
callerobjectThe caller object for logging context.
logVerboseAction<string, object[]>An action to log verbose messages.
tokenCancellationTokenThe cancellation token.
contentConverterFunc<string, string>An optional function to convert the response content.
throwIfEmptyResponseboolIndicates 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
requestRestRequestThe RestRequest to invoke.
urlUriThe URI endpoint for the request.
callerobjectThe caller object for logging context.
logVerboseAction<string, object[]>An action to log verbose messages.
tokenCancellationTokenThe cancellation token.
contentConverterFunc<string, string>An optional function to convert the response content.
authIAuthenticatorAn optional authenticator for the request.
throwIfEmptyResponseboolIndicates 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
TThe 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
requestRestRequestThe RestRequest to invoke.
urlUriThe URI endpoint for the request.
callerobjectThe caller object for logging context.
logVerboseAction<string, object[]>An action to log verbose messages.
tokenCancellationTokenThe cancellation token.
contentConverterFunc<string, string>An optional function to convert the response content.
authIAuthenticatorAn optional authenticator for the request.
throwIfEmptyResponseboolIndicates whether to throw an exception if the response is empty.
handleErrorStatusFunc<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
TThe 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
requestRestRequestThe RestRequest to invoke.
urlUriThe URI endpoint for the request.
callerobjectThe caller object for logging context.
logVerboseAction<string, object[]>An action to log verbose messages.
tokenCancellationTokenThe cancellation token.
contentConverterFunc<string, string>An optional function to convert the response content.
throwIfEmptyResponseboolIndicates 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
TThe 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
requestRestRequestThe RestRequest to invoke.
urlUriThe URI endpoint for the request.
callerobjectThe caller object for logging context.
logVerboseAction<string, object[]>An action to log verbose messages.
contentConverterFunc<string, string>An optional function to convert the response content.
throwIfEmptyResponseboolIndicates whether to throw an exception if the response is empty.
Returns
- T
The deserialized response of type T.
Type Parameters
TThe 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
requestRestRequestThe RestRequest to modify.
filterFunc<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
clientRestRequestThe RestRequest to modify.
tokenSecureStringThe 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
responseRestResponseThe RestResponse instance.
messagestringAn 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
parametersIEnumerable<Parameter>The collection of parameters.
encodeValueboolIndicates whether the parameter values should be encoded.
Returns
- string
A query string representing the parameters.