Repository navigation
Expand file tree
/
Copy pathRESTConnector.cs
More file actions
571 lines (510 loc) · 22 KB
/
Copy pathRESTConnector.cs
File metadata and controls
571 lines (510 loc) · 22 KB
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
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// uncomment to enable debugging
//#define ENABLE_DEBUGGING
using IBM.Watson.DeveloperCloud.Utilities;
using IBM.Watson.DeveloperCloud.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using UnityEngine;
#if UNITY_EDITOR
using System.Net;
using System.Net.Security;
#endif
namespace IBM.Watson.DeveloperCloud.Connection
{
/// <summary>
/// REST connector class.
/// </summary>
public class RESTConnector
{
#region Public Types
/// <summary>
/// This delegate type is declared for a Response handler function.
/// </summary>
/// <param name="req">The original request object.</param>
/// <param name="resp">The response object.</param>
public delegate void ResponseEvent(Request req, Response resp);
/// <summary>
/// This delegate is invoked to provide download progress.
/// </summary>
/// <param name="progress"></param>
public delegate void ProgressEvent(float progress);
/// <summary>
/// The class is returned by a Request object containing the response to a request made
/// by the client.
/// </summary>
public class Response
{
#region Public Properties
/// <summary>
/// True if the request was successful.
/// </summary>
public bool Success { get; set; }
/// <summary>
/// Error message if Success is false.
/// </summary>
public string Error { get; set; }
/// <summary>
/// The data returned by the request.
/// </summary>
public byte[] Data { get; set; }
/// <summary>
/// The amount of time in seconds it took to get this response from the server.
/// </summary>
public float ElapsedTime { get; set; }
#endregion
};
/// <summary>
/// Multi-part form data class.
/// </summary>
public class Form
{
/// <summary>
/// Make a multi-part form object from a string.
/// </summary>
/// <param name="s">The string data.</param>
public Form(string s)
{
IsBinary = false;
BoxedObject = s;
}
/// <summary>
/// Make a multi-part form object from an int.
/// </summary>
/// <param name="n">The int data.</param>
public Form(int n)
{
IsBinary = false;
BoxedObject = n;
}
/// <summary>
/// Make a multi-part form object from binary data.
/// </summary>
/// <param name="contents">The binary data.</param>
/// <param name="fileName">The filename of the binary data.</param>
/// <param name="mimeType">The mime type of the data.</param>
public Form(byte[] contents, string fileName = null, string mimeType = null)
{
IsBinary = true;
Contents = contents;
FileName = fileName;
MimeType = mimeType;
}
/// <summary>
/// True if the contained data is binary.
/// </summary>
public bool IsBinary { get; set; }
/// <summary>
/// The boxed POD data type, only set if IsBinary is false.
/// </summary>
public object BoxedObject { get; set; }
/// <summary>
/// If IsBinary is true, then this will contain the binary data.
/// </summary>
public byte[] Contents { get; set; }
/// <summary>
/// The filename of the binary data.
/// </summary>
public string FileName { get; set; }
/// <summary>
/// The Mime-Type of the binary data.
/// </summary>
public string MimeType { get; set; }
};
/// <summary>
/// This class is created to make a request to send to the server.
/// </summary>
public class Request
{
/// <summary>
/// Default constructor.
/// </summary>
public Request()
{
Parameters = new Dictionary<string, object>();
Headers = new Dictionary<string, string>();
}
#region Public Properties
/// <summary>
/// Custom timeout for this Request. This timeout is used if this timeout is larger than the value in the Config class.
/// </summary>
public float Timeout { get; set; }
/// <summary>
/// If true, then request will be cancelled.
/// </summary>
public bool Cancel { get; set; }
/// <summary>
/// True to send a delete method.
/// </summary>
public bool Delete { get; set; }
/// <summary>
/// The name of the function to invoke on the server.
/// </summary>
public string Function { get; set; }
/// <summary>
/// The parameters to pass to the function on the server.
/// </summary>
public Dictionary<string, object> Parameters { get; set; }
/// <summary>
/// Additional headers to provide in the request.
/// </summary>
public Dictionary<string, string> Headers { get; set; }
/// <summary>
/// The data to send through the connection. Do not use Forms if set.
/// </summary>
public byte[] Send { get; set; }
/// <summary>
/// Multi-part form data that needs to be sent. Do not use Send if set.
/// </summary>
public Dictionary<string, Form> Forms { get; set; }
/// <summary>
/// The callback that is invoked when a response is received.
/// </summary>
public ResponseEvent OnResponse { get; set; }
/// <summary>
/// This callback is invoked to provide progress on the WWW download.
/// </summary>
public ProgressEvent OnDownloadProgress { get; set; }
/// <summary>
/// This callback is invoked to provide upload progress.
/// </summary>
public ProgressEvent OnUploadProgress { get; set; }
#endregion
}
#endregion
#region Public Properties
private static float sm_LogResponseTime = 3.0f;
/// <summary>
/// Specify a time to log to the logging system when a response takes longer than this amount.
/// </summary>
public static float LogResponseTime { get { return sm_LogResponseTime; } set { sm_LogResponseTime = value; } }
/// <summary>
/// Base URL for REST requests.
/// </summary>
public string URL { get; set; }
/// <summary>
/// Credentials used to authenticate with the server.
/// </summary>
public Credentials Authentication { get; set; }
/// <summary>
/// Additional headers to attach to all requests.
/// </summary>
public Dictionary<string, string> Headers { get; set; }
#endregion
#region Private Data
//! Dictionary of connectors by service & function.
private static Dictionary<string, RESTConnector> sm_Connectors = new Dictionary<string, RESTConnector>();
#endregion
/// <summary>
/// This function returns a RESTConnector object for the given service and function.
/// </summary>
/// <param name="serviceID">The ID of the service.</param>
/// <param name="function">The name of the function.</param>
/// <param name="useCache">If true, then the connections will use a static cache.</param>
/// <returns>Returns a RESTConnector object or null on error.</returns>
public static RESTConnector GetConnector(string serviceID, string function, bool useCache = true)
{
RESTConnector connector = null;
string connectorID = serviceID + function;
if (useCache && sm_Connectors.TryGetValue(connectorID, out connector))
return connector;
Config cfg = Config.Instance;
Config.CredentialInfo cred = cfg.FindCredentials(serviceID);
if (cred == null)
{
Log.Error("Config", "Failed to find credentials for service {0}.", serviceID);
return null;
}
connector = new RESTConnector();
connector.URL = cred.m_URL + function;
if(cred.HasCredentials())
connector.Authentication = new Credentials(cred.m_User, cred.m_Password);
if (useCache)
sm_Connectors[connectorID] = connector;
return connector;
}
/// <summary>
/// Flush all connectors from the static pool.
/// </summary>
public static void FlushConnectors()
{
sm_Connectors.Clear();
}
#region Send Interface
/// <summary>
/// Send a request to the server. The request contains a callback that is invoked
/// when a response is received. The request may be queued if multiple requests are
/// made at once.
/// </summary>
/// <param name="request">The request object.</param>
/// <returns>true is returned on success, false is returned if the Request can't be sent.</returns>
public bool Send(Request request)
{
if (request == null)
throw new ArgumentNullException("request");
m_Requests.Enqueue(request);
// if we are not already running a co-routine to send the Requests
// then start one at this point.
if (m_ActiveConnections < Config.Instance.MaxRestConnections)
{
// This co-routine will increment m_ActiveConnections then yield back to us so
// we can return from the Send() as quickly as possible.
Runnable.Run(ProcessRequestQueue());
}
return true;
}
#endregion
#region Private Data
private int m_ActiveConnections = 0;
private Queue<Request> m_Requests = new Queue<Request>();
#endregion
#region Private Functions
private void AddHeaders(Dictionary<string, string> headers)
{
if (Authentication != null)
{
if (headers == null)
throw new ArgumentNullException("headers");
headers.Add("Authorization", Authentication.CreateAuthorization());
}
if (Headers != null)
{
foreach (var kp in Headers)
headers[kp.Key] = kp.Value;
}
headers.Add("User-Agent", Constants.String.VERSION);
}
private IEnumerator ProcessRequestQueue()
{
// yield AFTER we increment the connection count, so the Send() function can return immediately
m_ActiveConnections += 1;
#if UNITY_EDITOR
if (!UnityEditorInternal.InternalEditorUtility.inBatchMode)
yield return null;
#else
yield return null;
#endif
while (m_Requests.Count > 0)
{
Request req = m_Requests.Dequeue();
if (req.Cancel)
continue;
string url = URL;
if (!string.IsNullOrEmpty(req.Function))
url += req.Function;
StringBuilder args = null;
foreach (var kp in req.Parameters)
{
var key = kp.Key;
var value = kp.Value;
if (value is string)
value = WWW.EscapeURL((string)value); // escape the value
else if (value is byte[])
value = Convert.ToBase64String((byte[])value); // convert any byte data into base64 string
else if (value is Int32 || value is Int64 || value is UInt32 || value is UInt64)
value = value.ToString();
else if (value != null)
Log.Warning("RESTConnector", "Unsupported parameter value type {0}", value.GetType().Name);
else
Log.Error("RESTConnector", "Parameter {0} value is null", key);
if (args == null)
args = new StringBuilder();
else
args.Append("&"); // append separator
args.Append(key + "=" + value); // append key=value
}
if (args != null && args.Length > 0)
url += "?" + args.ToString();
AddHeaders(req.Headers);
Response resp = new Response();
DateTime startTime = DateTime.Now;
if (!req.Delete)
{
WWW www = null;
if (req.Forms != null)
{
if (req.Send != null)
Log.Warning("RESTConnector", "Do not use both Send & Form fields in a Request object.");
WWWForm form = new WWWForm();
try
{
foreach (var formData in req.Forms)
{
if (formData.Value.IsBinary)
form.AddBinaryData(formData.Key, formData.Value.Contents, formData.Value.FileName, formData.Value.MimeType);
else if (formData.Value.BoxedObject is string)
form.AddField(formData.Key, (string)formData.Value.BoxedObject);
else if (formData.Value.BoxedObject is int)
form.AddField(formData.Key, (int)formData.Value.BoxedObject);
else if (formData.Value.BoxedObject != null)
Log.Warning("RESTCOnnector", "Unsupported form field type {0}", formData.Value.BoxedObject.GetType().ToString());
}
foreach (var headerData in form.headers)
req.Headers[headerData.Key] = headerData.Value;
}
catch (Exception e)
{
Log.Error("RESTConnector", "Exception when initializing WWWForm: {0}", e.ToString());
}
www = new WWW(url, form.data, req.Headers);
}
else if (req.Send == null)
www = new WWW(url, null, req.Headers);
else
www = new WWW(url, req.Send, req.Headers);
#if ENABLE_DEBUGGING
Log.Debug("RESTCOnnector", "URL: {0}", url);
#endif
// wait for the request to complete.
float timeout = Mathf.Max(Config.Instance.TimeOut, req.Timeout);
while (!www.isDone)
{
if (req.Cancel)
break;
if ((DateTime.Now - startTime).TotalSeconds > timeout)
break;
if (req.OnUploadProgress != null)
req.OnUploadProgress(www.uploadProgress);
if (req.OnDownloadProgress != null)
req.OnDownloadProgress(www.progress);
#if UNITY_EDITOR
if (!UnityEditorInternal.InternalEditorUtility.inBatchMode)
yield return null;
#else
yield return null;
#endif
}
if (req.Cancel)
continue;
bool bError = false;
if (!string.IsNullOrEmpty(www.error))
{
int nErrorCode = -1;
int nSeperator = www.error.IndexOf(' ');
if (nSeperator > 0 && int.TryParse(www.error.Substring(0, nSeperator).Trim(), out nErrorCode))
bError = nErrorCode != 200;
if (bError)
Log.Error("RESTConnector", "URL: {0}, ErrorCode: {1}, Error: {2}, Response: {3}", url, nErrorCode, www.error,
string.IsNullOrEmpty(www.text) ? "" : www.text);
else
Log.Warning("RESTConnector", "URL: {0}, ErrorCode: {1}, Error: {2}, Response: {3}", url, nErrorCode, www.error,
string.IsNullOrEmpty(www.text) ? "" : www.text);
}
if (!www.isDone)
{
Log.Error("RESTConnector", "Request timed out for URL: {0}", url);
bError = true;
}
if (!bError && (www.bytes == null || www.bytes.Length == 0))
{
Log.Warning("RESTConnector", "No data recevied for URL: {0}", url);
bError = true;
}
// generate the Response object now..
if (!bError)
{
resp.Success = true;
resp.Data = www.bytes;
}
else
{
resp.Success = false;
resp.Error = string.Format("Request Error.\nURL: {0}\nError: {1}",
url, string.IsNullOrEmpty(www.error) ? "Timeout" : www.error);
}
resp.ElapsedTime = (float)(DateTime.Now - startTime).TotalSeconds;
// if the response is over a threshold, then log with status instead of debug
if (resp.ElapsedTime > LogResponseTime)
Log.Warning("RESTConnector", "Request {0} completed in {1} seconds.", url, resp.ElapsedTime);
if (req.OnResponse != null)
req.OnResponse(req, resp);
www.Dispose();
}
else
{
#if UNITY_EDITOR
float timeout = Mathf.Max(Config.Instance.TimeOut, req.Timeout);
DeleteRequest deleteReq = new DeleteRequest();
deleteReq.Send(url, req.Headers);
while (!deleteReq.IsComplete)
{
if (req.Cancel)
break;
if ((DateTime.Now - startTime).TotalSeconds > timeout)
break;
yield return null;
}
if (req.Cancel)
continue;
resp.Success = deleteReq.Success;
#else
Log.Warning( "RESTConnector", "DELETE method is supported in the editor only." );
resp.Success = false;
#endif
resp.ElapsedTime = (float)(DateTime.Now - startTime).TotalSeconds;
if (req.OnResponse != null)
req.OnResponse(req, resp);
}
}
// reduce the connection count before we exit..
m_ActiveConnections -= 1;
yield break;
}
#if UNITY_EDITOR
private class DeleteRequest
{
public string URL { get; set; }
public Dictionary<string, string> Headers { get; set; }
public bool IsComplete { get; set; }
public bool Success { get; set; }
public bool Send(string url, Dictionary<string, string> headers)
{
if (m_Thread != null && m_Thread.IsAlive)
return false;
URL = url;
Headers = new Dictionary<string, string>();
foreach (var kp in headers)
{
if (kp.Key != "User-Agent")
Headers[kp.Key] = kp.Value;
}
m_Thread = new Thread(ProcessRequest);
m_Thread.Start();
return true;
}
private Thread m_Thread = null;
private void ProcessRequest()
{
// This fixes the exception thrown by self-signed certificates.
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
WebRequest deleteReq = WebRequest.Create(URL);
foreach (var kp in Headers)
deleteReq.Headers.Add(kp.Key, kp.Value);
deleteReq.Method = "DELETE";
HttpWebResponse deleteResp = deleteReq.GetResponse() as HttpWebResponse;
Success = deleteResp.StatusCode == HttpStatusCode.OK;
IsComplete = true;
}
};
#endif
#endregion
}
}