-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBase.cs
339 lines (272 loc) · 12.8 KB
/
Base.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
namespace HRworksConnector
{
public static class RequestMethod
{
public const string Get = "GET";
public const string Post = "POST";
public const string Put = "PUT";
public const string Delete = "DELETE";
}
public interface IBase
{
System.Threading.Tasks.Task<T> SendRequestAsync<T>(string requestMethod, string target, string jsonBody, int timeoutSeconds);
}
public class Base : IBase
{
#region Consts
public const string Host = "api.hrworks.de";
private const string AuthenticationUrl = @"/v2/authentication";
#endregion
#region Privates
private static volatile System.Lazy<LoginHelper> lazyLoginHelper = new System.Lazy<LoginHelper>(delegate
{
return new LoginHelper();
});
/// <summary>
/// Threadsicheres Singleton
/// </summary>
private static LoginHelper Login
{
get
{
return lazyLoginHelper.Value;
}
}
private string accessKey = string.Empty;
private string secretAccessKey = string.Empty;
#endregion
#region Properties
public string AccessKey
{
get
{
return this.accessKey;
}
}
public string SecretAccessKey
{
get
{
return this.secretAccessKey;
}
}
#endregion
#region Constructor
public Base(string accessKey, string secretAccessKey)
{
this.accessKey = accessKey;
this.secretAccessKey = secretAccessKey;
}
#endregion
#region Methods
protected async System.Threading.Tasks.Task<T> PostAsync<T>(string target, Newtonsoft.Json.Linq.JObject json)
{
return await SendRequestAsync<T>(RequestMethod.Post, target, json.ToString(Newtonsoft.Json.Formatting.None));
}
protected async System.Threading.Tasks.Task<T> PostAsync<T>(string target, string jsonBody)
{
return await SendRequestAsync<T>(RequestMethod.Post, target, jsonBody);
}
protected async System.Threading.Tasks.Task<T> GetAsync<T>(string target)
{
return await SendRequestAsync<T>(RequestMethod.Get, target, string.Empty);
}
protected async System.Threading.Tasks.Task<T> GetAsync<T>(string target, string queryString)
{
return await SendRequestAsync<T>(RequestMethod.Get, target, queryString);
}
public async System.Threading.Tasks.Task<T> SendRequestAsync<T>(string requestMethod, string target, string jsonBody, int timeoutSeconds = 60)
{
System.Net.Http.HttpResponseMessage httpResponseMessage = await SendAsync(requestMethod, target, jsonBody, timeoutSeconds);
System.Net.Http.HttpContent requestContent = httpResponseMessage.Content;
string resultContent = requestContent.ReadAsStringAsync().Result;
#if DEBUG
try
{
string tempDirectory = System.IO.Path.GetTempPath();
string targetFilename = string.Format("hrworks-dbg-{0}.json", target.Replace("/", "_"));
string tempFilepath = System.IO.Path.Combine(tempDirectory, targetFilename);
string tmpContent = resultContent;
try
{
Newtonsoft.Json.Linq.JObject jsonResult = Newtonsoft.Json.Linq.JObject.Parse(tmpContent);
tmpContent = jsonResult.ToString(Newtonsoft.Json.Formatting.Indented);
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
System.IO.File.WriteAllText(tempFilepath, tmpContent);
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
#endif
try
{
httpResponseMessage.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
System.Net.Http.HttpRequestException httpRequestException = new System.Net.Http.HttpRequestException(resultContent, ex);
throw httpRequestException;
}
System.Threading.Tasks.Task<T> typedResponse = System.Threading.Tasks.Task.Factory.StartNew(() => Newtonsoft.Json.JsonConvert.DeserializeObject<T>(resultContent));
return await typedResponse;
}
public async System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(string requestMethod, string target, string jsonBody, int timeoutSeconds = 60)
{
System.DateTime now = System.DateTime.Now;
await this.ReNewTokenIfNecessary(timeoutSeconds);
if (string.IsNullOrEmpty(Login.AccessToken))
{
System.Net.Http.HttpResponseMessage httpResponseMessageNoApiKey = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
httpResponseMessageNoApiKey.ReasonPhrase = "No Token created.";
Newtonsoft.Json.Linq.JObject errorAsJson = new Newtonsoft.Json.Linq.JObject();
errorAsJson["message"] = httpResponseMessageNoApiKey.ReasonPhrase;
httpResponseMessageNoApiKey.Content = new System.Net.Http.StringContent(errorAsJson.ToString(Newtonsoft.Json.Formatting.None));
httpResponseMessageNoApiKey.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
return httpResponseMessageNoApiKey;
}
using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient())
{
TimeSpan timeout = TimeSpan.FromSeconds(timeoutSeconds);
string url = string.Format("https://{0}{1}", Host, target);
httpClient.BaseAddress = new Uri(url);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.Timeout = timeout;
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Login.AccessToken);
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Date = new System.DateTimeOffset(now);
System.Net.Http.StringContent body = new System.Net.Http.StringContent(jsonBody, Encoding.UTF8, "application/json");
System.Net.Http.HttpResponseMessage httpResponseMessage = null;
switch (requestMethod.ToUpper())
{
case RequestMethod.Post:
httpResponseMessage = await httpClient.PostAsync(url, body);
break;
case RequestMethod.Put:
httpResponseMessage = await httpClient.PutAsync(url, body);
break;
case RequestMethod.Get:
if (string.IsNullOrEmpty(jsonBody))
{
httpResponseMessage = await httpClient.GetAsync(url);
}
else
{
if (!jsonBody.StartsWith("?"))
{
jsonBody = "?" + jsonBody;
}
httpResponseMessage = await httpClient.GetAsync(url + jsonBody);
}
break;
case RequestMethod.Delete:
httpResponseMessage = await httpClient.DeleteAsync(url);
break;
}
return httpResponseMessage;
}
}
#endregion
#region Private Methods
private async System.Threading.Tasks.Task ReNewTokenIfNecessary(int timeoutSeconds = 60)
{
if (string.IsNullOrEmpty(Login.AccessToken) ||
System.DateTime.Now.Add(System.TimeSpan.FromSeconds(60)).Subtract(Login.AccessTokenExpire).TotalSeconds >= 0)
{
using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient())
{
TimeSpan timeout = TimeSpan.FromSeconds(timeoutSeconds);
try
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3 | System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
}
catch (Exception)
{
}
string authUrl = string.Format("https://{0}{1}", Host, AuthenticationUrl);
Newtonsoft.Json.Linq.JObject authAsJson = new Newtonsoft.Json.Linq.JObject();
authAsJson["accessKey"] = this.accessKey;
authAsJson["secretAccessKey"] = this.secretAccessKey;
System.Net.Http.StringContent bodyAuth = new System.Net.Http.StringContent(authAsJson.ToString(), Encoding.UTF8, "application/json");
System.Net.Http.HttpResponseMessage httpResponseMessageAuth = await httpClient.PostAsync(authUrl, bodyAuth);
httpResponseMessageAuth.EnsureSuccessStatusCode();
System.Net.Http.HttpContent httpContentAuth = httpResponseMessageAuth.Content;
string resultContent = httpContentAuth.ReadAsStringAsync().Result;
Newtonsoft.Json.Linq.JObject tokenAsJson = Newtonsoft.Json.Linq.JObject.Parse(resultContent);
string tmpAccessToken = tokenAsJson.GetValue("token", StringComparison.InvariantCultureIgnoreCase).ToString();
Login.LoadFromJwt(tmpAccessToken);
}
}
}
#endregion
#region Private Classes
private class LoginHelper
{
private string accessToken = string.Empty;
private System.DateTime accessTokenExpire = System.DateTime.MinValue;
private readonly System.TimeSpan DefaultAccessTokenTimeToLive = System.TimeSpan.FromMinutes(5);
public string AccessToken
{
get
{
return this.accessToken;
}
}
public System.DateTime AccessTokenExpire
{
get
{
return this.accessTokenExpire;
}
}
public void LoadFromJwt(string jwt)
{
if (string.IsNullOrEmpty(jwt))
{
return;
}
System.DateTime now = System.DateTime.Now;
this.accessToken = jwt;
this.accessTokenExpire = now.Add(DefaultAccessTokenTimeToLive);
try
{
// JWT decodieren
(JWTDecoder.JwtHeader Header, string Payload, string Verification) decodedToken = JWTDecoder.Decoder.DecodeToken(jwt);
if (!string.IsNullOrEmpty(decodedToken.Payload))
{
Newtonsoft.Json.Linq.JObject payloadAsJson = Newtonsoft.Json.Linq.JObject.Parse(decodedToken.Payload);
Newtonsoft.Json.Linq.JToken expireDateTimeAsJson = payloadAsJson.GetValue("exp", System.StringComparison.InvariantCultureIgnoreCase);
if (expireDateTimeAsJson != null)
{
long expireInSeconds = 0;
if (long.TryParse(expireDateTimeAsJson.ToString(), out expireInSeconds))
{
DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(expireInSeconds);
// wenn das "Verfallsdatum" größer als Now ist, dann merken.
if (dateTimeOffset.LocalDateTime.CompareTo(now) > 0)
{
this.accessTokenExpire = dateTimeOffset.LocalDateTime;
System.Console.WriteLine(string.Format("JWT expiration time: {0:dd.MM.yyyy HH:mm.ss}", this.accessTokenExpire));
}
}
}
}
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}
}
#endregion
}
}