-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathRestRequest.cs
411 lines (319 loc) · 13.8 KB
/
RestRequest.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
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
using System.Net.Http;
using System.Net.Http.Headers;
using System;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Snowflake.Data.Core
{
internal interface IRestRequest
{
HttpRequestMessage ToRequestMessage(HttpMethod method);
TimeSpan GetRestTimeout();
string getSid();
}
/// <summary>
/// A base rest request implementation with timeout defined
/// </summary>
internal abstract class BaseRestRequest : IRestRequest
{
internal static string HTTP_REQUEST_TIMEOUT_KEY = "TIMEOUT_PER_HTTP_REQUEST";
internal static string REST_REQUEST_TIMEOUT_KEY = "TIMEOUT_PER_REST_REQUEST";
// The default Rest timeout. Set to 120 seconds.
public static int DEFAULT_REST_RETRY_SECONDS_TIMEOUT = 120;
internal Uri Url { get; set; }
/// <summary>
/// Timeout of the overall rest request
/// </summary>
internal TimeSpan RestTimeout { get; set; }
internal String sid { get; set; }
/// <summary>
/// Timeout for every single HTTP request
/// </summary>
internal TimeSpan HttpTimeout { get; set; }
HttpRequestMessage IRestRequest.ToRequestMessage(HttpMethod method)
{
throw new NotImplementedException();
}
protected HttpRequestMessage newMessage(HttpMethod method, Uri url)
{
HttpRequestMessage message = new HttpRequestMessage(method, url);
message.Properties[HTTP_REQUEST_TIMEOUT_KEY] = HttpTimeout;
message.Properties[REST_REQUEST_TIMEOUT_KEY] = RestTimeout;
return message;
}
TimeSpan IRestRequest.GetRestTimeout()
{
return RestTimeout;
}
string IRestRequest.getSid()
{
return sid;
}
}
internal class S3DownloadRequest : BaseRestRequest, IRestRequest
{
private const string SSE_C_ALGORITHM = "x-amz-server-side-encryption-customer-algorithm";
private const string SSE_C_KEY = "x-amz-server-side-encryption-customer-key";
private const string SSE_C_AES = "AES256";
internal string qrmk { get; set; }
internal Dictionary<string, string> chunkHeaders { get; set; }
HttpRequestMessage IRestRequest.ToRequestMessage(HttpMethod method)
{
HttpRequestMessage message = newMessage(method, Url);
if (chunkHeaders != null)
{
foreach (var item in chunkHeaders)
{
message.Headers.Add(item.Key, item.Value);
}
} else
{
message.Headers.Add(SSE_C_ALGORITHM, SSE_C_AES);
message.Headers.Add(SSE_C_KEY, qrmk);
}
return message;
}
}
internal class SFRestRequest : BaseRestRequest, IRestRequest
{
private static MediaTypeWithQualityHeaderValue applicationSnowflake = new MediaTypeWithQualityHeaderValue("application/snowflake");
private static MediaTypeWithQualityHeaderValue applicationJson = new MediaTypeWithQualityHeaderValue("application/json");
private const string SF_AUTHORIZATION_HEADER = "Authorization";
private const string SF_SERVICE_NAME_HEADER = "X-Snowflake-Service";
private const string ClientAppId = "CLIENT_APP_ID";
private const string ClientAppVersion = "CLIENT_APP_VERSION";
internal SFRestRequest() : base()
{
RestTimeout = TimeSpan.FromSeconds(DEFAULT_REST_RETRY_SECONDS_TIMEOUT);
// default each http request timeout to 16 seconds
HttpTimeout = TimeSpan.FromSeconds(16);
}
internal Object jsonBody { get; set; }
internal String authorizationToken { get; set; }
internal String serviceName { get; set; }
internal bool isPutGet { get; set; }
internal bool _isLogin { get; set; }
internal bool _isStatusRequest { get; set; }
public override string ToString()
{
return String.Format("SFRestRequest {{url: {0}, request body: {1} }}", Url.ToString(),
jsonBody.ToString());
}
HttpRequestMessage IRestRequest.ToRequestMessage(HttpMethod method)
{
var message = newMessage(method, Url);
if (method != HttpMethod.Get && jsonBody != null)
{
var json = JsonConvert.SerializeObject(jsonBody, JsonUtils.JsonSettings);
//TODO: Check if we should use other encodings...
message.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
message.Headers.Add(SF_AUTHORIZATION_HEADER, authorizationToken);
if (serviceName != null)
{
message.Headers.Add(SF_SERVICE_NAME_HEADER, serviceName);
}
// add quote otherwise it would be reported as error format
string osInfo = "(" + SFEnvironment.ClientEnv.osVersion + ")";
if (isPutGet || _isStatusRequest)
{
message.Headers.Accept.Add(applicationJson);
}
else
{
message.Headers.Accept.Add(applicationSnowflake);
}
if (_isLogin)
{
message.Headers.Add(ClientAppId, SFEnvironment.DriverName);
message.Headers.Add(ClientAppVersion, SFEnvironment.DriverVersion);
}
message.Headers.UserAgent.Add(new ProductInfoHeaderValue(SFEnvironment.DriverName, SFEnvironment.DriverVersion));
message.Headers.UserAgent.Add(new ProductInfoHeaderValue(osInfo));
message.Headers.UserAgent.Add(new ProductInfoHeaderValue(
SFEnvironment.ClientEnv.netRuntime,
SFEnvironment.ClientEnv.netVersion));
return message;
}
}
class AuthenticatorRequest
{
[JsonProperty(PropertyName = "data")]
internal AuthenticatorRequestData Data { get; set; }
public override string ToString()
{
return String.Format("AuthenticatorRequest {{data: {0} }}", Data.ToString());
}
}
class AuthenticatorRequestData
{
[JsonProperty(PropertyName = "ACCOUNT_NAME", NullValueHandling = NullValueHandling.Ignore)]
internal String AccountName { get; set; }
[JsonProperty(PropertyName = "AUTHENTICATOR")]
internal String Authenticator { get; set; }
[JsonProperty(PropertyName = "BROWSER_MODE_REDIRECT_PORT", NullValueHandling = NullValueHandling.Ignore)]
internal String BrowserModeRedirectPort { get; set; }
[JsonProperty(PropertyName = "CLIENT_APP_ID", NullValueHandling = NullValueHandling.Ignore)]
internal String DriverName { get; set; }
[JsonProperty(PropertyName = "CLIENT_APP_VERSION", NullValueHandling = NullValueHandling.Ignore)]
internal String DriverVersion { get; set; }
public override string ToString()
{
return String.Format("AuthenticatorRequestData {{ACCOUNT_NAME: {0} }}",
AccountName.ToString());
}
}
class LoginRequest
{
[JsonProperty(PropertyName = "data")]
internal LoginRequestData data { get; set; }
public override string ToString()
{
return String.Format("LoginRequest {{data: {0} }}", data.ToString());
}
}
class LoginRequestData
{
[JsonProperty(PropertyName = "CLIENT_APP_ID")]
internal String clientAppId { get; set; }
[JsonProperty(PropertyName = "CLIENT_APP_VERSION")]
internal String clientAppVersion { get; set; }
[JsonProperty(PropertyName = "ACCOUNT_NAME", NullValueHandling = NullValueHandling.Ignore)]
internal String accountName { get; set; }
[JsonProperty(PropertyName = "LOGIN_NAME", NullValueHandling = NullValueHandling.Ignore)]
internal String loginName { get; set; }
[JsonProperty(PropertyName = "PASSWORD", NullValueHandling = NullValueHandling.Ignore)]
internal String password { get; set; }
[JsonProperty(PropertyName = "AUTHENTICATOR", NullValueHandling = NullValueHandling.Ignore)]
internal String Authenticator { get; set; }
[JsonProperty(PropertyName = "CLIENT_ENVIRONMENT")]
internal LoginRequestClientEnv clientEnv { get; set; }
[JsonProperty(PropertyName = "RAW_SAML_RESPONSE", NullValueHandling = NullValueHandling.Ignore)]
internal String RawSamlResponse { get; set; }
[JsonProperty(PropertyName = "TOKEN", NullValueHandling = NullValueHandling.Ignore)]
internal string Token { get; set; }
[JsonProperty(PropertyName = "PROOF_KEY", NullValueHandling = NullValueHandling.Ignore)]
internal string ProofKey { get; set; }
[JsonProperty(PropertyName = "EXT_AUTHN_DUO_METHOD", NullValueHandling = NullValueHandling.Ignore)]
internal string extAuthnDuoMethod { get; set; }
[JsonProperty(PropertyName = "PASSCODE", NullValueHandling = NullValueHandling.Ignore)]
internal string passcode;
[JsonProperty(PropertyName = "SESSION_PARAMETERS", NullValueHandling = NullValueHandling.Ignore)]
internal Dictionary<SFSessionParameter, Object> SessionParameters { get; set; }
[JsonIgnore]
internal TimeSpan? HttpTimeout { get; set; }
public override string ToString()
{
return String.Format("LoginRequestData {{ClientAppVersion: {0},\n AccountName: {1},\n loginName: {2},\n ClientEnv: {3},\n authenticator: {4} }}",
clientAppVersion, accountName, loginName, clientEnv.ToString(), Authenticator);
}
}
class LoginRequestClientEnv
{
[JsonProperty(PropertyName = "APPLICATION")]
internal String application { get; set; }
[JsonProperty(PropertyName = "OS_VERSION")]
internal String osVersion { get; set; }
[JsonProperty(PropertyName = "NET_RUNTIME")]
internal String netRuntime { get; set; }
[JsonProperty(PropertyName = "NET_VERSION")]
internal string netVersion { get; set; }
[JsonProperty(PropertyName = "INSECURE_MODE")]
internal string insecureMode { get; set; }
[JsonIgnore]
internal bool IsNetFramework => netRuntime.Contains("NETFramework");
public override string ToString()
{
return String.Format("{{ APPLICATION: {0}, OS_VERSION: {1}, NET_RUNTIME: {2}, NET_VERSION: {3}, INSECURE_MODE: {4} }}",
application, osVersion, netRuntime, netVersion, insecureMode);
}
}
class QueryRequest
{
[JsonProperty(PropertyName = "sqlText")]
internal string sqlText { get; set; }
[JsonProperty(PropertyName = "describeOnly")]
internal bool describeOnly { get; set; }
[JsonProperty(PropertyName = "bindings")]
internal Dictionary<string, BindingDTO> parameterBindings { get; set; }
[JsonProperty(PropertyName = "bindStage")]
internal string bindStage { get; set; }
[JsonProperty(PropertyName = "parameters")]
internal Dictionary<string, string> parameters { get; set; }
[JsonProperty(PropertyName = "queryContextDTO", NullValueHandling = NullValueHandling.Ignore)]
internal RequestQueryContext QueryContextDTO { get; set; }
[JsonProperty(PropertyName = "asyncExec")]
internal bool asyncExec { get; set; }
}
// The query context in query response
internal class RequestQueryContext
{
[JsonProperty(PropertyName = "entries")]
internal List<RequestQueryContextElement> Entries { get; set; }
}
// The empty query context value in request
internal class QueryContextValueEmpty
{
// empty object with no filed
}
// The non-empty query context value in request
internal class QueryContextValue
{
// base64 encoded string of Opaque information
[JsonProperty(PropertyName = "base64Data")]
public string Base64Data { get; set; }
public QueryContextValue(string context)
{
Base64Data = context;
}
}
// The query context in query response
internal class RequestQueryContextElement
{
// database id as key. (bigint)
[JsonProperty(PropertyName = "id")]
public long Id { get; set; }
// When the query context read (bigint). Compare for same id.
[JsonProperty(PropertyName = "timestamp")]
public long ReadTimestamp { get; set; }
// Priority of the query context (bigint). Compare for different ids.
[JsonProperty(PropertyName = "priority")]
public long Priority { get; set; }
// Opaque information (object with a value of base64 encoded string).
[JsonProperty(PropertyName = "context")]
public object Context{ get; set; }
public void SetContext(string context)
{
if (context != null)
{
Context = new QueryContextValue(context);
}
else
{
Context = new QueryContextValueEmpty();
}
}
// default constructor for JSON converter
public RequestQueryContextElement() { }
public RequestQueryContextElement(QueryContextElement elem)
{
Id = elem.Id;
Priority = elem.Priority;
ReadTimestamp = elem.ReadTimestamp;
SetContext(elem.Context);
}
}
class QueryCancelRequest
{
[JsonProperty(PropertyName = "requestId")]
internal string requestId { get; set; }
}
class RenewSessionRequest
{
[JsonProperty(PropertyName = "oldSessionToken")]
internal string oldSessionToken { get; set; }
[JsonProperty(PropertyName = "requestType")]
internal string requestType { get; set; }
}
}