-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp_Connection_Handler.cs
More file actions
172 lines (133 loc) · 5.65 KB
/
Copy pathHttp_Connection_Handler.cs
File metadata and controls
172 lines (133 loc) · 5.65 KB
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
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
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using EZcade_Client.EzcadeDtos;
namespace EZcade_Client
{
public class HttpConnectionHandler
{
private readonly HttpClient _httpClient;
//private readonly string? _base_address = "http://192.168.1.236:8080/";
private readonly string? _base_address = "http://192.168.1.122:8080/";
//private readonly string? _base_address = "http://127.0.0.1:5218/";
private readonly string? _base_route = "api/ezcade_client/main/";
public readonly string? serverIp = "localhost";
public readonly string? serverPort = "5218";
private string? _token;
public bool ServerConnectionStatus { get; private set; }
public HttpConnectionHandler()
{
_httpClient = new HttpClient
{
BaseAddress = new Uri(_base_address)
};
}
public bool LoginAsync(string username, string password)
{
var request = new { Username = username, Password = password };
var content = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
var response = _httpClient.PostAsync("login", content).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
{
ServerConnectionStatus = false;
return false;
}
var json = response.Content.ReadAsStringAsync();
var root = JsonSerializer.Deserialize<JsonObject>(json.Result);
_token = root["myToken"]?.GetValue<string>();
if (!string.IsNullOrEmpty(_token))
{
_httpClient.DefaultRequestHeaders.Add("Authorization", _token);
ServerConnectionStatus = true;
return true;
}
ServerConnectionStatus = false;
return false;
}
public bool StatusCheckAsync()
{
var response = _httpClient.GetAsync(_base_route).Result;
ServerConnectionStatus = response.IsSuccessStatusCode;
return ServerConnectionStatus;
}
public ListDto<BatchDto> GetBatchListAsync_legacy()
{
var response = _httpClient.GetAsync(_base_route + "Batches").GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
{
ServerConnectionStatus = false;
return null;
}
var json = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var result = JsonSerializer.Deserialize<ListDto<BatchDto>>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return result;
}
public ListDto<string> GetComponentBatchListAsync()
{
var response = _httpClient.GetAsync(_base_route + "ComponentBatches").GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
{
ServerConnectionStatus = false;
return null;
}
var json = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var result = JsonSerializer.Deserialize<ListDto<string>>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return result;
}
public async Task<JsonObject?> SendSerialNumberWithBatchAsync(string serialInput, string? batch)
{
var payload = new JsonObject
{
["requestType"] = "serialNumber_withBatch",
["model"] = serialInput,
["batch"] = batch
};
var content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("api/server/serialNumber_withBatch", content);
if (!response.IsSuccessStatusCode)
return null;
string jsonResponse = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<JsonObject>(jsonResponse);
}
public SerialNumberDto GetSerialNumber(SerialNumberQuery Query, string batchName)
{
var request = new SerialNumberRequest
{
BatchName = batchName,
ProductModel = Query.ProductModel,
Type = Query.Type,
};
if (Query.PcbModel != null)
request.PcbModel = Query.PcbModel;
var content = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
var response = _httpClient.PostAsync(_base_route + "serialNumber", content).GetAwaiter().GetResult();
string res = response.ToString();
if (!response.IsSuccessStatusCode)
return new() ;
var resultJson = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var dto = JsonSerializer.Deserialize<SerialNumberDto>(resultJson, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return dto;
}
public bool Activation (string serialNumber, string fullType)
{
var url = $"{_base_route}activation/{fullType}/{serialNumber}";
var response = _httpClient.PostAsync(url, null).GetAwaiter().GetResult();
return response.IsSuccessStatusCode;
}
public void Cleanup()
{
_httpClient.Dispose();
}
}
}