Skip to content

Commit db040b7

Browse files
committed
GetAPIKey implementation instead of variable
1 parent 772b379 commit db040b7

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

Examples/ServiceExamples/Scripts/ExampleVisualRecognition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void Start ()
3636
// Get all classifiers
3737
if(!m_VisualRecognition.GetClassifiers(OnGetClassifiers))
3838
Log.Debug("ExampleVisualRecognition", "Getting classifiers failed!");
39-
39+
//
4040
// Find classifier by name
4141
m_VisualRecognition.FindClassifier(m_classifierName, OnFindClassifier);
4242

Scripts/Services/AlchemyAPI/AlchemyLanguage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class AlchemyLanguage : IWatsonService
4545
#region SetCredentials
4646
private void SetCredentials()
4747
{
48-
mp_ApiKey = Config.Instance.GetVariableValue("ALCHEMY_API_KEY");
48+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
4949

5050
if (string.IsNullOrEmpty(mp_ApiKey))
5151
throw new WatsonException("ALCHEMY_API_KEY needs to be defined in config.json");

Scripts/Services/VisualRecognition/VisualRecognition.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public class VisualRecognition : IWatsonService
109109
public bool Classify(string url, OnClassify callback, string[] owners = default(string[]), string[] classifierIDs = default(string[]), float threshold = default(float), string acceptLanguage = "en")
110110
{
111111
if(string.IsNullOrEmpty(mp_ApiKey))
112-
mp_ApiKey = Config.Instance.GetVariableValue("VISUAL_RECOGNITION_API_KEY");
112+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
113113
if(string.IsNullOrEmpty(mp_ApiKey))
114114
throw new WatsonException("FindClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
115115
if(string.IsNullOrEmpty(url))
@@ -151,7 +151,7 @@ public class VisualRecognition : IWatsonService
151151
public bool Classify(OnClassify callback, string imagePath, string[] owners = default(string[]), string[] classifierIDs = default(string[]), float threshold = default(float), string acceptLanguage = "en")
152152
{
153153
if(string.IsNullOrEmpty(mp_ApiKey))
154-
mp_ApiKey = Config.Instance.GetVariableValue("VISUAL_RECOGNITION_API_KEY");
154+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
155155
if(string.IsNullOrEmpty(mp_ApiKey))
156156
throw new WatsonException("FindClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
157157
if(callback == null)
@@ -259,7 +259,7 @@ public bool DetectFaces(string url, OnDetectFaces callback)
259259
if(callback == null)
260260
throw new ArgumentNullException("callback");
261261
if(string.IsNullOrEmpty(mp_ApiKey))
262-
mp_ApiKey = Config.Instance.GetVariableValue("VISUAL_RECOGNITION_API_KEY");
262+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
263263
if(string.IsNullOrEmpty(mp_ApiKey))
264264
throw new WatsonException("FindClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
265265

@@ -288,7 +288,7 @@ public bool DetectFaces(OnDetectFaces callback, string imagePath)
288288
if(string.IsNullOrEmpty(imagePath))
289289
throw new ArgumentNullException("Define an image path to classify!");
290290
if(string.IsNullOrEmpty(mp_ApiKey))
291-
mp_ApiKey = Config.Instance.GetVariableValue("VISUAL_RECOGNITION_API_KEY");
291+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
292292
if(string.IsNullOrEmpty(mp_ApiKey))
293293
throw new WatsonException("FindClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
294294

@@ -392,7 +392,7 @@ public bool RecognizeText(string url, OnRecognizeText callback)
392392
if(callback == null)
393393
throw new ArgumentNullException("callback");
394394
if(string.IsNullOrEmpty(mp_ApiKey))
395-
mp_ApiKey = Config.Instance.GetVariableValue("VISUAL_RECOGNITION_API_KEY");
395+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
396396
if(string.IsNullOrEmpty(mp_ApiKey))
397397
throw new WatsonException("FindClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
398398

@@ -421,7 +421,7 @@ public bool RecognizeText(OnRecognizeText callback, string imagePath)
421421
if(string.IsNullOrEmpty(imagePath))
422422
throw new ArgumentNullException("Define an image path to classify!");
423423
if(string.IsNullOrEmpty(mp_ApiKey))
424-
mp_ApiKey = Config.Instance.GetVariableValue("VISUAL_RECOGNITION_API_KEY");
424+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
425425
if(string.IsNullOrEmpty(mp_ApiKey))
426426
throw new WatsonException("FindClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
427427

@@ -545,7 +545,7 @@ public FindClassifierReq(VisualRecognition service, string classifierName, OnFin
545545
if(string.IsNullOrEmpty(classifierName))
546546
throw new WatsonException("classifierName required");
547547
if(string.IsNullOrEmpty(mp_ApiKey))
548-
mp_ApiKey = Config.Instance.GetVariableValue("VISUAL_RECOGNITION_API_KEY");
548+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
549549
if(string.IsNullOrEmpty(mp_ApiKey))
550550
throw new WatsonException("FindClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
551551

@@ -598,7 +598,7 @@ public bool GetClassifiers(OnGetClassifiers callback)
598598
if(callback == null)
599599
throw new ArgumentNullException("callback");
600600
if(string.IsNullOrEmpty(mp_ApiKey))
601-
mp_ApiKey = Config.Instance.GetVariableValue("VISUAL_RECOGNITION_API_KEY");
601+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
602602
if(string.IsNullOrEmpty(mp_ApiKey))
603603
throw new WatsonException("GetClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
604604

@@ -662,7 +662,7 @@ public bool GetClassifier(string classifierId, OnGetClassifier callback)
662662
if (callback == null)
663663
throw new ArgumentNullException("callback");
664664
if(string.IsNullOrEmpty(mp_ApiKey))
665-
mp_ApiKey = Config.Instance.GetVariableValue("VISUAL_RECOGNITION_API_KEY");
665+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
666666
if(string.IsNullOrEmpty(mp_ApiKey))
667667
throw new WatsonException("GetClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
668668

@@ -728,7 +728,7 @@ private void OnGetClassifierResp(RESTConnector.Request req, RESTConnector.Respon
728728
public bool TrainClassifier(string classifierName, string className, string positiveExamplesPath, string negativeExamplesPath, OnTrainClassifier callback)
729729
{
730730
if(string.IsNullOrEmpty(mp_ApiKey))
731-
mp_ApiKey = Config.Instance.GetVariableValue("VISUAL_RECOGNITION_API_KEY");
731+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
732732
if(string.IsNullOrEmpty(mp_ApiKey))
733733
throw new WatsonException("GetClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
734734
if(string.IsNullOrEmpty(classifierName))
@@ -764,7 +764,7 @@ public bool TrainClassifier(string classifierName, string className, string posi
764764
private bool UploadClassifier(string classifierName, string className, byte[] positiveExamplesData, byte[] negativeExamplesData, OnTrainClassifier callback)
765765
{
766766
if(string.IsNullOrEmpty(mp_ApiKey))
767-
mp_ApiKey = Config.Instance.GetVariableValue("VISUAL_RECOGNITION_API_KEY");
767+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
768768
if(string.IsNullOrEmpty(mp_ApiKey))
769769
throw new WatsonException("GetClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
770770
if(string.IsNullOrEmpty(classifierName))
@@ -842,7 +842,7 @@ public bool DeleteClassifier(string classifierId, OnDeleteClassifier callback)
842842
if(callback == null)
843843
throw new ArgumentNullException("callback");
844844
if(string.IsNullOrEmpty(mp_ApiKey))
845-
mp_ApiKey = Config.Instance.GetVariableValue("VISUAL_RECOGNITION_API_KEY");
845+
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
846846
if(string.IsNullOrEmpty(mp_ApiKey))
847847
throw new WatsonException("GetClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
848848

Scripts/Utilities/Config.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ public Variable GetVariable(string key)
317317
return null;
318318
}
319319

320+
public string GetAPIKey(string serviceID)
321+
{
322+
foreach (var info in m_Credentials)
323+
if (info.m_ServiceID == serviceID)
324+
return info.m_Apikey;
325+
return null;
326+
}
327+
320328
/// <summary>
321329
/// Gets the variable value.
322330
/// </summary>

0 commit comments

Comments
 (0)