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),
};
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#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)
{
SetHeader(HttpHeaders.CLIENT, value);
}
public void ChangeClientGuid(Guid value)
{
SetHeader(HttpHeaders.CLIENT, value.ToString());
}
public void ChangeBranchGuid(string value)
{
SetHeader(HttpHeaders.BRANCH, value);
}
public void ChangeBranchGuid(Guid value)
{
SetHeader(HttpHeaders.BRANCH, value.ToString());
}
public void ChangeToken(string value)
{
SetHeader(HttpHeaders.TOKEN, value);
}
public void ChangeApiKey(string value)
{
SetHeader(HttpHeaders.APIKEY, value);
}
public void ChangeMainApiKey(string value)
{
SetHeader(HttpHeaders.MAINAPIKEY, 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 SetHeader(string header, string value)
{
httpClient.DefaultRequestHeaders.Add(header, value);
}
private void RemoveHeader(string header)
{
httpClient.DefaultRequestHeaders.Remove(header);
}
#endregion