-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClientExtensions.cs
146 lines (124 loc) · 3.93 KB
/
ClientExtensions.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 Newtonsoft.Json;
using Pyrus.ApiClient.JsonConverters;
using Pyrus.ApiClient.Requests;
using Pyrus.ApiClient.Responses;
using PyrusApiClient;
using PyrusApiClient.Exceptions;
using System;
using System.Net;
using System.Threading.Tasks;
namespace Pyrus.ApiClient
{
internal static class ClientExtensions
{
private static readonly TimeSpan DefaultRetryTimeout = TimeSpan.FromMilliseconds(200);
public static async Task<TResponse> RunQuery<TResponse>(this PyrusClient client, Func<Task<MessageWithStatusCode>> action) where TResponse : ResponseBase
{
try
{
var result = default(TResponse);
for (var i = 0; i <= client.ClientSettings.RetryCount; i++)
{
if (i > 0)
await System.Threading.Tasks.Task.Delay(DefaultRetryTimeout);
var res = await action();
if (res == null)
continue;
if (typeof(TResponse) == typeof(DownloadResponse))
return CreateDownloadResponse<TResponse>(res);
if (typeof(TResponse) == typeof(FormRegisterResponse) && res.ToCsv)
return new FormRegisterResponse { Csv = res.Message } as TResponse;
try
{
result = JsonConvert.DeserializeObject<TResponse>(res.Message, new FormFieldJsonConverter());
}
catch
{
if (i == client.ClientSettings.RetryCount)
throw;
continue;
}
if (typeof(TResponse) == typeof(ResponseBase)
&& result is null
&& res.StatusCode == HttpStatusCode.OK)
return result;
if (result is null)
continue;
if (result.Error != null)
{
if (res.StatusCode != HttpStatusCode.Unauthorized || i == client.ClientSettings.RetryCount)
continue;
var isValidParameters = await client.GetTokenAsync();
if (!isValidParameters)
return result;
}
else
{
break;
}
}
return result;
}
catch (Exception e)
{
throw new PyrusApiClientException(e.Message, e);
}
}
private static TResponse CreateDownloadResponse<TResponse>(MessageWithStatusCode res) where TResponse : ResponseBase
{
var resp = new DownloadResponse
{
Content = res.Content,
FileName = res.FileName
};
if (res.StatusCode == HttpStatusCode.Forbidden || res.StatusCode == HttpStatusCode.Gone)
resp.ErrorCode = ErrorCodeType.AccessDeniedFile;
else if (res.StatusCode == HttpStatusCode.NotFound)
resp.ErrorCode = ErrorCodeType.FileIsMissing;
else if (res.StatusCode == HttpStatusCode.Unauthorized)
resp.ErrorCode = ErrorCodeType.AuthorizationError;
else if (res.StatusCode != HttpStatusCode.OK)
resp.ErrorCode = ErrorCodeType.ServerError;
return resp as TResponse;
}
private static async Task<bool> GetTokenAsync(this PyrusClient client)
{
try
{
var url = GetAuthUrl(client);
var response = await RequestHelper.PostRequest(client, url, new AuthRequest() { Login = client.Login, SecurityKey = client.SecretKey, PersonId = client.PersonId });
var result = JsonConvert.DeserializeObject<AuthResponse>(response.Message);
if (result.AccessToken == null)
return false;
client.Token = result.AccessToken;
SetOrigins(client, result);
return true;
}
catch (Exception e)
{
throw new PyrusApiClientException(e.Message, e);
}
}
internal static string GetAuthUrl(PyrusClient client)
{
// if is default API origin or custom auth origin
if (string.Equals(client.ClientSettings.Origin, Settings.PyrusOrigin)
|| !string.Equals(client.ClientSettings.AuthOrigin, Settings.PyrusAuthOrigin))
{
return client.ClientSettings.AuthOrigin + PyrusClient.AuthEndpoint;
}
return client.ClientSettings.Origin + PyrusClient.AuthEndpoint;
}
internal static void SetOrigins(PyrusClient client, AuthResponse response)
{
if (!string.IsNullOrEmpty(response.FilesUrl))
{
client.ClientSettings.FilesOrigin = response.FilesUrl;
}
if (!string.IsNullOrEmpty(response.ApiUrl))
{
client.ClientSettings.Origin = response.ApiUrl;
}
}
}
}