Newer
Older
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace SDK
{
public abstract class DevkitConnector
{
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
protected readonly ConnectionOptions connectionOptions;
protected HttpClient httpClient;
public bool EnsureSuccessStatusCode { get; set; } = true;
public DevkitConnector(ConnectionOptions connectionOptions) : this(connectionOptions, new HttpClient())
{
}
public DevkitConnector(ConnectionOptions connectionOptions, HttpClient httpClient)
{
this.connectionOptions = connectionOptions;
this.httpClient = httpClient;
httpClient.BaseAddress = new Uri(connectionOptions.Url + "/" + connectionOptions.Version + "/");
ResetHttpClientHeaders();
}
protected void ResetHttpClientHeaders()
{
httpClient.DefaultRequestHeaders.Clear();
httpClient.DefaultRequestHeaders.Add("Client", connectionOptions.ClientGuid);
httpClient.DefaultRequestHeaders.Add("Branch", connectionOptions.BranchGuid);
httpClient.DefaultRequestHeaders.Add("Token", connectionOptions.Token);
httpClient.DefaultRequestHeaders.Add("Api-Key", connectionOptions.ApiKey);
}
#region REQUESTS
protected async Task<Type> GetRequest<Type>(string subUrl)
{
var response = await httpClient.GetAsync(subUrl);
return await JsonResponse<Type>(response);
}
protected async Task<Type> PostRequest<Type>(string subUrl, object body)
{
string bodyContent = JsonSerializer.Serialize(body);
HttpContent httpContent = new StringContent(bodyContent, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(subUrl, httpContent);
return await JsonResponse<Type>(response);
}
protected async Task<Type> PatchRequest<Type>(string subUrl, object body)
{
string bodyContent = JsonSerializer.Serialize(body);
HttpContent httpContent = new StringContent(bodyContent, Encoding.UTF8, "application/json");
var response = await httpClient.PatchAsync(subUrl, httpContent);
return await JsonResponse<Type>(response);
}
protected async Task PatchRequest(string subUrl, object body)
{
string bodyContent = JsonSerializer.Serialize(body);
HttpContent httpContent = new StringContent(bodyContent, Encoding.UTF8, "application/json");
var response = await httpClient.PatchAsync(subUrl, httpContent);
}
protected async Task<Type> DeleteRequest<Type>(string subUrl)
{
var response = await httpClient.DeleteAsync(subUrl);
return await JsonResponse<Type>(response);
}
protected async Task DeleteRequest(string subUrl)
{
var response = await httpClient.DeleteAsync(subUrl);
}
#endregion
protected async Task<Type> JsonResponse<Type>(HttpResponseMessage response)
{
if (EnsureSuccessStatusCode)
{
if (!response.IsSuccessStatusCode)
await HandleError(response);
}
else
{
if (!response.IsSuccessStatusCode)
}
var jsonString = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Type>(jsonString);
}
protected async Task EmptyResponse(HttpResponseMessage response)
if (!response.IsSuccessStatusCode)
await HandleError(response);
}
protected async Task HandleError(HttpResponseMessage response)
{
var stringContent = await response.Content.ReadAsStringAsync();
_ => throw new ServerResponseException(stringContent),
};