Skip to content

Commit 46d1cd1

Browse files
committed
Merge branch '4933-abstract-discovery-endpoints' of github.com:watson-developer-cloud/unity-sdk into 4933-abstract-discovery-endpoints
2 parents 9d8eb04 + 6151d7b commit 46d1cd1

File tree

6 files changed

+284
-8
lines changed

6 files changed

+284
-8
lines changed

NOTICES.txt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
This asset is governed by the Asset Store EULA; however, the following components are governed by the licenses indicated below:
2+
3+
A. websocket-sharp
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2010-2018 sta.blockhead
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
28+
B. Full Serializer
29+
30+
The MIT License (MIT)
31+
32+
Copyright (c) 2014 Jacob Dufault
33+
34+
Permission is hereby granted, free of charge, to any person obtaining a copy
35+
of this software and associated documentation files (the "Software"), to deal
36+
in the Software without restriction, including without limitation the rights
37+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38+
copies of the Software, and to permit persons to whom the Software is
39+
furnished to do so, subject to the following conditions:
40+
41+
The above copyright notice and this permission notice shall be included in
42+
all copies or substantial portions of the Software.
43+
44+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
50+
THE SOFTWARE.
51+
52+
C. MiniJSON
53+
54+
Copyright (c) 2013 Calvin Rien
55+
56+
Based on the JSON parser by Patrick van Bergen
57+
http://techblog.procurios.nl/k/618/news/view/14605/14863/How-do-I-write-my-own-parser-for-JSON.html
58+
59+
Simplified it so that it doesn't throw exceptions
60+
and can be used in Unity iPhone with maximum code stripping.
61+
62+
Permission is hereby granted, free of charge, to any person obtaining
63+
a copy of this software and associated documentation files (the
64+
"Software"), to deal in the Software without restriction, including
65+
without limitation the rights to use, copy, modify, merge, publish,
66+
distribute, sublicense, and/or sell copies of the Software, and to
67+
permit persons to whom the Software is furnished to do so, subject to
68+
the following conditions:
69+
70+
The above copyright notice and this permission notice shall be
71+
included in all copies or substantial portions of the Software.
72+
73+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
74+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
75+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
76+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
77+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
78+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
79+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ private void OnMessage(object resp, Dictionary<string, object> customData)
315315
```
316316

317317
## Authentication Tokens
318+
319+
**Authenticating with the `X-Watson-Authorization-Token` header is deprecated. The token continues to work with Cloud Foundry services, but is not supported for services that use Identity and Access Management (IAM) authentication. For details see [Authenticating with IAM tokens](https://console.bluemix.net/docs/services/watson/getting-started-iam.html#iam) or the [README](#IAM) in the IBM Watson SDK you use.**
320+
318321
You use tokens to write applications that make authenticated requests to IBM Watson™ services without embedding service credentials in every call.
319322

320323
You can write an authentication proxy in IBM Cloud that obtains and returns a token to your client application, which can then use the token to call the service directly. This proxy eliminates the need to channel all service requests through an intermediate server-side application, which is otherwise necessary to avoid exposing your service credentials from your client application.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using FullSerializer;
19+
using IBM.Watson.DeveloperCloud.Connection;
20+
using IBM.Watson.DeveloperCloud.Logging;
21+
using IBM.Watson.DeveloperCloud.Services.LanguageTranslator.v3;
22+
using IBM.Watson.DeveloperCloud.Utilities;
23+
using System.Collections;
24+
using System.Collections.Generic;
25+
using System.IO;
26+
using UnityEngine;
27+
28+
namespace IBM.Watson.DeveloperCloud.UnitTests
29+
{
30+
public class TestLanguageTranslatorV3ApikeyAsBasicauth : UnitTest
31+
{
32+
private string _pharseToTranslate = "Hello, welcome to IBM Watson!";
33+
private fsSerializer _serializer = new fsSerializer();
34+
35+
private LanguageTranslator _languageTranslator;
36+
37+
private bool _getTranslationTested = false;
38+
private bool _getModelsTested = false;
39+
private bool _getModelTested = false;
40+
private bool _identifyTested = false;
41+
private bool _getLanguagesTested = false;
42+
private string _versionDate = "2018-05-01";
43+
44+
public override IEnumerator RunTest()
45+
{
46+
LogSystem.InstallDefaultReactors();
47+
48+
VcapCredentials vcapCredentials = new VcapCredentials();
49+
fsData data = null;
50+
51+
string result = null;
52+
string credentialsFilepath = "../sdk-credentials/credentials.json";
53+
54+
// Load credentials file if it exists. If it doesn't exist, don't run the tests.
55+
if (File.Exists(credentialsFilepath))
56+
result = File.ReadAllText(credentialsFilepath);
57+
else
58+
yield break;
59+
60+
// Add in a parent object because Unity does not like to deserialize root level collection types.
61+
result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES");
62+
63+
// Convert json to fsResult
64+
fsResult r = fsJsonParser.Parse(result, out data);
65+
if (!r.Succeeded)
66+
throw new WatsonException(r.FormattedMessages);
67+
68+
// Convert fsResult to VcapCredentials
69+
object obj = vcapCredentials;
70+
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
71+
if (!r.Succeeded)
72+
throw new WatsonException(r.FormattedMessages);
73+
74+
// Set credentials from imported credntials
75+
Credential credential = vcapCredentials.GetCredentialByname("language-translator-v3-sdk-rc-wdc")[0].Credentials;
76+
_url = credential.Url.ToString();
77+
78+
Credentials credentials = new Credentials("apikey", credential.IamApikey, credential.Url);
79+
80+
// Wait for tokendata
81+
while (!credentials.HasIamTokenData())
82+
yield return null;
83+
84+
_languageTranslator = new LanguageTranslator(_versionDate, credentials);
85+
86+
if (!_languageTranslator.GetTranslation(OnGetTranslation, OnFail, _pharseToTranslate, "en", "es"))
87+
Log.Debug("TestLanguageTranslator.GetTranslation()", "Failed to translate.");
88+
while (!_getTranslationTested)
89+
yield return null;
90+
91+
if (!_languageTranslator.GetModels(OnGetModels, OnFail))
92+
Log.Debug("TestLanguageTranslator.GetModels()", "Failed to get models.");
93+
while (!_getModelsTested)
94+
yield return null;
95+
96+
if (!_languageTranslator.GetModel(OnGetModel, OnFail, "en-es"))
97+
Log.Debug("TestLanguageTranslator.GetModel()", "Failed to get model.");
98+
while (!_getModelTested)
99+
yield return null;
100+
101+
if (!_languageTranslator.Identify(OnIdentify, OnFail, _pharseToTranslate))
102+
Log.Debug("TestLanguageTranslator.Identify()", "Failed to identify language.");
103+
while (!_identifyTested)
104+
yield return null;
105+
106+
if (!_languageTranslator.GetLanguages(OnGetLanguages, OnFail))
107+
Log.Debug("TestLanguageTranslator.GetLanguages()", "Failed to get languages.");
108+
while (!_getLanguagesTested)
109+
yield return null;
110+
111+
Log.Debug("TestLanguageTranslator.RunTest()", "Language Translator examples complete.");
112+
113+
yield break;
114+
}
115+
116+
private void OnGetModels(TranslationModels models, Dictionary<string, object> customData)
117+
{
118+
Log.Debug("TestLanguageTranslator.OnGetModels()", "Language Translator - Get models response: {0}", customData["json"].ToString());
119+
Test(models != null);
120+
_getModelsTested = true;
121+
}
122+
123+
private void OnGetModel(TranslationModel model, Dictionary<string, object> customData)
124+
{
125+
Log.Debug("TestLanguageTranslator.OnGetModel()", "Language Translator - Get model response: {0}", customData["json"].ToString());
126+
Test(model != null);
127+
_getModelTested = true;
128+
}
129+
130+
private void OnGetTranslation(Translations translation, Dictionary<string, object> customData)
131+
{
132+
Log.Debug("TestLanguageTranslator.OnGetTranslation()", "Langauge Translator - Translate Response: {0}", customData["json"].ToString());
133+
Test(translation != null);
134+
_getTranslationTested = true;
135+
}
136+
137+
private void OnIdentify(IdentifiedLanguages identifiedLanguages, Dictionary<string, object> customData)
138+
{
139+
Log.Debug("TestLanguageTranslator.OnIdentify()", "Language Translator - Identify response: {0}", customData["json"].ToString());
140+
Test(identifiedLanguages != null);
141+
_identifyTested = true;
142+
}
143+
144+
private void OnGetLanguages(Languages languages, Dictionary<string, object> customData)
145+
{
146+
Log.Debug("TestLanguageTranslator.OnGetLanguages()", "Language Translator - Get languages response: {0}", customData["json"].ToString());
147+
Test(languages != null);
148+
_getLanguagesTested = true;
149+
}
150+
151+
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
152+
{
153+
Log.Error("TestLanguageTranslator.OnFail()", "Error received: {0}", error.ToString());
154+
}
155+
}
156+
}

Scripts/UnitTests/TestLanguageTranslatorV3RCApikeyAsBasicauth.cs.meta

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

Scripts/Utilities/Credentials.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class Credentials
3535
private IamTokenData _iamTokenData;
3636
private string _iamApiKey;
3737
private string _userAcessToken;
38+
private const string APIKEY_AS_USERNAME = "apikey";
3839
#endregion
3940

4041
#region Public Fields
@@ -118,10 +119,7 @@ public Credentials(string url = null)
118119
/// <param name="url">The service endpoint.</param>
119120
public Credentials(string username, string password, string url = null)
120121
{
121-
Username = username;
122-
Password = password;
123-
if(!string.IsNullOrEmpty(url))
124-
Url = url;
122+
SetCredentials(username, password, url);
125123
}
126124

127125
/// <summary>
@@ -141,7 +139,35 @@ public Credentials(string apiKey, string url = null)
141139
/// <param name="iamTokenOptions"></param>
142140
public Credentials(TokenOptions iamTokenOptions, string serviceUrl = null)
143141
{
144-
if(!string.IsNullOrEmpty(serviceUrl))
142+
SetCredentials(iamTokenOptions, serviceUrl);
143+
}
144+
#endregion
145+
146+
#region SetCredentials
147+
private void SetCredentials(string username, string password, string url = null)
148+
{
149+
if (username == APIKEY_AS_USERNAME)
150+
{
151+
TokenOptions tokenOptions = new TokenOptions()
152+
{
153+
IamApiKey = password
154+
};
155+
156+
SetCredentials(tokenOptions, url);
157+
}
158+
else
159+
{
160+
Username = username;
161+
Password = password;
162+
}
163+
164+
if (!string.IsNullOrEmpty(url))
165+
Url = url;
166+
}
167+
168+
private void SetCredentials(TokenOptions iamTokenOptions, string serviceUrl = null)
169+
{
170+
if (!string.IsNullOrEmpty(serviceUrl))
145171
Url = serviceUrl;
146172
_iamUrl = !string.IsNullOrEmpty(iamTokenOptions.IamUrl) ? iamTokenOptions.IamUrl : "https://iam.bluemix.net/identity/token";
147173
_iamTokenData = new IamTokenData();
@@ -155,7 +181,7 @@ public Credentials(TokenOptions iamTokenOptions, string serviceUrl = null)
155181
GetToken();
156182
}
157183
#endregion
158-
184+
159185
#region Get Token
160186
/// <summary>
161187
/// This function sends an access token back through a callback. The source of the token
@@ -536,9 +562,9 @@ public class VcapCredentials
536562
public List<VcapCredential> GetCredentialByname(string name)
537563
{
538564
List<VcapCredential> credentialsList = new List<VcapCredential>();
539-
foreach(KeyValuePair<string, List<VcapCredential>> kvp in VCAP_SERVICES)
565+
foreach (KeyValuePair<string, List<VcapCredential>> kvp in VCAP_SERVICES)
540566
{
541-
foreach(VcapCredential credential in kvp.Value)
567+
foreach (VcapCredential credential in kvp.Value)
542568
{
543569
if (credential.Name == name)
544570
credentialsList.Add(credential);

Scripts/Utilities/Utility.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,7 @@ public static string GetExtension(string mimeType)
11781178
/// <returns>True if the call succeeds.</returns>
11791179
public static bool GetWatsonToken(OnGetWatsonToken callback, string serviceEndpoint, string username, string password, string tokenName = "")
11801180
{
1181+
Log.Warning("Utility.GetWatsonToken()", "Authenticating with the `X-Watson-Authorization-Token` header is deprecated. The token continues to work with Cloud Foundry services, but is not supported for services that use Identity and Access Management (IAM) authentication. For details see [Authenticating with IAM tokens](https://console.bluemix.net/docs/services/watson/getting-started-iam.html#iam) or the README in the IBM Watson SDK you use.");
11811182
if (callback == null)
11821183
throw new ArgumentNullException("callback");
11831184
if (string.IsNullOrEmpty(serviceEndpoint))

0 commit comments

Comments
 (0)