Newer
Older
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public static class HttpHeaders
{
public static string BRANCH { get; } = "Branch";
public static string CLIENT { get; } = "Client";
public static string TOKEN { get; } = "Token";
public static string APIKEY { get; } = "Api-Key";
public static string MAINAPIKEY { get; } = "mApi-Key";
}
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
67
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();
}
#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),
};
#region HEADERS
protected void ResetHttpClientHeaders()
{
httpClient.DefaultRequestHeaders.Clear();
if (connectionOptions.ClientGuid != null) SetHeader(HttpHeaders.CLIENT, connectionOptions.ClientGuid);
if (connectionOptions.BranchGuid != null) SetHeader(HttpHeaders.BRANCH, connectionOptions.BranchGuid);
if (connectionOptions.Token != null) SetHeader(HttpHeaders.TOKEN, connectionOptions.Token);
if (connectionOptions.ApiKey != null) SetHeader(HttpHeaders.APIKEY, connectionOptions.ApiKey);
if (connectionOptions.MainApiKey != null) SetHeader(HttpHeaders.MAINAPIKEY, connectionOptions.MainApiKey);
}
public void ChangeClientGuid(string value)
{
}
public void ChangeClientGuid(Guid value)
{
ChangeHeader(HttpHeaders.CLIENT, value.ToString());
}
public void ChangeBranchGuid(string value)
{
}
public void ChangeBranchGuid(Guid value)
{
ChangeHeader(HttpHeaders.BRANCH, value.ToString());
}
public void ChangeToken(string value)
{
}
public void ChangeApiKey(string value)
{
}
public void ChangeMainApiKey(string value)
{
}
public void RemoveClientGuid()
{
RemoveHeader(HttpHeaders.CLIENT);
}
public void RemoveBranchGuid()
{
RemoveHeader(HttpHeaders.BRANCH);
}
public void RemoveToken()
{
RemoveHeader(HttpHeaders.TOKEN);
}
public void RemoveApiKey()
{
RemoveHeader(HttpHeaders.APIKEY);
}
public void RemoveMainApiKey()
{
RemoveHeader(HttpHeaders.MAINAPIKEY);
}
private void ChangeHeader(string header, string value)
{
RemoveHeader(header);
SetHeader(header, value);
}
private void SetHeader(string header, string value)
{
httpClient.DefaultRequestHeaders.Add(header, value);
}
private void RemoveHeader(string header)
{
httpClient.DefaultRequestHeaders.Remove(header);
}
#endregion