Class WebServerInvoker
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
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
stringThe URL.
timeout
int?The timeout in milliseconds or default 5 minutes.
overrideHttpMessageHandler
HttpMessageHandlerThe HttpMessageHandler for overriding of the default shared httpClient (max connection pool size 64).
apiClientConfiguration
ApiClientConfigurationThe ApiClientConfiguration provided by the API client for custom configurations.
Fields
DefaultTimeOut
The default time out in milliseconds.
protected const int DefaultTimeOut = 300000
Field Value
KeyOfResponseJsonRpcErrorData
The key to access the JsonRpcErrorData from response exception
public const string KeyOfResponseJsonRpcErrorData = "JsonRpcErrorData"
Field Value
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
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
stringThe error message.
innerException
ExceptionThe inner exception.
errorCode
JsonRpcErrorCode?The JsonRpcErrorCode for JSON-RPC error type that occurred.
errorData
JsonRpcErrorDataPrimitive 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
stringThe method to invoke.
outputStream
StreamThe stream to which the binary data will be copied.
parameters
objectParameters 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
intThe number of times the call will be retried. Use 1 for no retries.
cancellationToken
CancellationTokenThe CancellationToken used to signal that the request should be canceled.
Returns
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
stringRequest url.
cancellationToken
CancellationTokenCancellation token.
traceId
stringtraceId 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
stringThe 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
stringThe method to invoke.
path
stringThe 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
objectParameters 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
stringThe method to invoke.
path
stringThe 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
objectParameters 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
CancellationTokenThe 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
stringThe URL.
file
stringThe file.
contentType
stringType of the content.
method
stringThe method.
parameters
objectThe parameters.
cancellationToken
CancellationTokenThe 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
stringThe URL.
file
stringThe file.
contentType
stringType of the content.
method
stringThe method.
parameters
objectThe 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
stringMethod to invoke.
parameters
objectAn 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
CancellationTokenThe CancellationToken used to signal that the request should be cancelled.
Returns
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
stringMethod to invoke.
traceId
stringtraceId of httpcontext
parameters
objectParameters 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
CancellationTokenThe 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
stringMethod to invoke.
parameters
objectParameters 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
StreamThe 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
HttpStatusCodeThe status code.
innerException
ExceptionThe inner Exception.
url
stringThe 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
objectThe 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
objectThe object for seralization.
Returns
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
StreamThe Stream of binary data to upload.
fileName
stringThe file name including extension.
contentType
stringType of the content.
method
stringThe method.
parameters
objectThe parameters.
cancellationToken
CancellationTokenThe CancellationToken used to signal that the request should be cancelled.
Returns
- Task
The optional method results from the provided method.