RestSharpHelper
Provides helper methods to work with RestSharp requests and responses.
方法
public static void AddBodyAsStr(RestRequest request, string bodyStr)
RestSharpHelper.AddBodyAsStr(request, bodyStr)
Adds a string body to the request with JSON format.
- request
- The RestRequest to which the body will be added.
- bodyStr
- The string representation of the body.
public static IEnumerable<string> DecodeJWT(string jwt)
result = RestSharpHelper.DecodeJWT(jwt)
Decodes a JSON Web Token (JWT) into its component parts.
- jwt
- The JWT string.
返回值: An enumerable of strings representing the decoded parts of the JWT.
public static object Invoke(RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, Func<string, string> contentConverter, bool throwIfEmptyResponse)
result = RestSharpHelper.Invoke(request, url, caller, logVerbose, contentConverter, throwIfEmptyResponse)
Invokes the RestRequest synchronously.
- request
- The RestRequest to invoke.
- url
- The URI endpoint for the request.
- caller
- The caller object for logging context.
- logVerbose
- An action to log verbose messages.
- contentConverter
- An optional function to convert the response content.
- throwIfEmptyResponse
- Indicates whether to throw an exception if the response is empty.
返回值: The deserialized object response.
public static RestResponse<T> Invoke2<T>(RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, Func<string, string> contentConverter, bool throwIfEmptyResponse)
result = RestSharpHelper.Invoke2(request, url, caller, logVerbose, contentConverter, throwIfEmptyResponse)
Invokes the RestRequest synchronously and returns the full RestResponse of type T.
- request
- The RestRequest to invoke.
- url
- The URI endpoint for the request.
- caller
- The caller object for logging context.
- logVerbose
- An action to log verbose messages.
- contentConverter
- An optional function to convert the response content.
- throwIfEmptyResponse
- Indicates whether to throw an exception if the response is empty.
返回值: The full RestResponse response.
public static Task<object> InvokeAsync(RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, CancellationToken token, Func<string, string> contentConverter, bool throwIfEmptyResponse)
result = RestSharpHelper.InvokeAsync(request, url, caller, logVerbose, token, contentConverter, throwIfEmptyResponse)
Invokes the RestRequest asynchronously.
- request
- The RestRequest to invoke.
- url
- The URI endpoint for the request.
- caller
- The caller object for logging context.
- logVerbose
- An action to log verbose messages.
- token
- The cancellation token.
- contentConverter
- An optional function to convert the response content.
- throwIfEmptyResponse
- Indicates whether to throw an exception if the response is empty.
返回值: A task representing the asynchronous operation, returning the deserialized object response.
public static Task<RestResponse<T>> InvokeAsync2<T>(RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, CancellationToken token, Func<string, string> contentConverter, IAuthenticator auth, bool throwIfEmptyResponse)
result = RestSharpHelper.InvokeAsync2(request, url, caller, logVerbose, token, contentConverter, auth, throwIfEmptyResponse)
Asynchronously invokes the RestRequest and returns the full RestResponse response.
- request
- The RestRequest to invoke.
- url
- The URI endpoint for the request.
- caller
- The caller object for logging context.
- logVerbose
- An action to log verbose messages.
- token
- The cancellation token.
- contentConverter
- An optional function to convert the response content.
- auth
- An optional authenticator for the request.
- throwIfEmptyResponse
- Indicates whether to throw an exception if the response is empty.
返回值: A task representing the asynchronous operation, returning the full RestResponse response.
public static Task<RestResponse<T>> InvokeAsync3<T>(RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, CancellationToken token, Func<string, string> contentConverter, IAuthenticator auth, bool throwIfEmptyResponse, Func<HttpStatusCode, bool> handleErrorStatus)
result = RestSharpHelper.InvokeAsync3(request, url, caller, logVerbose, token, contentConverter, auth, throwIfEmptyResponse, handleErrorStatus)
Asynchronously invokes the RestRequest with extended error handling and returns the full RestResponse response.
- request
- The RestRequest to invoke.
- url
- The URI endpoint for the request.
- caller
- The caller object for logging context.
- logVerbose
- An action to log verbose messages.
- token
- The cancellation token.
- contentConverter
- An optional function to convert the response content.
- auth
- An optional authenticator for the request.
- throwIfEmptyResponse
- Indicates whether to throw an exception if the response is empty.
- handleErrorStatus
- An optional function to handle error HTTP status codes.
返回值: A task representing the asynchronous operation, returning the full RestResponse response.
public static Task<T> InvokeAsync<T>(RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, CancellationToken token, Func<string, string> contentConverter, bool throwIfEmptyResponse)
result = RestSharpHelper.InvokeAsync(request, url, caller, logVerbose, token, contentConverter, throwIfEmptyResponse)
Asynchronously invokes the RestRequest and returns a deserialized response of type T.
- request
- The RestRequest to invoke.
- url
- The URI endpoint for the request.
- caller
- The caller object for logging context.
- logVerbose
- An action to log verbose messages.
- token
- The cancellation token.
- contentConverter
- An optional function to convert the response content.
- throwIfEmptyResponse
- Indicates whether to throw an exception if the response is empty.
返回值: A task representing the asynchronous operation, returning the deserialized response of type T.
public static T Invoke<T>(RestRequest request, Uri url, object caller, Action<string, object[]> logVerbose, Func<string, string> contentConverter, bool throwIfEmptyResponse)
result = RestSharpHelper.Invoke(request, url, caller, logVerbose, contentConverter, throwIfEmptyResponse)
Invokes the RestRequest synchronously and returns a deserialized response of type T.
- request
- The RestRequest to invoke.
- url
- The URI endpoint for the request.
- caller
- The caller object for logging context.
- logVerbose
- An action to log verbose messages.
- contentConverter
- An optional function to convert the response content.
- throwIfEmptyResponse
- Indicates whether to throw an exception if the response is empty.
返回值: The deserialized response of type T.
public static void RemoveWhere(RestRequest request, Func<Parameter, bool> filter)
RestSharpHelper.RemoveWhere(request, filter)
Removes parameters from the request that match the specified filter.
- request
- The RestRequest to modify.
- filter
- The filter function to determine which parameters to remove.
public static RestRequest SetBearer(RestRequest client, SecureString token)
result = RestSharpHelper.SetBearer(client, token)
Adds a Bearer authorization header to the RestRequest.
- client
- The RestRequest to modify.
- token
- The secure token used for Bearer authentication.
返回值: The modified RestRequest.
public static RestSharpException ToError(RestResponse response, string message)
result = RestSharpHelper.ToError(response, message)
Converts a RestResponse into a RestSharpException with additional error details.
- response
- The RestResponse instance.
- message
- An optional error message.
返回值: A new instance of RestSharpException.
public static string ToQueryString(IEnumerable<Parameter> parameters, bool encodeValue)
result = RestSharpHelper.ToQueryString(parameters, encodeValue)
Converts a collection of parameters into a query string representation.
- parameters
- The collection of parameters.
- encodeValue
- Indicates whether the parameter values should be encoded.
返回值: A query string representing the parameters.