Skip to content

Commit

Permalink
feat(connectors): update connectors to work with new design
Browse files Browse the repository at this point in the history
  • Loading branch information
mamoonraja committed Aug 26, 2019
1 parent 6716d05 commit 9fd52c8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 53 deletions.
49 changes: 10 additions & 39 deletions Connection/RESTConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using MiniJSON;
using IBM.Cloud.SDK.Authentication;
using Utility = IBM.Cloud.SDK.Utilities.Utility;

#if !NETFX_CORE
using System.Net;
Expand Down Expand Up @@ -220,11 +222,11 @@ public Request()
/// The http method for use with UnityWebRequest.
/// </summary>
public string HttpMethod { get; set; }
private bool disableSslVerification = false;
private bool? disableSslVerification = false;
/// <summary>
/// Gets and sets the option to disable ssl verification
/// </summary>
public bool DisableSslVerification
public bool? DisableSslVerification
{
get { return disableSslVerification; }
set { disableSslVerification = value; }
Expand All @@ -246,38 +248,27 @@ public bool DisableSslVerification
/// <summary>
/// Credentials used to authenticate with the server.
/// </summary>
public Credentials Authentication { get; set; }
public Authenticator Authentication { get; set; }
/// <summary>
/// Additional headers to attach to all requests.
/// </summary>
public Dictionary<string, string> Headers { get; set; }
#endregion

/// <summary>
/// This function returns a RESTConnector object for the given service and function.
/// 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>
/// <returns>Returns a RESTConnector object or null on error.</returns>
///


public static RESTConnector GetConnector(Credentials credentials, string function)
public static RESTConnector GetConnector(Authenticator authenticator, string function)
{
RESTConnector connector = new RESTConnector
{
URL = credentials.Url + function,
Authentication = credentials
URL = authenticator.Url + function,
Authentication = authenticator
};

if (connector.Authentication.HasIamTokenData())
{
connector.Authentication.iamTokenManager.GetToken();
}
else if (connector.Authentication.HasIcp4dTokenData())
{
connector.Authentication.icp4dTokenManager.GetToken();
}
return connector;
}

Expand Down Expand Up @@ -319,26 +310,6 @@ public bool Send(Request request)
#region Private Functions
private void AddHeaders(Dictionary<string, string> headers)
{
if (Authentication != null)
{
if (headers == null)
{
throw new ArgumentNullException("headers");
}

if (Authentication.HasCredentials())
{
headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, Authentication.CreateAuthorization());
}
else if (Authentication.HasIamTokenData())
{
headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, string.Format("Bearer {0}", Authentication.iamTokenManager.GetAccessToken()));
}
else if (Authentication.HasIcp4dTokenData())
{
headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, string.Format("Bearer {0}", Authentication.icp4dTokenManager.GetAccessToken()));
}
}

if (Headers != null)
{
Expand Down Expand Up @@ -508,7 +479,7 @@ private IEnumerator ProcessRequestQueue()

unityWebRequest.downloadHandler = new DownloadHandlerBuffer();

if (req.DisableSslVerification)
if (req.DisableSslVerification == true)
{
unityWebRequest.certificateHandler = new AcceptAllCertificates();
}
Expand Down
23 changes: 9 additions & 14 deletions Connection/WSConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Security.Authentication;
using IBM.Cloud.SDK.Authentication;
using System.Threading;
#if !NETFX_CORE
using UnitySDK.WebSocketSharp;
Expand Down Expand Up @@ -172,7 +173,7 @@ public Dictionary<string, string> Headers
/// <summary>
/// Credentials used to authenticate with the server.
/// </summary>
public Credentials Authentication { get; set; }
public Authenticator Authentication { get; set; }
/// <summary>
/// The current state of this connector.
/// </summary>
Expand Down Expand Up @@ -294,29 +295,23 @@ public static string FixupURL(string URL)
/// <summary>
/// Create a WSConnector for the given service and function.
/// </summary>
/// <param name="credentials">The credentials for the service.</param>
/// <param name="authenticator">The credentials for the service.</param>
/// <param name="function">The name of the function to connect.</param>
/// <param name="args">Additional function arguments.</param>
/// <returns>The WSConnector object or null or error.</returns>
public static WSConnector CreateConnector(Credentials credentials, string function, string args)
public static WSConnector CreateConnector(Authenticator authenticator, string function, string args)
{
WSConnector connector = new WSConnector();
if (credentials.HasCredentials())
if (authenticator.AuthenticationType == "basic")
{
connector.Authentication = credentials;
connector.Authentication = authenticator;
}
else if (credentials.HasIamTokenData())
else if (authenticator.AuthenticationType == "iam" || authenticator.AuthenticationType == "cp4d")
{
credentials.iamTokenManager.GetToken();
connector.Headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, string.Format("Bearer {0}", credentials.iamTokenManager.GetAccessToken()));
}
else if (credentials.HasIcp4dTokenData())
{
credentials.icp4dTokenManager.GetToken();
connector.Headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, string.Format("Bearer {0}", credentials.icp4dTokenManager.GetAccessToken()));
authenticator.Authenticate(connector);
}

connector.URL = FixupURL(credentials.Url) + function + args;
connector.URL = FixupURL(authenticator.Url) + function + args;

return connector;
}
Expand Down

0 comments on commit 9fd52c8

Please # to comment.