Skip to content

Commit 9fd52c8

Browse files
committed
feat(connectors): update connectors to work with new design
1 parent 6716d05 commit 9fd52c8

File tree

2 files changed

+19
-53
lines changed

2 files changed

+19
-53
lines changed

Connection/RESTConnector.cs

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
using Newtonsoft.Json.Linq;
3030
using Newtonsoft.Json;
3131
using MiniJSON;
32+
using IBM.Cloud.SDK.Authentication;
33+
using Utility = IBM.Cloud.SDK.Utilities.Utility;
3234

3335
#if !NETFX_CORE
3436
using System.Net;
@@ -220,11 +222,11 @@ public Request()
220222
/// The http method for use with UnityWebRequest.
221223
/// </summary>
222224
public string HttpMethod { get; set; }
223-
private bool disableSslVerification = false;
225+
private bool? disableSslVerification = false;
224226
/// <summary>
225227
/// Gets and sets the option to disable ssl verification
226228
/// </summary>
227-
public bool DisableSslVerification
229+
public bool? DisableSslVerification
228230
{
229231
get { return disableSslVerification; }
230232
set { disableSslVerification = value; }
@@ -246,38 +248,27 @@ public bool DisableSslVerification
246248
/// <summary>
247249
/// Credentials used to authenticate with the server.
248250
/// </summary>
249-
public Credentials Authentication { get; set; }
251+
public Authenticator Authentication { get; set; }
250252
/// <summary>
251253
/// Additional headers to attach to all requests.
252254
/// </summary>
253255
public Dictionary<string, string> Headers { get; set; }
254256
#endregion
255257

256258
/// <summary>
257-
/// This function returns a RESTConnector object for the given service and function.
259+
/// This function returns a RESTConnector object for the given service and function.
258260
/// </summary>
259261
/// <param name="serviceID">The ID of the service.</param>
260262
/// <param name="function">The name of the function.</param>
261263
/// <returns>Returns a RESTConnector object or null on error.</returns>
262264
///
263-
264-
265-
public static RESTConnector GetConnector(Credentials credentials, string function)
265+
public static RESTConnector GetConnector(Authenticator authenticator, string function)
266266
{
267267
RESTConnector connector = new RESTConnector
268268
{
269-
URL = credentials.Url + function,
270-
Authentication = credentials
269+
URL = authenticator.Url + function,
270+
Authentication = authenticator
271271
};
272-
273-
if (connector.Authentication.HasIamTokenData())
274-
{
275-
connector.Authentication.iamTokenManager.GetToken();
276-
}
277-
else if (connector.Authentication.HasIcp4dTokenData())
278-
{
279-
connector.Authentication.icp4dTokenManager.GetToken();
280-
}
281272
return connector;
282273
}
283274

@@ -319,26 +310,6 @@ public bool Send(Request request)
319310
#region Private Functions
320311
private void AddHeaders(Dictionary<string, string> headers)
321312
{
322-
if (Authentication != null)
323-
{
324-
if (headers == null)
325-
{
326-
throw new ArgumentNullException("headers");
327-
}
328-
329-
if (Authentication.HasCredentials())
330-
{
331-
headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, Authentication.CreateAuthorization());
332-
}
333-
else if (Authentication.HasIamTokenData())
334-
{
335-
headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, string.Format("Bearer {0}", Authentication.iamTokenManager.GetAccessToken()));
336-
}
337-
else if (Authentication.HasIcp4dTokenData())
338-
{
339-
headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, string.Format("Bearer {0}", Authentication.icp4dTokenManager.GetAccessToken()));
340-
}
341-
}
342313

343314
if (Headers != null)
344315
{
@@ -508,7 +479,7 @@ private IEnumerator ProcessRequestQueue()
508479

509480
unityWebRequest.downloadHandler = new DownloadHandlerBuffer();
510481

511-
if (req.DisableSslVerification)
482+
if (req.DisableSslVerification == true)
512483
{
513484
unityWebRequest.certificateHandler = new AcceptAllCertificates();
514485
}

Connection/WSConnector.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System.Collections;
2424
using System.Collections.Generic;
2525
using System.Security.Authentication;
26+
using IBM.Cloud.SDK.Authentication;
2627
using System.Threading;
2728
#if !NETFX_CORE
2829
using UnitySDK.WebSocketSharp;
@@ -172,7 +173,7 @@ public Dictionary<string, string> Headers
172173
/// <summary>
173174
/// Credentials used to authenticate with the server.
174175
/// </summary>
175-
public Credentials Authentication { get; set; }
176+
public Authenticator Authentication { get; set; }
176177
/// <summary>
177178
/// The current state of this connector.
178179
/// </summary>
@@ -294,29 +295,23 @@ public static string FixupURL(string URL)
294295
/// <summary>
295296
/// Create a WSConnector for the given service and function.
296297
/// </summary>
297-
/// <param name="credentials">The credentials for the service.</param>
298+
/// <param name="authenticator">The credentials for the service.</param>
298299
/// <param name="function">The name of the function to connect.</param>
299300
/// <param name="args">Additional function arguments.</param>
300301
/// <returns>The WSConnector object or null or error.</returns>
301-
public static WSConnector CreateConnector(Credentials credentials, string function, string args)
302+
public static WSConnector CreateConnector(Authenticator authenticator, string function, string args)
302303
{
303304
WSConnector connector = new WSConnector();
304-
if (credentials.HasCredentials())
305+
if (authenticator.AuthenticationType == "basic")
305306
{
306-
connector.Authentication = credentials;
307+
connector.Authentication = authenticator;
307308
}
308-
else if (credentials.HasIamTokenData())
309+
else if (authenticator.AuthenticationType == "iam" || authenticator.AuthenticationType == "cp4d")
309310
{
310-
credentials.iamTokenManager.GetToken();
311-
connector.Headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, string.Format("Bearer {0}", credentials.iamTokenManager.GetAccessToken()));
312-
}
313-
else if (credentials.HasIcp4dTokenData())
314-
{
315-
credentials.icp4dTokenManager.GetToken();
316-
connector.Headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, string.Format("Bearer {0}", credentials.icp4dTokenManager.GetAccessToken()));
311+
authenticator.Authenticate(connector);
317312
}
318313

319-
connector.URL = FixupURL(credentials.Url) + function + args;
314+
connector.URL = FixupURL(authenticator.Url) + function + args;
320315

321316
return connector;
322317
}

0 commit comments

Comments
 (0)