1 |
afafbc22
|
Pultak
|
using System;
|
2 |
|
|
using System.Collections.Generic;
|
3 |
|
|
using System.Linq;
|
4 |
|
|
using System.Net.Http.Json;
|
5 |
|
|
using System.Text;
|
6 |
|
|
using System.Text.Json;
|
7 |
|
|
using System.Text.Json.Serialization;
|
8 |
|
|
using System.Threading.Tasks;
|
9 |
|
|
using LDClient.network.data;
|
10 |
|
|
|
11 |
|
|
namespace LDClient.network {
|
12 |
a155a763
|
silhavyj
|
|
13 |
|
|
/// <summary>
|
14 |
|
|
/// Implementation of IHttpClient which defines the functionality
|
15 |
|
|
/// of a HTTP client that is used by the API client to send data to the server.
|
16 |
|
|
/// </summary>
|
17 |
afafbc22
|
Pultak
|
public class HttpClient : IHttpClient{
|
18 |
|
|
|
19 |
a155a763
|
silhavyj
|
/// <summary>
|
20 |
|
|
/// Instance of System.Net.Http.HttpClient
|
21 |
|
|
/// </summary>
|
22 |
afafbc22
|
Pultak
|
private readonly System.Net.Http.HttpClient _httpClient;
|
23 |
|
|
|
24 |
a155a763
|
silhavyj
|
/// <summary>
|
25 |
|
|
/// URL to which the HTTP client will connect
|
26 |
|
|
/// </summary>
|
27 |
afafbc22
|
Pultak
|
private readonly string _uri;
|
28 |
a155a763
|
silhavyj
|
|
29 |
|
|
/// <summary>
|
30 |
|
|
/// Creates an instance of the class
|
31 |
|
|
/// </summary>
|
32 |
|
|
/// <param name="uri">URL to which the HTTP client will connect</param>
|
33 |
afafbc22
|
Pultak
|
public HttpClient(string uri) {
|
34 |
|
|
_httpClient = new System.Net.Http.HttpClient();
|
35 |
|
|
_uri = uri;
|
36 |
|
|
}
|
37 |
|
|
|
38 |
a155a763
|
silhavyj
|
/// <summary>
|
39 |
|
|
/// Asynchronically sends data in JSON format to the server.
|
40 |
|
|
/// </summary>
|
41 |
|
|
/// <param name="payload">Payload to be sent to the server</param>
|
42 |
|
|
/// <returns></returns>
|
43 |
afafbc22
|
Pultak
|
public Task<HttpResponseMessage> PostAsJsonAsync(Payload payload) {
|
44 |
a155a763
|
silhavyj
|
// Serialize the payload and send it to the server as JSON.
|
45 |
afafbc22
|
Pultak
|
return _httpClient.PostAsJsonAsync(_uri, payload, new JsonSerializerOptions {
|
46 |
|
|
Converters = {
|
47 |
|
|
new JsonStringEnumConverter( JsonNamingPolicy.CamelCase)
|
48 |
|
|
}
|
49 |
|
|
});
|
50 |
|
|
|
51 |
|
|
}
|
52 |
|
|
}
|
53 |
|
|
}
|