Newer
Older
1
2
3
4
5
6
7
8
9
10
11
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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using SDK.Contracts.Communication;
using SDK.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using tDevkit;
namespace tDevkit
{
public partial class DevkitConnectorV3
{
private 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
private async Task<Type> GetRequest<Type>(string subUrl)
{
var task = await SendGetRequest(subUrl);
string responseString = await ProcessResponse(task);
return JsonSerializer.Deserialize<Type>(responseString);
}
private async Task<Type> PostRequest<Type>(string subUrl, object body)
{
string bodyContent = JsonSerializer.Serialize(body);
var task = await SendPostRequest(subUrl, bodyContent);
string responseString = await ProcessResponse(task);
return JsonSerializer.Deserialize<Type>(responseString);
}
private async Task<PatchResponseContract> PatchRequest(string subUrl, object body)
{
string bodyContent = JsonSerializer.Serialize(body);
var task = await SendPatchRequest(subUrl, bodyContent);
string responseString = await ProcessResponse(task);
return new PatchResponseContract();
}
private async Task<HttpResponseMessage> DeleteRequest(string subUrl)
{
var task = await SendDeleteRequest(subUrl);
string responseString = await ProcessResponse(task);
if (responseString == "")
{
return new HttpResponseMessage();
}
return JsonSerializer.Deserialize<HttpResponseMessage>(responseString);
}
#endregion
#region SEND REQUESTS
private async Task<HttpResponseMessage> SendGetRequest(string subUrl)
{
var task = await httpClient.GetAsync(baseAddress + subUrl);
return task;
}
private async Task<HttpResponseMessage> SendPostRequest(string subUrl, string bodyContent)
{
HttpContent httpContent = new StringContent(bodyContent, Encoding.UTF8, "application/json");
var task = await httpClient.PostAsync(baseAddress + subUrl, httpContent);
return task;
}
private async Task<HttpResponseMessage> SendPatchRequest(string subUrl, string bodyContent)
{
HttpContent httpContent = new StringContent(bodyContent, Encoding.UTF8, "application/json");
var task = await httpClient.PatchAsync(baseAddress + subUrl, httpContent);
return task;
}
private async Task<HttpResponseMessage> SendDeleteRequest(string subUrl)
{
var task = await httpClient.DeleteAsync(baseAddress + subUrl);
return task;
}
#endregion
private async Task<string> ProcessResponse(HttpResponseMessage response)
{
var responseString = await response.Content.ReadAsStringAsync();
switch (response.StatusCode)
{
case (System.Net.HttpStatusCode)400:
var responseA = JsonSerializer.Deserialize<ServerErrorResponseContract>(responseString);
throw new BadRequestException(BadRequestException.message + " " + responseA.Message);
case (System.Net.HttpStatusCode)401:
throw new NotAuthorizedException(NotAuthorizedException.message);
case (System.Net.HttpStatusCode)404:
ServerErrorResponseContract responseB;
try
{
responseB = JsonSerializer.Deserialize<ServerErrorResponseContract>(responseString);
}
catch (JsonException)
{
throw new NotFoundException(NotFoundException.message);
}
throw new NotFoundException(NotFoundException.message + " " + responseB.Message);
case (System.Net.HttpStatusCode)500:
throw new InternalServerErrorException(InternalServerErrorException.message);
}
return responseString;
}
}
}