-
Notifications
You must be signed in to change notification settings - Fork 0
/
RConfigure.cs
107 lines (99 loc) · 4.04 KB
/
RConfigure.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
using JsonFormatterPlus;
using Newtonsoft.Json.Linq;
using Pretech.Software.RConfig.Crypto.Abstract;
using Pretech.Software.RConfig.Crypto.Concrete;
using Pretech.Software.RConfig.Crypto.Response;
using System.Net.Http.Json;
namespace Pretech.Software.RConfig.Crypto
{
public class RConfigure
{
private readonly HttpClient _client;
private IJsonCrypto _crypto;
private readonly string _secretKey;
private readonly string _decryptKey;
public RConfigure(string rconfigBaseUri, string secretKey, string decryptKey)
{
_client = new HttpClient();
_crypto = new JsonCrypto();
_client.BaseAddress = new Uri(rconfigBaseUri);
_secretKey = secretKey;
_decryptKey = decryptKey;
}
//public async Task<T> LoadAsync<T>()
//{
// try
// {
// var sectionName = typeof(T).Name;
// using (var client = _client)
// {
// var getJson = await _client.GetFromJsonAsync<RestResponse>($"{_client.BaseAddress}api/Config/RawJson/{_secretKey}");
// if (getJson.isSuccess)
// {
// JObject jsonObject = JObject.Parse(getJson.data.value);
// var configureTypeValue = (string)jsonObject[sectionName];
// var configureDecryptValue = await _crypto.DecryptAsync(configureTypeValue, _decryptKey);
// if (configureDecryptValue.IsSuccess)
// {
// return JsonConvert.DeserializeObject<T>(configureDecryptValue?.Data);
// }
// else return default(T);
// }
// else return default(T);
// }
// }
// catch (Exception ex)
// {
// return default(T);
// }
//}
public async Task<RestResponse> LoadJsonAsync()
{
RestResponse result = new();
try
{
using (var client = _client)
{
var getJson = await _client.GetFromJsonAsync<RestResponse>($"{_client.BaseAddress}api/Config/RawJson/{_secretKey}");
if (getJson.isSuccess)
{
JObject jsonObject = JObject.Parse(getJson.data.value);
HashSet<string> hashList = new();
foreach (JProperty property in jsonObject.Properties())
{
// item.Value<string>() kullanılarak JSON öğesinin değeri alınabilir.
var value = property.Value.ToString();
var decryptedValue = await _crypto.DecryptAsync(value, _decryptKey);
if (decryptedValue.IsSuccess)
{
hashList.Add($"\"{property.Name}\":{decryptedValue.Data}");
}
else
{
result.isSuccess = false;
result.message = decryptedValue.Message;
}
}
var json = string.Join(",", hashList);
json = JsonFormatter.Format("{" + "\n" + json + "\n" + "}");
result.isSuccess = true;
result.message = "";
result.data = new Data();
result.data.value = json;
}
else
{
result.isSuccess = false;
result.message = getJson.message;
}
}
}
catch (Exception ex)
{
result.isSuccess = false;
result.message = ex.Message;
}
return result;
}
}
}