Newer
Older
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
namespace SDK
{
public abstract class DevkitConnector
{
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 string UrlCombine(params string[] items)
{
if (items?.Any() != true)
{
return string.Empty;
}
return string.Join("/", items.Where(u => !string.IsNullOrWhiteSpace(u)).Select(u => u.Trim('/', '\\')));
}
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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);
EmptyResponse(response);
}
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);
EmptyResponse(response);
}
#endregion
protected async Task<Type> JsonResponse<Type>(HttpResponseMessage response)
{
if (EnsureSuccessStatusCode)
{
if (!response.IsSuccessStatusCode)
await HandleError(response);
}
else
{
if (!response.IsSuccessStatusCode)
return default(Type);
}
var jsonString = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Type>(jsonString);
}
protected async Task EmptyResponse(HttpResponseMessage response)
115
116
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
if (!response.IsSuccessStatusCode)
await HandleError(response);
}
protected async Task HandleError(HttpResponseMessage response)
{
var stringContent = await response.Content.ReadAsStringAsync();
try
{
switch (response.StatusCode)
{
case System.Net.HttpStatusCode.BadRequest:
case System.Net.HttpStatusCode.Unauthorized:
case System.Net.HttpStatusCode.PaymentRequired:
case System.Net.HttpStatusCode.Forbidden:
case System.Net.HttpStatusCode.NotFound:
case System.Net.HttpStatusCode.MethodNotAllowed:
case System.Net.HttpStatusCode.InternalServerError:
var prettyJson = JsonSerializer.Serialize(stringContent, new JsonSerializerOptions()
{
WriteIndented = true
});
throw new ServerResponseException(prettyJson);
default:
throw new ServerResponseException(stringContent);
}
}
catch (Exception)
{
throw new ServerResponseException(stringContent);
}