-
Notifications
You must be signed in to change notification settings - Fork 0
/
Web.cs
146 lines (138 loc) · 6.14 KB
/
Web.cs
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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace WinAMBurner
{
class Web
{
private const string TEST_URL = "http://qa.armentavet.co/";
private const string PRODUCTION_URL = "https://api.armentavet.co/";
private const string URL = PRODUCTION_URL;
private readonly HttpClient client = new HttpClient();
public async Task<JsonDocument> login<T>(T login, string entityUrl)
{
JsonDocument jsonDocument = null;
LoginJson rlogin = null;
try
{
LogFile.logWrite("Request POST: " + URL + entityUrl + "\n" + login.ToString() + JsonSerializer.Serialize(login));
client.DefaultRequestHeaders.Authorization = null;
HttpResponseMessage response = await client.PostAsync(URL + "api/p/login/",
new StringContent(JsonSerializer.Serialize(login), Encoding.UTF8, "application/json"));
if (response != null)
{
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
jsonDocument = await JsonSerializer.DeserializeAsync<JsonDocument>(await response.Content.ReadAsStreamAsync());
LogFile.logWrite(parseReply("Reply POST:\n", jsonDocument, response));
if (jsonDocument != null)
{
rlogin = JsonSerializer.Deserialize<Login>(jsonDocument.RootElement.ToString());
if (rlogin != null)
client.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue("JWT", rlogin.token);
}
}
}
LogFile.logWrite(parseReply("Reply POST:\n", jsonDocument, response));
}
catch (Exception e)
{
LogFile.logWrite(e.ToString());
}
return jsonDocument;
}
public async Task<T> entityGet<T>(string entityUrl)
{
Task<Stream> streamTask = null;
JsonDocument jsonDocument = null;
T entity = default(T);
try
{
LogFile.logWrite("Request GET: " + URL + entityUrl + "\n");
streamTask = client.GetStreamAsync(URL + entityUrl);
if(streamTask != null)
{
jsonDocument = await JsonSerializer.DeserializeAsync<JsonDocument>(await streamTask);
LogFile.logWrite(parseReply("Reply GET:\n", jsonDocument));
if (jsonDocument != null)
entity = JsonSerializer.Deserialize<T>(jsonDocument.RootElement.ToString());
}
LogFile.logWrite(parseReply("Reply GET:\n", jsonDocument));
}
catch (Exception e)
{
LogFile.logWrite(e.ToString());
}
return entity;
}
public async Task<JsonDocument> entityAdd<T>(T entity, string entityUrl)
{
JsonDocument jsonDocument = null;
try
{
LogFile.logWrite("Request POST: " + URL + entityUrl + "\n" + JsonSerializer.Serialize(entity));
HttpResponseMessage response = await client.PostAsync(URL + entityUrl,
new StringContent(JsonSerializer.Serialize(entity), Encoding.UTF8, "application/json"));
if (response != null)
jsonDocument = await JsonSerializer.DeserializeAsync<JsonDocument>(await response.Content.ReadAsStreamAsync());
LogFile.logWrite(parseReply("Reply POST:\n", jsonDocument, response));
}
catch (Exception e)
{
LogFile.logWrite(e.ToString());
}
return jsonDocument;
}
public async Task<JsonDocument> entityEdit<T>(T entity, string entityUrl)
{
JsonDocument jsonDocument = null;
try
{
LogFile.logWrite("Request PATCH: " + URL + entityUrl + "\n" + JsonSerializer.Serialize(entity));
HttpResponseMessage response = await client.PatchAsync(URL + entityUrl,
new StringContent(JsonSerializer.Serialize(entity), Encoding.UTF8, "application/json"));
if (response != null)
jsonDocument = await JsonSerializer.DeserializeAsync<JsonDocument>(await response.Content.ReadAsStreamAsync());
LogFile.logWrite(parseReply("Reply PATCH:\n", jsonDocument, response));
}
catch (Exception e)
{
LogFile.logWrite(e.ToString());
}
return jsonDocument;
}
public async Task<JsonDocument> getConstants()
{
JsonDocument jsonDocument = null;
try
{
LogFile.logWrite("Request OPTIONS: " + URL + "api/p/farms/" + "\n");
HttpResponseMessage response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Options, URL + "api/p/farms/"));
if (response != null)
jsonDocument = await JsonSerializer.DeserializeAsync<JsonDocument>(await response.Content.ReadAsStreamAsync());
LogFile.logWrite(parseReply("Reply OPTIONS:\n", jsonDocument, response));
}
catch (Exception e)
{
LogFile.logWrite(e.ToString());
}
return jsonDocument;
}
private string parseReply(string caption, JsonDocument jsonDocument, HttpResponseMessage response = null)
{
string logStr = caption;
if (response != null)
logStr += response.StatusCode + "\n" + response.Content.ReadAsStringAsync().Result + "\n";
if (jsonDocument != null)
logStr += jsonDocument.RootElement.ToString() + "\n";
return logStr;
}
}
}