Class JsonClient
Represents a HttpClient with extended methods to use with JSON payloads and bearer tokens authentication.
Inherited Members
Namespace: Swan.Net
Syntax
public static class JsonClient
Methods
Authenticate(Uri, String, String, CancellationToken)
Authenticate against a web server using Bearer Token.
Declaration
public static Task<IDictionary<string, object>> Authenticate(Uri requestUri, string username, string password, CancellationToken ct = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| String | username | The username. |
| String | password | The password. |
| CancellationToken | ct | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<IDictionary<String, Object>> | A task with a Dictionary with authentication data. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | url or username. |
| SecurityException | Error Authenticating. |
Get<T>(Uri, IDictionary<String, IEnumerable<String>>, String, CancellationToken)
Gets the specified URL and return the JSON data as object with optional authorization token.
Declaration
public static Task<T> Get<T>(Uri requestUri, IDictionary<string, IEnumerable<string>> headers, string authorization = null, CancellationToken ct = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| IDictionary<String, IEnumerable<String>> | headers | The headers. |
| String | authorization | The authorization. |
| CancellationToken | ct | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<T> | A task with a result of the requested type. |
Type Parameters
| Name | Description |
|---|---|
| T | The response type. |
Get<T>(Uri, String, CancellationToken)
Gets the specified URL and return the JSON data as object with optional authorization token.
Declaration
public static Task<T> Get<T>(Uri requestUri, string authorization = null, CancellationToken ct = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| String | authorization | The authorization. |
| CancellationToken | ct | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<T> | A task with a result of the requested type. |
Type Parameters
| Name | Description |
|---|---|
| T | The response type. |
GetBinary(Uri, String, CancellationToken)
Gets the binary.
Declaration
public static Task<byte[]> GetBinary(Uri requestUri, string authorization = null, CancellationToken ct = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| String | authorization | The authorization. |
| CancellationToken | ct | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<Byte[]> | A task with a result of the requested byte array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | url. |
| JsonRequestException | Error GET Binary. |
GetString(Uri, IDictionary<String, IEnumerable<String>>, String, CancellationToken)
Gets the string.
Declaration
public static Task<string> GetString(Uri uri, IDictionary<string, IEnumerable<string>> headers, string authorization = null, CancellationToken ct = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | uri | The URI. |
| IDictionary<String, IEnumerable<String>> | headers | The headers. |
| String | authorization | The authorization. |
| CancellationToken | ct | The ct. |
Returns
| Type | Description |
|---|---|
| Task<String> | A task with a result of the requested string. |
GetString(Uri, String, CancellationToken)
Gets as string.
Declaration
public static Task<string> GetString(Uri requestUri, string authorization = null, CancellationToken ct = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| String | authorization | The authorization. |
| CancellationToken | ct | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<String> | A task with a result of the requested string. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | url. |
| JsonRequestException | Error GET JSON. |
Post(Uri, Object, String, CancellationToken)
Posts the specified URL.
Declaration
public static Task<IDictionary<string, object>> Post(Uri requestUri, object payload, string authorization = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| Object | payload | The payload. |
| String | authorization | The authorization. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<IDictionary<String, Object>> | A task with a result as a collection of key/value pairs. |
Post<T>(Uri, Object, String, CancellationToken)
Post a object as JSON with optional authorization token.
Declaration
public static Task<T> Post<T>(Uri requestUri, object payload, string authorization = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| Object | payload | The payload. |
| String | authorization | The authorization. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<T> | A task with a result of the requested type. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of response object. |
PostFile<T>(Uri, Byte[], String, String, CancellationToken)
Posts the file.
Declaration
public static Task<T> PostFile<T>(Uri requestUri, byte[] buffer, string fileName, string authorization = null, CancellationToken ct = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| Byte[] | buffer | The buffer. |
| String | fileName | Name of the file. |
| String | authorization | The authorization. |
| CancellationToken | ct | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<T> | A task with a result of the requested string. |
Type Parameters
| Name | Description |
|---|---|
| T | The response type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | buffer or fileName |
PostFileString(Uri, Byte[], String, String, CancellationToken)
Posts the file.
Declaration
public static Task<string> PostFileString(Uri requestUri, byte[] buffer, string fileName, string authorization = null, CancellationToken ct = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| Byte[] | buffer | The buffer. |
| String | fileName | Name of the file. |
| String | authorization | The authorization. |
| CancellationToken | ct | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<String> | A task with a result of the requested string. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | fileName |
PostString(Uri, Object, String, CancellationToken)
Posts the specified URL.
Declaration
public static Task<string> PostString(Uri requestUri, object payload, string authorization = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| Object | payload | The payload. |
| String | authorization | The authorization. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<String> | A task with a result of the requested string. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | url. |
| JsonRequestException | Error POST JSON. |
Put(Uri, Object, String, CancellationToken)
Puts the specified URL.
Declaration
public static Task<IDictionary<string, object>> Put(Uri requestUri, object payload, string authorization = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| Object | payload | The payload. |
| String | authorization | The authorization. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<IDictionary<String, Object>> | A task with a result of the requested collection of key/value pairs. |
Put<T>(Uri, Object, String, CancellationToken)
Puts the specified URL.
Declaration
public static Task<T> Put<T>(Uri requestUri, object payload, string authorization = null, CancellationToken ct = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| Object | payload | The payload. |
| String | authorization | The authorization. |
| CancellationToken | ct | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<T> | A task with a result of the requested type. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of response object. |
PutString(Uri, Object, String, CancellationToken)
Puts as string.
Declaration
public static Task<string> PutString(Uri requestUri, object payload, string authorization = null, CancellationToken ct = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| Uri | requestUri | The request URI. |
| Object | payload | The payload. |
| String | authorization | The authorization. |
| CancellationToken | ct | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<String> | A task with a result of the requested string. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | url. |
| JsonRequestException | Error PUT JSON. |
SendAsync(HttpMethod, Uri, Object, String, IDictionary<String, IEnumerable<String>>, CancellationToken)
Sends the asynchronous request.
Declaration
public static Task<string> SendAsync(HttpMethod method, Uri requestUri, object payload = null, string authorization = null, IDictionary<string, IEnumerable<string>> headers = null, CancellationToken ct = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| HttpMethod | method | The method. |
| Uri | requestUri | The request URI. |
| Object | payload | The payload. |
| String | authorization | The authorization. |
| IDictionary<String, IEnumerable<String>> | headers | The headers. |
| CancellationToken | ct | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<String> | A task with a result of the requested string. |
Exceptions
| Type | Condition |
|---|---|
| JsonRequestException | Error {method} JSON. |
| ArgumentNullException | requestUri. |