Skip to content

IAM apikey as basicauth #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions Scripts/UnitTests/TestLanguageTranslatorV3RCApikeyAsBasicauth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* 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.
*
*/

using FullSerializer;
using IBM.Watson.DeveloperCloud.Connection;
using IBM.Watson.DeveloperCloud.Logging;
using IBM.Watson.DeveloperCloud.Services.LanguageTranslator.v3;
using IBM.Watson.DeveloperCloud.Utilities;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace IBM.Watson.DeveloperCloud.UnitTests
{
public class TestLanguageTranslatorV3ApikeyAsBasicauth : UnitTest
{
private string _pharseToTranslate = "Hello, welcome to IBM Watson!";
private fsSerializer _serializer = new fsSerializer();

private LanguageTranslator _languageTranslator;

private bool _getTranslationTested = false;
private bool _getModelsTested = false;
private bool _getModelTested = false;
private bool _identifyTested = false;
private bool _getLanguagesTested = false;
private string _versionDate = "2018-05-01";

public override IEnumerator RunTest()
{
LogSystem.InstallDefaultReactors();

VcapCredentials vcapCredentials = new VcapCredentials();
fsData data = null;

string result = null;
string credentialsFilepath = "../sdk-credentials/credentials.json";

// Load credentials file if it exists. If it doesn't exist, don't run the tests.
if (File.Exists(credentialsFilepath))
result = File.ReadAllText(credentialsFilepath);
else
yield break;

// Add in a parent object because Unity does not like to deserialize root level collection types.
result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES");

// Convert json to fsResult
fsResult r = fsJsonParser.Parse(result, out data);
if (!r.Succeeded)
throw new WatsonException(r.FormattedMessages);

// Convert fsResult to VcapCredentials
object obj = vcapCredentials;
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
if (!r.Succeeded)
throw new WatsonException(r.FormattedMessages);

// Set credentials from imported credntials
Credential credential = vcapCredentials.GetCredentialByname("language-translator-v3-sdk-rc-wdc")[0].Credentials;
_url = credential.Url.ToString();

Credentials credentials = new Credentials("apikey", credential.IamApikey, credential.Url);

// Wait for tokendata
while (!credentials.HasIamTokenData())
yield return null;

_languageTranslator = new LanguageTranslator(_versionDate, credentials);

if (!_languageTranslator.GetTranslation(OnGetTranslation, OnFail, _pharseToTranslate, "en", "es"))
Log.Debug("TestLanguageTranslator.GetTranslation()", "Failed to translate.");
while (!_getTranslationTested)
yield return null;

if (!_languageTranslator.GetModels(OnGetModels, OnFail))
Log.Debug("TestLanguageTranslator.GetModels()", "Failed to get models.");
while (!_getModelsTested)
yield return null;

if (!_languageTranslator.GetModel(OnGetModel, OnFail, "en-es"))
Log.Debug("TestLanguageTranslator.GetModel()", "Failed to get model.");
while (!_getModelTested)
yield return null;

if (!_languageTranslator.Identify(OnIdentify, OnFail, _pharseToTranslate))
Log.Debug("TestLanguageTranslator.Identify()", "Failed to identify language.");
while (!_identifyTested)
yield return null;

if (!_languageTranslator.GetLanguages(OnGetLanguages, OnFail))
Log.Debug("TestLanguageTranslator.GetLanguages()", "Failed to get languages.");
while (!_getLanguagesTested)
yield return null;

Log.Debug("TestLanguageTranslator.RunTest()", "Language Translator examples complete.");

yield break;
}

private void OnGetModels(TranslationModels models, Dictionary<string, object> customData)
{
Log.Debug("TestLanguageTranslator.OnGetModels()", "Language Translator - Get models response: {0}", customData["json"].ToString());
Test(models != null);
_getModelsTested = true;
}

private void OnGetModel(TranslationModel model, Dictionary<string, object> customData)
{
Log.Debug("TestLanguageTranslator.OnGetModel()", "Language Translator - Get model response: {0}", customData["json"].ToString());
Test(model != null);
_getModelTested = true;
}

private void OnGetTranslation(Translations translation, Dictionary<string, object> customData)
{
Log.Debug("TestLanguageTranslator.OnGetTranslation()", "Langauge Translator - Translate Response: {0}", customData["json"].ToString());
Test(translation != null);
_getTranslationTested = true;
}

private void OnIdentify(IdentifiedLanguages identifiedLanguages, Dictionary<string, object> customData)
{
Log.Debug("TestLanguageTranslator.OnIdentify()", "Language Translator - Identify response: {0}", customData["json"].ToString());
Test(identifiedLanguages != null);
_identifyTested = true;
}

private void OnGetLanguages(Languages languages, Dictionary<string, object> customData)
{
Log.Debug("TestLanguageTranslator.OnGetLanguages()", "Language Translator - Get languages response: {0}", customData["json"].ToString());
Test(languages != null);
_getLanguagesTested = true;
}

private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
{
Log.Error("TestLanguageTranslator.OnFail()", "Error received: {0}", error.ToString());
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 34 additions & 8 deletions Scripts/Utilities/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Credentials
private IamTokenData _iamTokenData;
private string _iamApiKey;
private string _userAcessToken;
private const string APIKEY_AS_USERNAME = "apikey";
#endregion

#region Public Fields
Expand Down Expand Up @@ -118,10 +119,7 @@ public Credentials(string url = null)
/// <param name="url">The service endpoint.</param>
public Credentials(string username, string password, string url = null)
{
Username = username;
Password = password;
if(!string.IsNullOrEmpty(url))
Url = url;
SetCredentials(username, password, url);
}

/// <summary>
Expand All @@ -141,7 +139,35 @@ public Credentials(string apiKey, string url = null)
/// <param name="iamTokenOptions"></param>
public Credentials(TokenOptions iamTokenOptions, string serviceUrl = null)
{
if(!string.IsNullOrEmpty(serviceUrl))
SetCredentials(iamTokenOptions, serviceUrl);
}
#endregion

#region SetCredentials
private void SetCredentials(string username, string password, string url = null)
{
if (username == APIKEY_AS_USERNAME)
{
TokenOptions tokenOptions = new TokenOptions()
{
IamApiKey = password
};

SetCredentials(tokenOptions, url);
}
else
{
Username = username;
Password = password;
}

if (!string.IsNullOrEmpty(url))
Url = url;
}

private void SetCredentials(TokenOptions iamTokenOptions, string serviceUrl = null)
{
if (!string.IsNullOrEmpty(serviceUrl))
Url = serviceUrl;
_iamUrl = !string.IsNullOrEmpty(iamTokenOptions.IamUrl) ? iamTokenOptions.IamUrl : "https://iam.bluemix.net/identity/token";
_iamTokenData = new IamTokenData();
Expand All @@ -155,7 +181,7 @@ public Credentials(TokenOptions iamTokenOptions, string serviceUrl = null)
GetToken();
}
#endregion

#region Get Token
/// <summary>
/// This function sends an access token back through a callback. The source of the token
Expand Down Expand Up @@ -536,9 +562,9 @@ public class VcapCredentials
public List<VcapCredential> GetCredentialByname(string name)
{
List<VcapCredential> credentialsList = new List<VcapCredential>();
foreach(KeyValuePair<string, List<VcapCredential>> kvp in VCAP_SERVICES)
foreach (KeyValuePair<string, List<VcapCredential>> kvp in VCAP_SERVICES)
{
foreach(VcapCredential credential in kvp.Value)
foreach (VcapCredential credential in kvp.Value)
{
if (credential.Name == name)
credentialsList.Add(credential);
Expand Down