Table of Contents

Class WebServerInvoker

Namespace
Geotab.Checkmate.Web
Assembly
Geotab.Checkmate.ObjectModel.dll

A class for communicating over the Internet to MyGeotab. This class is thread safe.

public class WebServerInvoker : IWebServerInvoker
Inheritance
WebServerInvoker
Implements
Derived
Inherited Members

Remarks

The maximum number of concurrent connections per server is 64, provided by the default HttpClientHandler. It can be increased/decreased by providing a custom instance of HttpMessageHandler.

Constructors

WebServerInvoker(string, int?)

Initializes a new instance of the WebServerInvoker class.

public WebServerInvoker(string url, int? timeout = null)

Parameters

url string

The URL.

timeout int?

The timeout in milliseconds or default 5 minutes.

WebServerInvoker(string, int?, HttpMessageHandler?, ApiClientConfiguration?)

Initializes a new instance of the WebServerInvoker class.

public WebServerInvoker(string url, int? timeout, HttpMessageHandler? overrideHttpMessageHandler, ApiClientConfiguration? apiClientConfiguration = null)

Parameters

url string

The URL.

timeout int?

The timeout in milliseconds or default 5 minutes.

overrideHttpMessageHandler HttpMessageHandler

The HttpMessageHandler for overriding of the default shared httpClient (max connection pool size 64).

apiClientConfiguration ApiClientConfiguration

The ApiClientConfiguration provided by the API client for custom configurations.

Fields

DefaultTimeOut

The default time out in milliseconds.

protected const int DefaultTimeOut = 300000

Field Value

int

KeyOfResponseJsonRpcErrorData

The key to access the JsonRpcErrorData from response exception

public const string KeyOfResponseJsonRpcErrorData = "JsonRpcErrorData"

Field Value

string

MaximumConnectionsPerServer

The maximum number of concurrent connections (per server endpoint) allowed when making requests using an HttpClient object The explicit connection pool size of 64

public const int MaximumConnectionsPerServer = 64

Field Value

int

Properties

ParallelDownloadSize

Gets or sets the size (in bytes) at which parallel downloading will be used. Value is 100GB.

public int ParallelDownloadSize { get; set; }

Property Value

int

size in bytes

SharedHttpClient

Gets the shared HttpClient.

protected virtual HttpClient SharedHttpClient { get; }

Property Value

HttpClient

The HttpClient.

Timeout

Gets the timeout.

public TimeSpan Timeout { get; }

Property Value

TimeSpan

The timeout.

Url

Gets the URL.

public string Url { get; }

Property Value

string

The URL.

Methods

CheckForError(string, Exception?, JsonRpcErrorCode?, JsonRpcErrorData?)

Check JSON error.

protected virtual Exception? CheckForError(string message, Exception? innerException, JsonRpcErrorCode? errorCode, JsonRpcErrorData? errorData)

Parameters

message string

The error message.

innerException Exception

The inner exception.

errorCode JsonRpcErrorCode?

The JsonRpcErrorCode for JSON-RPC error type that occurred.

errorData JsonRpcErrorData

Primitive or Structured value that contains additional information about the error.

Returns

Exception

The Exception object.

CreateParametersDictionary(params object?[])

Creates a dictionary from a set of provided parameter names and values.

public static Dictionary<string, object?> CreateParametersDictionary(params object?[] args)

Parameters

args object[]

An even number of alternating parameter names and values.

Returns

Dictionary<string, object>

A dictionary of parameters.

Exceptions

ArgumentException

There must be an even number of arguments to represent name and value pairs.

DownloadFileAsync(string, Stream, object?, int, CancellationToken)

Download a file to a stream.

public Task DownloadFileAsync(string method, Stream outputStream, object? parameters = null, int retryLimit = 3, CancellationToken cancellationToken = default)

Parameters

method string

The method to invoke.

outputStream Stream

The stream to which the binary data will be copied.

parameters object

Parameters to pass into the method: an array of objects in the order of the invoked method's parameter list, or IDictionary containing the named values to pass into the method, or an anonymous object with corresponding parameter values, for instance: new {username = "ivant@gmail.com", password = "123"}.

retryLimit int

The number of times the call will be retried. Use 1 for no retries.

cancellationToken CancellationToken

The CancellationToken used to signal that the request should be canceled.

Returns

Task

A Task.

GetJsonResponseAsync<T>(Dictionary<string, object>, string, CancellationToken, string?)

Make an API call and returns result of JToken.

protected virtual Task<JsonResponse<T>?> GetJsonResponseAsync<T>(Dictionary<string, object> jsonRequest, string requestUrl, CancellationToken cancellationToken, string? traceId = null)

Parameters

jsonRequest Dictionary<string, object>

API Call payload.

requestUrl string

Request url.

cancellationToken CancellationToken

Cancellation token.

traceId string

traceId of httpcontext

Returns

Task<JsonResponse<T>>

The JsonResponse<T>.

Type Parameters

T

The object type to deserialize to and return.

GetRpcError<T>(string, JsonResponse<T>?)

Generates an Exception based on the JSON-RPC response error.

protected virtual Exception? GetRpcError<T>(string method, JsonResponse<T>? jsonResponse)

Parameters

method string

The name of the api that is invoked by the invoker.

jsonResponse JsonResponse<T>

The JSON-RPC response from the api.

Returns

Exception

The Exception object, wraps an inner WebServerInvokerJsonException.

Type Parameters

T

The object type to deserialize to and return.

HttpDownloadFile(string, string, long?, object?)

Download a file.

[Obsolete("Use HttpDownloadFileAsync")]
public virtual void HttpDownloadFile(string method, string path, long? fileSize = null, object? parameters = null)

Parameters

method string

The method to invoke.

path string

The save location and file name.

fileSize long?

The size of the file to transfer. If known and if it is large the file will be downloaded in parallel streams.

parameters object

Parameters to pass into the method: an array of objects in the order of the invoked method's parameter list, or IDictionary containing the named values to pass into the method, or an anonymous object with corresponding parameter values, for instance: new {username = "ivant@gmail.com", password = "123"}.

Exceptions

ArgumentNullException

Path cannot be null.

HttpDownloadFileAsync(string, string?, long?, object?, in CancellationToken)

Download a file.

public Task HttpDownloadFileAsync(string method, string? path, long? fileSize = null, object? parameters = null, in CancellationToken cancellationToken = default)

Parameters

method string

The method to invoke.

path string

The save location and file name.

fileSize long?

The size of the file to transfer. If known and if it is large the file will be downloaded in parallel streams.

parameters object

Parameters to pass into the method: an array of objects in the order of the invoked method's parameter list, or IDictionary containing the named values to pass into the method, or an anonymous object with corresponding parameter values, for instance: new {username = "ivant@gmail.com", password = "123"}.

cancellationToken CancellationToken

The CancellationToken used to signal that the request should be cancelled.

Returns

Task

Task.

Exceptions

ArgumentNullException

Exception if path is null.

HttpUploadFileAsync<T>(string, string, string, string, object, in CancellationToken)

Upload a file to the specified URL.

public virtual Task<JsonResponse<T>?> HttpUploadFileAsync<T>(string uploadUrl, string file, string contentType, string method, object parameters, in CancellationToken cancellationToken = default)

Parameters

uploadUrl string

The URL.

file string

The file.

contentType string

Type of the content.

method string

The method.

parameters object

The parameters.

cancellationToken CancellationToken

The CancellationToken used to signal that the request should be cancelled.

Returns

Task<JsonResponse<T>>

The optional method results from the provided method.

Type Parameters

T

The object type to deserialize to and return.

HttpUploadFile<T>(string, string, string, string, object)

Upload a file to the specified URL.

[Obsolete("Use HttpUploadFileAsync rather")]
public JsonResponse<T>? HttpUploadFile<T>(string uploadUrl, string file, string contentType, string method, object parameters)

Parameters

uploadUrl string

The URL.

file string

The file.

contentType string

Type of the content.

method string

The method.

parameters object

The parameters.

Returns

JsonResponse<T>

The optional method results from the provided method.

Type Parameters

T

The object type to deserialize to and return.

InvokeAsync<T>(string, object?, CancellationToken)

Asynchronously invoke a call to the specified method.

public virtual Task<T?> InvokeAsync<T>(string method, object? parameters = null, CancellationToken cancellationToken = default)

Parameters

method string

Method to invoke.

parameters object

An anonymous object used as the parameters to pass into the method with corresponding parameter values, for instance: new {username = "ivant@gmail.com", password = "123"}.

cancellationToken CancellationToken

The CancellationToken used to signal that the request should be cancelled.

Returns

Task<T>

Result of the invoke operation converted to the return Type if provided.

Type Parameters

T

The object type to deserialize to and return.

InvokeAsync<T>(string, string?, object?, CancellationToken)

Asynchronously executes the method.

public Task<T?> InvokeAsync<T>(string method, string? traceId, object? parameters = null, CancellationToken cancellationToken = default)

Parameters

method string

Method to invoke.

traceId string

traceId of httpcontext

parameters object

Parameters to pass into the method: an array of objects in the order of the invoked method's parameter list, or IDictionary containing the named values to pass into the method, or an anonymous object with corresponding parameter values, for instance: new {username = "ivant@gmail.com", password = "123"}.

cancellationToken CancellationToken

The CancellationToken used to signal that the request should be cancelled.

Returns

Task<T>

The Task<TResult> containing the result of the invoke operation converted to the return Type if provided.

Type Parameters

T

The object type to deserialize to and return.

Invoke<T>(string, object?)

Synchronously executes the method.

[Obsolete("Sync code is not efficient - use await invoker.InvokeAsync(..")]
public T? Invoke<T>(string method, object? parameters = null)

Parameters

method string

Method to invoke.

parameters object

Parameters to pass into the method: an array of objects in the order of the invoked method's parameter list, or IDictionary containing the named values to pass into the method, or an anonymous object with corresponding parameter values, for instance: new {username = "ivant@gmail.com", password = "123"}.

Returns

T

Result of the invoke operation converted to the return Type if provided.

Type Parameters

T

The object type to deserialize to and return.

JsonToObjectAsync<T>(Stream)

Deserializes the JsonResponse<T> from the Stream.

protected virtual Task<JsonResponse<T>?> JsonToObjectAsync<T>(Stream contentStream)

Parameters

contentStream Stream

The stream to deserialize from.

Returns

Task<JsonResponse<T>>

The JsonResponse<T> from the Stream along with any errors.

Type Parameters

T

The T.

ThrowResponseFailed(HttpStatusCode, Exception, string?)

Throws new instance of the HttpRequestException class with inner ResponseFailException.

public static void ThrowResponseFailed(HttpStatusCode statusCode, Exception innerException, string? url = null)

Parameters

statusCode HttpStatusCode

The status code.

innerException Exception

The inner Exception.

url string

The url.

Exceptions

HttpRequestException

The instance of the HttpRequestException.

ArgumentNullException

The instance of the ArgumentNullException if innerException is undefined.

ToJson(object)

Convert object to JSON string.

protected virtual string ToJson(object obj)

Parameters

obj object

The object to be converted.

Returns

string

The JSON string result.

ToJsonContent(object)

Gets JsonContent wrapped serialization target.

protected virtual JsonContent ToJsonContent(object serializationTarget)

Parameters

serializationTarget object

The object for seralization.

Returns

JsonContent

A JsonContent.

UploadFileAsync(Stream, string, string, string, object, CancellationToken)

Upload a file.

public Task UploadFileAsync(Stream stream, string fileName, string contentType, string method, object parameters, CancellationToken cancellationToken = default)

Parameters

stream Stream

The Stream of binary data to upload.

fileName string

The file name including extension.

contentType string

Type of the content.

method string

The method.

parameters object

The parameters.

cancellationToken CancellationToken

The CancellationToken used to signal that the request should be cancelled.

Returns

Task

The optional method results from the provided method.