Skip to content

Commit 7f8a0b3

Browse files
committed
chore: Refactored Assistant V2 to use new WatsonResponse and WatsonError
1 parent f16890a commit 7f8a0b3

File tree

126 files changed

+782
-303
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+782
-303
lines changed
File renamed without changes.
File renamed without changes.

Scripts/Services/Assistant/v2/Assistant.cs renamed to Assistant/v2/Assistant.cs

Lines changed: 128 additions & 166 deletions
Large diffs are not rendered by default.
File renamed without changes.

Examples/ServiceExamples.meta renamed to Core.meta

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

Scripts/Connection/RESTConnector.cs renamed to Core/Connection/RESTConnector.cs

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class Response
7272
/// <summary>
7373
/// Error message if Success is false.
7474
/// </summary>
75-
public Error Error { get; set; }
75+
public WatsonError Error { get; set; }
7676
/// <summary>
7777
/// The data returned by the request.
7878
/// </summary>
@@ -91,41 +91,7 @@ public class Response
9191
public Dictionary<string, string> Headers { get; set; }
9292
#endregion
9393
};
94-
95-
/// <summary>
96-
/// Class to encapsulate an error returned from a server request.
97-
/// </summary>
98-
public class Error
99-
{
100-
/// <summary>
101-
/// The url that generated the error.
102-
/// </summary>
103-
public string URL { get; set; }
104-
/// <summary>
105-
/// The error code returned from the server.
106-
/// </summary>
107-
public long ErrorCode { get; set; }
108-
/// <summary>
109-
/// The error message returned from the server.
110-
/// </summary>
111-
public string ErrorMessage { get; set; }
112-
/// <summary>
113-
/// The contents of the response from the server.
114-
/// </summary>
115-
public string Response { get; set; }
116-
/// <summary>
117-
/// Dictionary of headers returned by the request.
118-
/// </summary>
119-
public Dictionary<string, string> ResponseHeaders { get; set; }
120-
121-
public override string ToString()
122-
{
123-
return string.Format("URL: {0}, ErrorCode: {1}, Error: {2}, Response: {3}", URL, ErrorCode,
124-
string.IsNullOrEmpty(ErrorMessage) ? "" : ErrorMessage,
125-
string.IsNullOrEmpty(Response) ? "" : Response);
126-
}
127-
}
128-
94+
12995
/// <summary>
13096
/// Multi-part form data class.
13197
/// </summary>
@@ -297,9 +263,11 @@ public bool DisableSslVerification
297263

298264
public static RESTConnector GetConnector(Credentials credentials, string function)
299265
{
300-
RESTConnector connector = new RESTConnector();
301-
connector.URL = credentials.Url + function;
302-
connector.Authentication = credentials;
266+
RESTConnector connector = new RESTConnector
267+
{
268+
URL = credentials.Url + function,
269+
Authentication = credentials
270+
};
303271
if (connector.Authentication.HasIamTokenData())
304272
{
305273
connector.Authentication.GetToken();
@@ -507,17 +475,22 @@ private IEnumerator ProcessRequestQueue()
507475
}
508476
else if (req.Send != null)
509477
{
510-
unityWebRequest = new UnityWebRequest(url, req.HttpMethod);
511-
unityWebRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(req.Send);
478+
unityWebRequest = new UnityWebRequest(url, req.HttpMethod)
479+
{
480+
uploadHandler = (UploadHandler)new UploadHandlerRaw(req.Send)
481+
};
482+
512483
unityWebRequest.SetRequestHeader("Content-Type", "application/json");
513484
}
514485
}
515486
else
516487
{
517488
// GET, DELETE and POST without data
518-
unityWebRequest = new UnityWebRequest();
519-
unityWebRequest.url = url;
520-
unityWebRequest.method = req.HttpMethod;
489+
unityWebRequest = new UnityWebRequest
490+
{
491+
url = url,
492+
method = req.HttpMethod
493+
};
521494
}
522495

523496
foreach (KeyValuePair<string, string> kvp in req.Headers)
@@ -582,7 +555,7 @@ private IEnumerator ProcessRequestQueue()
582555
}
583556

584557
bool bError = false;
585-
Error error = null;
558+
WatsonError error = null;
586559
if (!string.IsNullOrEmpty(unityWebRequest.error))
587560
{
588561
switch (unityWebRequest.responseCode)
@@ -597,10 +570,10 @@ private IEnumerator ProcessRequestQueue()
597570
break;
598571
}
599572

600-
error = new Error()
573+
error = new WatsonError()
601574
{
602-
URL = url,
603-
ErrorCode = unityWebRequest.responseCode,
575+
Url = url,
576+
StatusCode = unityWebRequest.responseCode,
604577
ErrorMessage = unityWebRequest.error,
605578
Response = unityWebRequest.downloadHandler.text,
606579
ResponseHeaders = unityWebRequest.GetResponseHeaders()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Core/Editor/Help/Working/MRefBuilder.config.meta

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Scripts/Utilities/Credentials.cs renamed to Core/Utilities/Credentials.cs

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,9 @@ public bool DisableSslVerification
146146
/// Success callback delegate.
147147
/// </summary>
148148
/// <typeparam name="T">Type of the returned object.</typeparam>
149-
/// <param name="response">The returned object.</param>
149+
/// <param name="response">The returned WatsonResponse.</param>
150150
/// <param name="customData">user defined custom data including raw json.</param>
151-
public delegate void SuccessCallback<T>(T response, Dictionary<string, object> customData);
152-
153-
/// <summary>
154-
/// Fail callback delegate.
155-
/// </summary>
156-
/// <param name="error">The error object.</param>
157-
/// <param name="customData">User defined custom data</param>
158-
public delegate void FailCallback(RESTConnector.Error error, Dictionary<string, object> customData);
151+
public delegate void Callback<T>(WatsonResponse<T> response, WatsonError error, Dictionary<string, object> customData);
159152
#endregion
160153

161154
#region Constructors
@@ -260,59 +253,53 @@ public void GetToken()
260253
if (!string.IsNullOrEmpty(_userAcessToken))
261254
{
262255
// 1. use user-managed token
263-
OnGetToken(new IamTokenData() { AccessToken = _userAcessToken }, new Dictionary<string, object>());
256+
OnGetToken(new WatsonResponse<IamTokenData>() { Result = new IamTokenData() { AccessToken = _userAcessToken } }, new WatsonError(), new Dictionary<string, object>());
264257
}
265258
else if (!string.IsNullOrEmpty(_iamTokenData.AccessToken) || IsRefreshTokenExpired())
266259
{
267260
// 2. request an initial token
268-
RequestIamToken(OnGetToken, OnGetTokenFail);
261+
RequestIamToken(OnGetToken);
269262
}
270263
else if (IsTokenExpired())
271264
{
272265
// 3. refresh a token
273-
RefreshIamToken(OnGetToken, OnGetTokenFail);
266+
RefreshIamToken(OnGetToken);
274267
}
275268
else
276269
{
277270
// 4. use valid managed token
278-
OnGetToken(new IamTokenData() { AccessToken = _iamTokenData.AccessToken }, new Dictionary<string, object>());
271+
272+
OnGetToken(new WatsonResponse<IamTokenData>() { Result = new IamTokenData() { AccessToken = _iamTokenData.AccessToken } }, new WatsonError(), new Dictionary<string, object>());
279273
}
280274
}
281275

282-
private void OnGetToken(IamTokenData iamTokenData, Dictionary<string, object> customData)
276+
private void OnGetToken(WatsonResponse<IamTokenData> response, WatsonError error, Dictionary<string, object> customData)
283277
{
284-
SaveTokenInfo(iamTokenData);
278+
SaveTokenInfo(response.Result);
285279
}
286280

287-
private void OnGetTokenFail(RESTConnector.Error error, Dictionary<string, object> customData)
288-
{
289-
Log.Debug("Credentials.OnGetTokenFail();", "Failed to get IAM Token: {0}", error.ToString());
290-
}
291281
#endregion
292282

293283
#region Request Token
294284
/// <summary>
295285
/// Request an IAM token using an API key.
296286
/// </summary>
297-
/// <param name="successCallback">The request callback.</param>
298-
/// <param name="failCallback">The fail callback.</param>
287+
/// <param name="callback">The request callback.</param>
288+
/// <param name="error"> The request error.</param>
299289
/// <param name="customData">Dictionary of custom data.</param>
300290
/// <returns></returns>
301-
public bool RequestIamToken(SuccessCallback<IamTokenData> successCallback, FailCallback failCallback, Dictionary<string, object> customData = null)
291+
public bool RequestIamToken(Callback<IamTokenData> callback, Dictionary<string, object> customData = null)
302292
{
303-
if (successCallback == null)
293+
if (callback == null)
304294
throw new ArgumentNullException("successCallback");
305-
if (failCallback == null)
306-
throw new ArgumentNullException("failCallback");
307295

308296
RESTConnector connector = new RESTConnector();
309297
connector.URL = _iamUrl;
310298
if (connector == null)
311299
return false;
312300

313301
RequestIamTokenRequest req = new RequestIamTokenRequest();
314-
req.SuccessCallback = successCallback;
315-
req.FailCallback = failCallback;
302+
req.Callback = callback;
316303
req.HttpMethod = UnityWebRequest.kHttpVerbGET;
317304
req.Headers.Add("Content-type", "application/x-www-form-urlencoded");
318305
req.Headers.Add("Authorization", "Basic Yng6Yng=");
@@ -337,13 +324,13 @@ public bool RequestIamToken(SuccessCallback<IamTokenData> successCallback, FailC
337324
private class RequestIamTokenRequest : RESTConnector.Request
338325
{
339326
public Dictionary<string, object> CustomData { get; set; }
340-
public SuccessCallback<IamTokenData> SuccessCallback { get; set; }
341-
public FailCallback FailCallback { get; set; }
327+
public Callback<IamTokenData> Callback { get; set; }
342328
}
343329

344330
private void OnRequestIamTokenResponse(RESTConnector.Request req, RESTConnector.Response resp)
345331
{
346-
IamTokenData result = new IamTokenData();
332+
WatsonResponse<IamTokenData> response = new WatsonResponse<IamTokenData>();
333+
response.Result = new IamTokenData();
347334
fsData data = null;
348335
Dictionary<string, object> customData = ((RequestIamTokenRequest)req).CustomData;
349336
customData.Add(Constants.String.RESPONSE_HEADERS, resp.Headers);
@@ -356,7 +343,7 @@ private void OnRequestIamTokenResponse(RESTConnector.Request req, RESTConnector.
356343
if (!r.Succeeded)
357344
throw new WatsonException(r.FormattedMessages);
358345

359-
object obj = result;
346+
object obj = response.Result;
360347
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
361348
if (!r.Succeeded)
362349
throw new WatsonException(r.FormattedMessages);
@@ -370,42 +357,31 @@ private void OnRequestIamTokenResponse(RESTConnector.Request req, RESTConnector.
370357
}
371358
}
372359

373-
if (resp.Success)
374-
{
375-
if (((RequestIamTokenRequest)req).SuccessCallback != null)
376-
((RequestIamTokenRequest)req).SuccessCallback(result, customData);
377-
}
378-
else
379-
{
380-
if (((RequestIamTokenRequest)req).FailCallback != null)
381-
((RequestIamTokenRequest)req).FailCallback(resp.Error, customData);
382-
}
360+
if (((RequestIamTokenRequest)req).Callback != null)
361+
((RequestIamTokenRequest)req).Callback(response, resp.Error, customData);
383362
}
384363
#endregion
385364

386365
#region Refresh Token
387366
/// <summary>
388367
/// Refresh an IAM token using a refresh token.
389368
/// </summary>
390-
/// <param name="successCallback">The success callback.</param>
369+
/// <param name="callback">The success callback.</param>
391370
/// <param name="failCallback">The fail callback.</param>
392371
/// <param name="customData">Dictionary of custom data.</param>
393372
/// <returns></returns>
394-
public bool RefreshIamToken(SuccessCallback<IamTokenData> successCallback, FailCallback failCallback, Dictionary<string, object> customData = null)
373+
public bool RefreshIamToken(Callback<IamTokenData> callback, Dictionary<string, object> customData = null)
395374
{
396-
if (successCallback == null)
397-
throw new ArgumentNullException("successCallback");
398-
if (failCallback == null)
399-
throw new ArgumentNullException("failCallback");
375+
if (callback == null)
376+
throw new ArgumentNullException("callback");
400377

401378
RESTConnector connector = new RESTConnector();
402379
connector.URL = _iamUrl;
403380
if (connector == null)
404381
return false;
405382

406383
RefreshIamTokenRequest req = new RefreshIamTokenRequest();
407-
req.SuccessCallback = successCallback;
408-
req.FailCallback = failCallback;
384+
req.Callback = callback;
409385
req.HttpMethod = UnityWebRequest.kHttpVerbGET;
410386
req.Headers.Add("Content-type", "application/x-www-form-urlencoded");
411387
req.Headers.Add("Authorization", "Basic Yng6Yng=");
@@ -428,14 +404,14 @@ public bool RefreshIamToken(SuccessCallback<IamTokenData> successCallback, FailC
428404

429405
private class RefreshIamTokenRequest : RESTConnector.Request
430406
{
407+
public Callback<IamTokenData> Callback { get; set; }
431408
public Dictionary<string, object> CustomData { get; set; }
432-
public SuccessCallback<IamTokenData> SuccessCallback { get; set; }
433-
public FailCallback FailCallback { get; set; }
434409
}
435410

436411
private void OnRefreshIamTokenResponse(RESTConnector.Request req, RESTConnector.Response resp)
437412
{
438-
IamTokenData result = new IamTokenData();
413+
WatsonResponse<IamTokenData> response = new WatsonResponse<IamTokenData>();
414+
response.Result = new IamTokenData();
439415
fsData data = null;
440416
Dictionary<string, object> customData = ((RefreshIamTokenRequest)req).CustomData;
441417
customData.Add(Constants.String.RESPONSE_HEADERS, resp.Headers);
@@ -448,7 +424,7 @@ private void OnRefreshIamTokenResponse(RESTConnector.Request req, RESTConnector.
448424
if (!r.Succeeded)
449425
throw new WatsonException(r.FormattedMessages);
450426

451-
object obj = result;
427+
object obj = response.Result;
452428
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
453429
if (!r.Succeeded)
454430
throw new WatsonException(r.FormattedMessages);
@@ -462,16 +438,8 @@ private void OnRefreshIamTokenResponse(RESTConnector.Request req, RESTConnector.
462438
}
463439
}
464440

465-
if (resp.Success)
466-
{
467-
if (((RefreshIamTokenRequest)req).SuccessCallback != null)
468-
((RefreshIamTokenRequest)req).SuccessCallback(result, customData);
469-
}
470-
else
471-
{
472-
if (((RefreshIamTokenRequest)req).FailCallback != null)
473-
((RefreshIamTokenRequest)req).FailCallback(resp.Error, customData);
474-
}
441+
if (((RefreshIamTokenRequest)req).Callback != null)
442+
((RefreshIamTokenRequest)req).Callback(response, resp.Error, customData);
475443
}
476444
#endregion
477445

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Scripts/Core/WatsonError.cs renamed to Core/WatsonError.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
/**
1+

2+
3+
using System.Collections.Generic;
4+
/**
25
* Copyright 2019 IBM Corp. All Rights Reserved.
36
*
47
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,10 +17,9 @@
1417
* limitations under the License.
1518
*
1619
*/
17-
1820
namespace IBM.Watson
1921
{
20-
class WatsonError
22+
public class WatsonError
2123
{
2224
/// <summary>
2325
/// The url that generated the error.
@@ -35,5 +37,9 @@ class WatsonError
3537
/// The contents of the response from the server.
3638
/// </summary>
3739
public string Response { get; set; }
40+
/// <summary>
41+
/// Dictionary of headers returned by the request.
42+
/// </summary>
43+
public Dictionary<string, string> ResponseHeaders { get; set; }
3844
}
3945
}

0 commit comments

Comments
 (0)