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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
using Agger.Registro;
using Gestor.Common.Security;
using Gestor.Common.Validation;
using Gestor.Model.API;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace Gestor.Application.Helpers
{
public static class Connection
{
private const bool defaultUsaAzureStorage = false;
private const string defaultAzureStorage = "ad1";
public static string Server;
public static string Catalog;
public static string User;
public static string Password;
public static string Pool;
public static string Type;
public static Gestor.Model.API.ConnectionAddress ConnectionAddress
{
get;
set;
}
public static string LinkAggilizador
{
get;
set;
}
public static string UrlCentralSegurado
{
get;
set;
}
private static string AdApiV2(string storage)
{
if (Address.get_ApiAD().ToString().Contains("/api/v1"))
{
return string.Empty;
}
return storage;
}
internal static Uri Append(this Uri uri, params string[] paths)
{
return new Uri(paths.Aggregate<string, string>(uri.AbsoluteUri, (string current, string path) => string.Concat(current.TrimEnd(new char[] { '/' }), "/", path.TrimStart(new char[] { '/' }))));
}
public static string Base64Encode(this string plainText)
{
return (new Token()).Encrypt(plainText);
}
public static string BasicKey()
{
string numeroSerial = ApplicationHelper.NumeroSerial;
DateTime universalTime = Funcoes.GetNetworkTime().ToUniversalTime();
return string.Format("{0}:{1}", numeroSerial, universalTime.Ticks).Base64Encode();
}
public static async Task<bool> Delete(string command)
{
Uri uri = new Uri(Address.GestorApi(), command);
HttpClient httpClient = new HttpClient();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
httpClient.get_DefaultRequestHeaders().set_Authorization(new AuthenticationHeaderValue("Token", Gestor.Application.Helpers.Connection.BasicKey()));
if (await httpClient.DeleteAsync(uri).get_StatusCode() != HttpStatusCode.OK)
{
throw new Exception(string.Concat("Api connection Error ", Environment.NewLine, " ", command), null);
}
return true;
}
internal static async Task<bool> DeleteFile(string azureStorage, int ano, Guid id, string extensao, string path = "ad")
{
bool flag;
string str;
bool flag1;
str = (string.IsNullOrEmpty(azureStorage) ? "ad1" : azureStorage);
azureStorage = str;
Uri apiAD = Address.get_ApiAD();
string[] strArrays = new string[] { Gestor.Application.Helpers.Connection.AdApiV2(azureStorage) };
Uri uri = apiAD.Append(strArrays);
string[] strArrays1 = new string[] { path };
Uri uri1 = uri.Append(strArrays1);
string[] str1 = new string[] { ano.ToString() };
Uri uri2 = uri1.Append(str1);
string[] str2 = new string[] { id.ToString() };
Uri uri3 = uri2.Append(str2);
string[] strArrays2 = new string[] { extensao.Replace(".", string.Empty) };
Uri uri4 = uri3.Append(strArrays2);
try
{
using (HttpClient httpClient = new HttpClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
httpClient.get_DefaultRequestHeaders().Clear();
httpClient.get_DefaultRequestHeaders().Add("Authorization", string.Concat("Token ", Gestor.Application.Helpers.Connection.BasicKey()));
HttpResponseMessage httpResponseMessage = await httpClient.DeleteAsync(uri4);
flag1 = (!httpResponseMessage.get_IsSuccessStatusCode() ? false : httpResponseMessage.get_StatusCode() != HttpStatusCode.NoContent);
flag = flag1;
return flag;
}
}
catch
{
}
flag = false;
return flag;
}
internal static async Task<byte[]> DownloadFile(string azureStorage, int ano, Guid id, string extensao, string path = "ad", bool forceDownload = false)
{
byte[] numArray;
string str;
str = (string.IsNullOrEmpty(azureStorage) ? "ad1" : azureStorage);
azureStorage = str;
Uri apiAD = Address.get_ApiAD();
string[] strArrays = new string[] { Gestor.Application.Helpers.Connection.AdApiV2(azureStorage) };
Uri uri = apiAD.Append(strArrays);
string[] strArrays1 = new string[] { path };
Uri uri1 = uri.Append(strArrays1);
string[] str1 = new string[] { ano.ToString() };
Uri uri2 = uri1.Append(str1);
string[] str2 = new string[] { id.ToString() };
Uri uri3 = uri2.Append(str2);
string[] strArrays2 = new string[] { extensao.Replace(".", string.Empty) };
Uri uri4 = uri3.Append(strArrays2);
try
{
string str3 = string.Format("{0}{1}{2}", Path.GetTempPath(), id, extensao);
if (forceDownload || !File.Exists(str3))
{
using (HttpClient httpClient = new HttpClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
httpClient.get_DefaultRequestHeaders().Clear();
httpClient.get_DefaultRequestHeaders().Add("Authorization", string.Concat("Token ", Gestor.Application.Helpers.Connection.BasicKey()));
HttpResponseMessage async = await httpClient.GetAsync(uri4);
if (async.get_IsSuccessStatusCode() && async.get_StatusCode() != HttpStatusCode.NotFound)
{
numArray = await async.get_Content().ReadAsByteArrayAsync();
return numArray;
}
}
httpClient = null;
}
else
{
numArray = File.ReadAllBytes(str3);
return numArray;
}
}
catch
{
}
numArray = null;
return numArray;
}
public static string EncodeBase64(this string plainText)
{
string base64String;
try
{
base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(plainText));
}
catch
{
return string.Empty;
}
return base64String;
}
internal static async Task<double> EspacoUsadoAzureInBytes(string path = "")
{
double num;
Uri apiAD = Address.get_ApiAD();
string[] strArrays = new string[] { Gestor.Application.Helpers.Connection.AdApiV2(Gestor.Application.Helpers.Connection.ConnectionAddress.get_AzureStorage()) };
Uri uri = apiAD.Append(strArrays);
string[] strArrays1 = new string[] { "tamanho" };
Uri uri1 = uri.Append(strArrays1);
if (!string.IsNullOrEmpty(path))
{
string[] strArrays2 = new string[] { path };
uri1.Append(strArrays2);
}
try
{
using (HttpClient httpClient = new HttpClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
httpClient.get_DefaultRequestHeaders().Clear();
httpClient.get_DefaultRequestHeaders().Add("Authorization", string.Concat("Token ", Gestor.Application.Helpers.Connection.BasicKey()));
HttpResponseMessage async = await httpClient.GetAsync(uri1);
if (async.get_IsSuccessStatusCode() && async.get_StatusCode() != HttpStatusCode.NoContent)
{
num = double.Parse(await async.get_Content().ReadAsStringAsync());
return num;
}
}
httpClient = null;
}
catch
{
}
num = 0;
return num;
}
public static async Task<T> Get<T>(string command, bool autorizar = true, bool verificarConexao = false)
where T : class
{
Gestor.Application.Helpers.T t;
HttpResponseMessage httpResponseMessage;
Uri uri = new Uri(Address.GestorApi(), command);
HttpClient httpClient = new HttpClient();
if (autorizar)
{
httpClient.get_DefaultRequestHeaders().set_Authorization(new AuthenticationHeaderValue("Token", Gestor.Application.Helpers.Connection.BasicKey()));
}
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
try
{
ConfiguredTaskAwaitable<HttpResponseMessage> configuredTaskAwaitable = httpClient.GetAsync(uri).ConfigureAwait(false);
httpResponseMessage = await configuredTaskAwaitable;
}
catch (Exception exception)
{
ApplicationHelper.Conectado = false;
t = default(Gestor.Application.Helpers.T);
return t;
}
if (verificarConexao)
{
ApplicationHelper.Conectado = true;
}
if (httpResponseMessage.get_StatusCode() != HttpStatusCode.NoContent)
{
if (httpResponseMessage.get_StatusCode() != HttpStatusCode.OK)
{
throw new Exception(string.Concat("Api connection Error ", Environment.NewLine, " ", command), null);
}
t = JsonConvert.DeserializeObject<Gestor.Application.Helpers.T>(httpResponseMessage.get_Content().ReadAsStringAsync().Result);
}
else
{
t = default(Gestor.Application.Helpers.T);
}
return t;
}
public static async Task<HttpStatusCode> Get(string command, bool autorizar = true)
{
HttpStatusCode statusCode;
HttpResponseMessage httpResponseMessage;
Uri uri = new Uri(Address.GestorApi(), command);
HttpClient httpClient = new HttpClient();
if (autorizar)
{
httpClient.get_DefaultRequestHeaders().set_Authorization(new AuthenticationHeaderValue("Token", Gestor.Application.Helpers.Connection.BasicKey()));
}
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
try
{
ConfiguredTaskAwaitable<HttpResponseMessage> configuredTaskAwaitable = httpClient.GetAsync(uri).ConfigureAwait(false);
httpResponseMessage = await configuredTaskAwaitable;
}
catch (Exception exception)
{
statusCode = HttpStatusCode.InternalServerError;
return statusCode;
}
statusCode = httpResponseMessage.get_StatusCode();
return statusCode;
}
public static string GetAdConnection(string serial)
{
ApplicationHelper.NumeroSerial = serial;
if (string.IsNullOrWhiteSpace(Gestor.Application.Helpers.Connection.Server))
{
Gestor.Application.Helpers.Connection.GetAdConnection(serial);
}
return string.Concat(new string[] { "Server=", Gestor.Application.Helpers.Connection.Server, ";initial catalog=", Gestor.Application.Helpers.Connection.Catalog, "AD;user=", Gestor.Application.Helpers.Connection.User, ";password=", Gestor.Application.Helpers.Connection.Password, ";" });
}
public static string GetConnection(bool tryconnect = true)
{
string str;
string aggilizador;
string urlCentralSegurado;
try
{
Token token = new Token();
RegistryHelper registryHelper = new RegistryHelper(ApplicationHelper.Subkey);
bool usaAzureStorage = false;
string str1 = "ad1";
if (ApplicationHelper.Conectado)
{
List<Gestor.Model.API.ConnectionAddress> result = Gestor.Application.Helpers.Connection.Get<List<Gestor.Model.API.ConnectionAddress>>(string.Format("Server/{0}", ApplicationHelper.IdFornecedor), true, false).Result;
for (int i = 0; i < result.Count; i++)
{
string str2 = token.AggerEncrypt(token.Decrypt(result[i].get_Server()));
string str3 = token.AggerEncrypt(token.Decrypt(result[i].get_Catalog()));
string str4 = token.AggerEncrypt(token.Decrypt(result[i].get_User()));
string str5 = token.AggerEncrypt(token.Decrypt(result[i].get_Password()));
string str6 = token.AggerEncrypt(token.Decrypt(result[i].get_Pool()));
string str7 = result[i].get_Type().ToString();
if (string.IsNullOrEmpty(result[i].get_Aggilizador()))
{
aggilizador = null;
}
else
{
aggilizador = result[i].get_Aggilizador();
}
Gestor.Application.Helpers.Connection.LinkAggilizador = aggilizador;
if (string.IsNullOrEmpty(result[i].get_UrlCentralSegurado()))
{
urlCentralSegurado = null;
}
else
{
urlCentralSegurado = result[0].get_UrlCentralSegurado();
}
Gestor.Application.Helpers.Connection.UrlCentralSegurado = urlCentralSegurado;
usaAzureStorage = result[i].get_UsaAzureStorage();
str1 = (string.IsNullOrEmpty(result[i].get_AzureStorage()) ? "ad1" : token.Decrypt(result[i].get_AzureStorage()));
registryHelper.Write<string>(string.Format("SERVER{0}", i + 1), str2, true);
registryHelper.Write<string>(string.Format("DB{0}", i + 1), str3, true);
registryHelper.Write<string>(string.Format("USER{0}", i + 1), str4, true);
registryHelper.Write<string>(string.Format("PASSWORD{0}", i + 1), str5, true);
registryHelper.Write<string>(string.Format("POOL{0}", i + 1), str6, true);
registryHelper.Write<string>(string.Format("TYPE{0}", i + 1), str7, true);
}
}
string str8 = "";
for (int j = 0; j < 3 && registryHelper.Read(string.Format("SERVER{0}", j + 1), true) != null; j++)
{
if (registryHelper.Read(string.Format("TYPE{0}", j + 1), true) != "4")
{
Gestor.Application.Helpers.Connection.Server = token.AggerDecrypt(registryHelper.Read(string.Format("SERVER{0}", j + 1), true));
Gestor.Application.Helpers.Connection.Catalog = token.AggerDecrypt(registryHelper.Read(string.Format("DB{0}", j + 1), true));
Gestor.Application.Helpers.Connection.User = token.AggerDecrypt(registryHelper.Read(string.Format("USER{0}", j + 1), true));
Gestor.Application.Helpers.Connection.Password = token.AggerDecrypt(registryHelper.Read(string.Format("PASSWORD{0}", j + 1), true));
Gestor.Application.Helpers.Connection.Pool = token.AggerDecrypt(registryHelper.Read(string.Format("POOL{0}", j + 1), true));
Gestor.Application.Helpers.Connection.Type = registryHelper.Read(string.Format("TYPE{0}", j + 1), true);
if (!usaAzureStorage)
{
usaAzureStorage = Task.Run<bool>(async () => await Gestor.Application.Helpers.Connection.SaveInAzureStorage()).Result;
}
Gestor.Model.API.ConnectionAddress connectionAddress = new Gestor.Model.API.ConnectionAddress();
connectionAddress.set_Server(Gestor.Application.Helpers.Connection.Server);
connectionAddress.set_Catalog(Gestor.Application.Helpers.Connection.Catalog);
connectionAddress.set_User(Gestor.Application.Helpers.Connection.User);
connectionAddress.set_Password(Gestor.Application.Helpers.Connection.Password);
connectionAddress.set_Pool(Gestor.Application.Helpers.Connection.Pool);
connectionAddress.set_Aggilizador(Gestor.Application.Helpers.Connection.LinkAggilizador);
connectionAddress.set_UrlCentralSegurado(Gestor.Application.Helpers.Connection.UrlCentralSegurado);
connectionAddress.set_UsaAzureStorage(usaAzureStorage);
connectionAddress.set_AzureStorage(str1);
connectionAddress.set_Type(int.Parse((Gestor.Common.Validation.ValidationHelper.IsNotNullOrEmpty(Gestor.Application.Helpers.Connection.Type) ? "0" : Gestor.Application.Helpers.Connection.Type)));
Gestor.Application.Helpers.Connection.ConnectionAddress = connectionAddress;
string connectionString = Gestor.Application.Helpers.Connection.GetConnectionString();
if (!tryconnect || Gestor.Application.Helpers.Connection.TryConnect(connectionString, 1))
{
str8 = connectionString;
break;
}
}
}
str = str8;
}
catch (Exception exception)
{
str = null;
}
return str;
}
public static string GetConnectionString()
{
return string.Concat(new string[] { "Server=", Gestor.Application.Helpers.Connection.Server, ";initial catalog=", Gestor.Application.Helpers.Connection.Catalog, ";user=", Gestor.Application.Helpers.Connection.User, ";password=", Gestor.Application.Helpers.Connection.Password, ";" });
}
public static async Task<string> GetPool()
{
string pool;
Gestor.Model.API.ConnectionAddress connectionAddress = await Gestor.Application.Helpers.Connection.Get<Gestor.Model.API.ConnectionAddress>("Server/Pool", true, false);
if (connectionAddress != null)
{
Gestor.Application.Helpers.Connection.Pool = (new Token()).Decrypt(connectionAddress.get_Pool());
pool = Gestor.Application.Helpers.Connection.Pool;
}
else
{
pool = "";
}
return pool;
}
public static async Task<T> Post<T>(string command, T keyValues)
where T : class
{
Gestor.Application.Helpers.T t;
Uri uri = new Uri(Address.GestorApi(), command);
object obj = keyValues;
JsonSerializerSettings jsonSerializerSetting = new JsonSerializerSettings();
jsonSerializerSetting.set_ReferenceLoopHandling(1);
jsonSerializerSetting.set_MissingMemberHandling(0);
StringContent stringContent = new StringContent(JsonConvert.SerializeObject(obj, 1, jsonSerializerSetting), Encoding.UTF8, "application/json");
HttpClient httpClient = new HttpClient();
httpClient.get_DefaultRequestHeaders().set_Authorization(new AuthenticationHeaderValue("Token", Gestor.Application.Helpers.Connection.BasicKey()));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(uri, stringContent);
if (httpResponseMessage.get_StatusCode() != HttpStatusCode.NoContent)
{
if (httpResponseMessage.get_StatusCode() != HttpStatusCode.OK)
{
throw new Exception(string.Concat("Api connection Error ", Environment.NewLine, " ", command), null);
}
t = JsonConvert.DeserializeObject<Gestor.Application.Helpers.T>(httpResponseMessage.get_Content().ReadAsStringAsync().Result);
}
else
{
t = default(Gestor.Application.Helpers.T);
}
return t;
}
public static async Task<T> Post<T>(string command, object keyValues)
where T : class
{
Gestor.Application.Helpers.T t;
Uri uri = new Uri(Address.GestorApi(), command);
object obj = keyValues;
JsonSerializerSettings jsonSerializerSetting = new JsonSerializerSettings();
jsonSerializerSetting.set_ReferenceLoopHandling(1);
jsonSerializerSetting.set_MissingMemberHandling(0);
StringContent stringContent = new StringContent(JsonConvert.SerializeObject(obj, 1, jsonSerializerSetting), Encoding.UTF8, "application/json");
HttpClient httpClient = new HttpClient();
httpClient.get_DefaultRequestHeaders().set_Authorization(new AuthenticationHeaderValue("Token", Gestor.Application.Helpers.Connection.BasicKey()));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(uri, stringContent);
if (httpResponseMessage.get_StatusCode() != HttpStatusCode.NoContent)
{
if (httpResponseMessage.get_StatusCode() != HttpStatusCode.OK)
{
throw new Exception(string.Concat("Api connection Error ", Environment.NewLine, " ", command), null);
}
t = JsonConvert.DeserializeObject<Gestor.Application.Helpers.T>(httpResponseMessage.get_Content().ReadAsStringAsync().Result);
}
else
{
t = default(Gestor.Application.Helpers.T);
}
return t;
}
public static async Task<T> Put<T>(string command, T keyValues)
where T : class
{
Uri uri = new Uri(Address.GestorApi(), command);
object obj = keyValues;
JsonSerializerSettings jsonSerializerSetting = new JsonSerializerSettings();
jsonSerializerSetting.set_ReferenceLoopHandling(1);
StringContent stringContent = new StringContent(JsonConvert.SerializeObject(obj, 1, jsonSerializerSetting), Encoding.UTF8, "application/json");
HttpClient httpClient = new HttpClient();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
httpClient.get_DefaultRequestHeaders().set_Authorization(new AuthenticationHeaderValue("Token", Gestor.Application.Helpers.Connection.BasicKey()));
HttpResponseMessage httpResponseMessage = await httpClient.PutAsync(uri, stringContent);
if (httpResponseMessage.get_StatusCode() != HttpStatusCode.OK)
{
throw new Exception(string.Concat("Api connection Error ", Environment.NewLine, " ", command), null);
}
Gestor.Application.Helpers.T t = JsonConvert.DeserializeObject<Gestor.Application.Helpers.T>(httpResponseMessage.get_Content().ReadAsStringAsync().Result);
return t;
}
public static async Task<T> Put<T>(string command, object keyValues)
where T : class
{
Gestor.Application.Helpers.T t;
Uri uri = new Uri(Address.GestorApi(), command);
object obj = keyValues;
JsonSerializerSettings jsonSerializerSetting = new JsonSerializerSettings();
jsonSerializerSetting.set_ReferenceLoopHandling(1);
StringContent stringContent = new StringContent(JsonConvert.SerializeObject(obj, 1, jsonSerializerSetting), Encoding.UTF8, "application/json");
HttpClient httpClient = new HttpClient();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
httpClient.get_DefaultRequestHeaders().set_Authorization(new AuthenticationHeaderValue("Token", Gestor.Application.Helpers.Connection.BasicKey()));
HttpResponseMessage httpResponseMessage = await httpClient.PutAsync(uri, stringContent);
if (httpResponseMessage.get_StatusCode() != HttpStatusCode.NoContent)
{
if (httpResponseMessage.get_StatusCode() != HttpStatusCode.OK)
{
throw new Exception(string.Concat("Api connection Error ", Environment.NewLine, " ", command), null);
}
t = JsonConvert.DeserializeObject<Gestor.Application.Helpers.T>(httpResponseMessage.get_Content().ReadAsStringAsync().Result);
}
else
{
t = default(Gestor.Application.Helpers.T);
}
return t;
}
public static async void PutError<T>(T keyValues)
where T : class
{
Uri uri = new Uri(Address.GestorApi(), "Error/Objeto");
object obj = keyValues;
JsonSerializerSettings jsonSerializerSetting = new JsonSerializerSettings();
jsonSerializerSetting.set_ReferenceLoopHandling(1);
StringContent stringContent = new StringContent(JsonConvert.SerializeObject(obj, 1, jsonSerializerSetting), Encoding.UTF8, "application/json");
HttpClient httpClient = new HttpClient();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
httpClient.get_DefaultRequestHeaders().set_Authorization(new AuthenticationHeaderValue("Token", Gestor.Application.Helpers.Connection.BasicKey()));
if (await httpClient.PutAsync(uri, stringContent).get_StatusCode() != HttpStatusCode.NoContent)
{
throw new Exception(string.Concat("Api connection Error ", Environment.NewLine, " Error"), null);
}
}
internal static async Task<bool> SaveInAzureStorage()
{
bool flag;
string str = string.Format("{0}Data", Address.GestorApi());
try
{
using (HttpClient httpClient = new HttpClient())
{
httpClient.get_DefaultRequestHeaders().Clear();
httpClient.get_DefaultRequestHeaders().Add("Authorization", string.Concat("Token ", Gestor.Application.Helpers.Connection.BasicKey()));
HttpResponseMessage async = await httpClient.GetAsync(str);
if (async.get_IsSuccessStatusCode())
{
flag = await async.get_Content().ReadAsStringAsync() == "true";
return flag;
}
}
httpClient = null;
}
catch
{
}
flag = false;
return flag;
}
internal static bool TryConnect(string connectionString, int retryCount = 1)
{
bool flag;
List<Exception> exceptions = new List<Exception>();
int num = 0;
Label1:
while (num < retryCount)
{
try
{
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
}
flag = true;
}
catch (Exception exception)
{
exceptions.Add(exception);
goto Label0;
}
return flag;
}
return exceptions.Count == 0;
Label0:
num++;
goto Label1;
}
internal static async Task<bool> UploadFile(UploadFile upload, string path = "ad")
{
bool flag;
bool flag1;
Uri apiAD = Address.get_ApiAD();
string[] strArrays = new string[] { Gestor.Application.Helpers.Connection.AdApiV2(Gestor.Application.Helpers.Connection.ConnectionAddress.get_AzureStorage()) };
Uri uri = apiAD.Append(strArrays);
string[] strArrays1 = new string[] { path };
Uri uri1 = uri.Append(strArrays1);
try
{
using (HttpClient httpClient = new HttpClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
httpClient.get_DefaultRequestHeaders().Clear();
httpClient.get_DefaultRequestHeaders().Add("Authorization", string.Concat("Token ", Gestor.Application.Helpers.Connection.BasicKey()));
HttpResponseMessage httpResponseMessage = await httpClient.PutAsync(uri1, new StringContent(JsonConvert.SerializeObject(upload), Encoding.UTF8, "application/json"));
flag1 = (!httpResponseMessage.get_IsSuccessStatusCode() ? false : httpResponseMessage.get_StatusCode() != HttpStatusCode.NoContent);
flag = flag1;
return flag;
}
}
catch
{
}
flag = false;
return flag;
}
}
}
|