1
|
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
|
|
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
|
public class HttpClient : IHttpClient{
|
18
|
|
19
|
/// <summary>
|
20
|
/// Instance of System.Net.Http.HttpClient
|
21
|
/// </summary>
|
22
|
private readonly System.Net.Http.HttpClient _httpClient;
|
23
|
|
24
|
/// <summary>
|
25
|
/// URL to which the HTTP client will connect
|
26
|
/// </summary>
|
27
|
private readonly string _uri;
|
28
|
|
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
|
public HttpClient(string uri) {
|
34
|
_httpClient = new System.Net.Http.HttpClient();
|
35
|
_uri = uri;
|
36
|
}
|
37
|
|
38
|
/// <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
|
public Task<HttpResponseMessage> PostAsJsonAsync(Payload payload) {
|
44
|
// Serialize the payload and send it to the server as JSON.
|
45
|
return _httpClient.PostAsJsonAsync(_uri, payload, new JsonSerializerOptions {
|
46
|
Converters = {
|
47
|
new JsonStringEnumConverter( JsonNamingPolicy.CamelCase)
|
48
|
}
|
49
|
});
|
50
|
|
51
|
}
|
52
|
}
|
53
|
}
|