/// <summary> /// 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> /// <param name="useCache">If true, then the connections will use a static cache.</param> /// <returns>Returns a RESTConnector object or null on error.</returns> public static RESTConnector GetConnector(string serviceID, string function, bool useCache = true) { RESTConnector connector = null; string connectorID = serviceID + function; if (useCache && sm_Connectors.TryGetValue(connectorID, out connector)) { return(connector); } Config cfg = Config.Instance; Config.CredentialInfo cred = cfg.FindCredentials(serviceID); if (cred == null) { Log.Error("Config", "Failed to find credentials for service {0}.", serviceID); return(null); } connector = new RESTConnector(); connector.URL = cred.m_URL + function; connector.Authentication = new Credentials(cred.m_User, cred.m_Password); if (useCache) { sm_Connectors[connectorID] = connector; } return(connector); }
/// <summary> /// Configures Watson API user credentials. /// </summary> /// <param name="apiUsername">Watson API username</param> /// <param name="apiPassword">Watson API password</param> public void ConfigureCredentials(string apiUsername, string apiPassword) { if (Config.Instance.FindCredentials(Utilities.Constants.WatsonSpeechToTextServiceID) == null) { var speechToTextCredentialInfo = new Config.CredentialInfo(); speechToTextCredentialInfo.m_ServiceID = Utilities.Constants.WatsonSpeechToTextServiceID; speechToTextCredentialInfo.m_URL = Utilities.Constants.WatsonSpeechToTextBaseURL; speechToTextCredentialInfo.m_User = apiUsername; speechToTextCredentialInfo.m_Password = apiPassword; Config.Instance.Credentials.Add(speechToTextCredentialInfo); } }
private bool GetIsValid(ServiceSetup setup) { bool isValid = false; Config cfg = Config.Instance; Config.CredentialInfo info = cfg.FindCredentials(setup.ServiceID); if (info != null) { if ((!string.IsNullOrEmpty(info.m_URL) && !string.IsNullOrEmpty(info.m_Password)) || !string.IsNullOrEmpty(info.m_Apikey)) { isValid = true; } } return(isValid); }
/// <summary> /// 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(string serviceID, string function) { RESTConnector connector = null; string connectorID = serviceID + function; if (sm_Connectors.TryGetValue(connectorID, out connector)) { return(connector); } Config cfg = Config.Instance; #if ENABLE_GATEWAY string serviceType = null; if (cfg.EnableGateway && sm_GatewayServiceTypes.TryGetValue(connectorID, out serviceType)) { connector = new RESTConnector(); connector.UsingGateway = true; connector.URL = cfg.GatewayURL + "/v1/en/service"; connector.Headers = new Dictionary <string, string>(); connector.Headers["ROBOT_KEY"] = cfg.ProductKey; connector.Headers["MAC_ID"] = "UnitySDK"; connector.Headers["Service-Type"] = serviceType; sm_Connectors[connectorID] = connector; return(connector); } #endif Config.CredentialInfo cred = cfg.FindCredentials(serviceID); if (cred == null) { Log.Error("Config", "Failed to find credentials for service {0}.", serviceID); return(null); } connector = new RESTConnector(); connector.UsingGateway = false; connector.URL = cred.m_URL + function; connector.Authentication = new Credentials(cred.m_User, cred.m_Password); sm_Connectors[connectorID] = connector; return(connector); }
/// <summary> /// Create a WSConnector for the given service and function. /// </summary> /// <param name="serviceID">The ID of 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(string serviceID, string function, string args) { WSConnector connector = null; Config cfg = Config.Instance; Config.CredentialInfo cred = cfg.FindCredentials(serviceID); if (cred == null) { Log.Error("Config", "Failed to find BLueMix Credentials for service {0}.", serviceID); return(null); } connector = new WSConnector(); connector.URL = FixupURL(cred.m_URL) + function + args; connector.Authentication = new Credentials(cred.m_User, cred.m_Password); return(connector); }
/// <summary> /// Create a WSConnector for the given service and function. /// </summary> /// <param name="serviceID">The ID of 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(string serviceID, string function, string args) { WSConnector connector = null; string connectorID = serviceID + function; Config cfg = Config.Instance; string serviceType = null; if (cfg.EnableGateway && sm_GatewayServiceTypes.TryGetValue(connectorID, out serviceType)) { connector = new WSConnector(); connector.UsingGateway = true; connector.URL = FixupURL(cfg.GatewayURL) + "/" + serviceType; // + args; Dictionary <string, object> auth = new Dictionary <string, object>(); auth["ROBOT_KEY"] = cfg.ProductKey; auth["MAC_ID"] = "UnitySDK"; connector.Send(new TextMessage(MiniJSON.Json.Serialize(auth)), true); // just queue, we want to let the user do any fix-ups before we actually try to connect return(connector); } Config.CredentialInfo cred = cfg.FindCredentials(serviceID); if (cred == null) { Log.Error("Config", "Failed to find BLueMix Credentials for service {0}.", serviceID); return(null); } connector = new WSConnector(); connector.UsingGateway = false; connector.URL = FixupURL(cred.m_URL) + function + args; connector.Authentication = new Credentials(cred.m_User, cred.m_Password); return(connector); }
private void OnGUI() { Config cfg = Config.Instance; GUILayout.Label(m_WatsonIcon); m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos); if (m_WizardMode) { //GUILayout.Label( "Use this dialog to generate your configuration file for the Watson Unity SDK." ); //GUILayout.Label( "If you have never registered for Watson BlueMix services, click on the button below to begin registration." ); if (GUILayout.Button("Register for Watson Services")) { Application.OpenURL(BLUEMIX_REGISTRATION); } foreach (var setup in SERVICE_SETUP) { Config.CredentialInfo info = cfg.FindCredentials(setup.ServiceID); bool bValid = GetIsValid(setup); GUILayout.BeginHorizontal(); if (m_ServiceStatus.ContainsKey(setup.ServiceID)) { if (m_ServiceStatus[setup.ServiceID]) { GUILayout.Label(m_StatusUp, GUILayout.Width(20)); } else { GUILayout.Label(m_StatusDown, GUILayout.Width(20)); } } else { GUILayout.Label(m_StatusUnknown, GUILayout.Width(20)); } GUIStyle labelStyle = new GUIStyle(GUI.skin.label); labelStyle.normal.textColor = bValid ? Color.green : Color.grey; GUILayout.Label(string.Format("Service {0} {1}.", setup.ServiceName, bValid ? "CONFIGURED" : "NOT CONFIGURED"), labelStyle); if (GUILayout.Button("Configure", GUILayout.Width(100))) { Application.OpenURL(setup.URL); } if (bValid && GUILayout.Button("Clear", GUILayout.Width(100))) { cfg.Credentials.Remove(info); } GUILayout.EndHorizontal(); } GUILayout.Label("PASTE CREDENTIALS BELOW:"); m_PastedCredentials = EditorGUILayout.TextArea(m_PastedCredentials); GUI.SetNextControlName("Apply"); if (GUILayout.Button("Apply Credentials")) { bool bParsed = false; Config.CredentialInfo newInfo = new Config.CredentialInfo(); if (newInfo.ParseJSON(m_PastedCredentials)) { foreach (var setup in SERVICE_SETUP) { if (newInfo.m_URL.EndsWith(setup.ServiceAPI)) { newInfo.m_ServiceID = setup.ServiceID; bool bAdd = true; // remove any previous credentials with the same service ID for (int i = 0; i < cfg.Credentials.Count; ++i) { if (cfg.Credentials[i].m_ServiceID == newInfo.m_ServiceID) { bAdd = false; if (EditorUtility.DisplayDialog("Confirm", string.Format("Replace existing service credentials for {0}?", setup.ServiceName), YES, NO)) { cfg.Credentials.RemoveAt(i); bAdd = true; break; } } } if (bAdd) { cfg.Credentials.Add(newInfo); } bParsed = true; } } } if (bParsed) { m_CheckServicesNow = true; EditorUtility.DisplayDialog("Complete", "Credentials applied.", OK); m_PastedCredentials = "\n\n\n\n\n\n\n"; GUI.FocusControl("Apply"); SaveConfig(); } else { EditorUtility.DisplayDialog("Error", "Failed to parse credentials:\n" + m_PastedCredentials, OK); } } if (GUILayout.Button("Save")) { SaveConfig(); } if (GUILayout.Button("Advanced Mode")) { m_WizardMode = false; PlayerPrefs.SetInt("WizardMode", 0); } } else { cfg.ClassifierDirectory = EditorGUILayout.TextField("Classifier Directory", cfg.ClassifierDirectory); cfg.TimeOut = EditorGUILayout.FloatField("Timeout", cfg.TimeOut); cfg.MaxRestConnections = EditorGUILayout.IntField("Max Connections", cfg.MaxRestConnections); EditorGUILayout.LabelField("BlueMix Credentials"); EditorGUI.indentLevel += 1; for (int i = 0; i < cfg.Credentials.Count; ++i) { Config.CredentialInfo info = cfg.Credentials[i]; GUILayout.BeginHorizontal(); info.m_ServiceID = EditorGUILayout.TextField("ServiceID", info.m_ServiceID); if (!string.IsNullOrEmpty(info.m_ServiceID) && m_ServiceStatus.ContainsKey(info.m_ServiceID)) { if (m_ServiceStatus[info.m_ServiceID]) { GUILayout.Label(m_StatusUp, GUILayout.Width(20)); } else { GUILayout.Label(m_StatusDown, GUILayout.Width(20)); } } else { GUILayout.Label(m_StatusUnknown, GUILayout.Width(20)); } GUILayout.EndHorizontal(); info.m_URL = EditorGUILayout.TextField("URL", info.m_URL); if (!string.IsNullOrEmpty(info.m_URL)) { if (info.m_URL.StartsWith("https://gateway-a")) { info.m_Apikey = EditorGUILayout.TextField("API Key", info.m_Apikey); } else { info.m_User = EditorGUILayout.TextField("User", info.m_User); info.m_Password = EditorGUILayout.TextField("Password", info.m_Password); } } if (GUILayout.Button("Delete")) { cfg.Credentials.RemoveAt(i--); } } if (GUILayout.Button("Add Credential")) { cfg.Credentials.Add(new Config.CredentialInfo()); } EditorGUI.indentLevel -= 1; EditorGUILayout.LabelField("Variables"); EditorGUI.indentLevel += 1; for (int i = 0; i < cfg.Variables.Count; ++i) { Config.Variable variable = cfg.Variables[i]; GUILayout.BeginHorizontal(); variable.Key = EditorGUILayout.TextField(variable.Key); EditorGUILayout.LabelField("=", GUILayout.Width(30)); variable.Value = EditorGUILayout.TextField(variable.Value); if (GUILayout.Button("Delete", GUILayout.Width(100))) { cfg.Variables.RemoveAt(i--); } GUILayout.EndHorizontal(); } if (GUILayout.Button("Add Variable")) { cfg.Variables.Add(new Config.Variable()); } EditorGUI.indentLevel -= 1; if (GUILayout.Button("Save")) { m_CheckServicesNow = true; SaveConfig(); } if (GUILayout.Button("Basic Mode")) { m_WizardMode = true; PlayerPrefs.SetInt("WizardMode", 1); } } EditorGUILayout.EndScrollView(); }
private void OnGUI() { Config cfg = Config.Instance; GUILayout.Label(m_WatsonIcon); m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos); if (m_WizardMode) { //GUILayout.Label( "Use this dialog to generate your configuration file for the Watson Unity SDK." ); //GUILayout.Label( "If you have never registered for Watson BlueMix services, click on the button below to begin registration." ); if (GUILayout.Button("Register for Watson Services")) { Application.OpenURL(BLUEMIX_REGISTRATION); } foreach (var setup in SERVICE_SETUP) { Config.CredentialInfo info = cfg.FindCredentials(setup.ServiceID); bool bValid = info != null && !string.IsNullOrEmpty(info.m_URL) && !string.IsNullOrEmpty(info.m_User) && !string.IsNullOrEmpty(info.m_Password); GUILayout.BeginHorizontal(); if (m_ServiceStatus.ContainsKey(setup.ServiceID)) { if (m_ServiceStatus[setup.ServiceID]) { GUILayout.Label(m_StatusUp, GUILayout.Width(20)); } else { GUILayout.Label(m_StatusDown, GUILayout.Width(20)); } } else { GUILayout.Label(m_StatusUnknown, GUILayout.Width(20)); } GUIStyle labelStyle = new GUIStyle(GUI.skin.label); labelStyle.normal.textColor = bValid ? Color.green : Color.grey; GUILayout.Label(string.Format("Service {0} {1}.", setup.ServiceName, bValid ? "CONFIGURED" : "NOT CONFIGURED"), labelStyle); if (GUILayout.Button("Configure", GUILayout.Width(100))) { Application.OpenURL(setup.URL); } if (bValid && GUILayout.Button("Clear", GUILayout.Width(100))) { cfg.Credentials.Remove(info); } GUILayout.EndHorizontal(); } GUILayout.Label("PASTE CREDENTIALS BELOW:"); m_PastedCredentials = EditorGUILayout.TextArea(m_PastedCredentials); GUI.SetNextControlName("Apply"); if (GUILayout.Button("Apply Credentials")) { bool bParsed = false; Config.CredentialInfo newInfo = new Config.CredentialInfo(); if (newInfo.ParseJSON(m_PastedCredentials)) { foreach (var setup in SERVICE_SETUP) { if (newInfo.m_URL.EndsWith(setup.ServiceAPI)) { newInfo.m_ServiceID = setup.ServiceID; bool bAdd = true; // remove any previous credentials with the same service ID for (int i = 0; i < cfg.Credentials.Count; ++i) { if (cfg.Credentials[i].m_ServiceID == newInfo.m_ServiceID) { bAdd = false; if (EditorUtility.DisplayDialog("Confirm", string.Format("Replace existing service credentials for {0}?", setup.ServiceName), YES, NO)) { cfg.Credentials.RemoveAt(i); bAdd = true; break; } } } if (bAdd) { cfg.Credentials.Add(newInfo); } bParsed = true; } } } if (bParsed) { m_CheckServicesNow = true; EditorUtility.DisplayDialog("Complete", "Credentials applied.", OK); m_PastedCredentials = "\n\n\n\n\n\n\n"; GUI.FocusControl("Apply"); SaveConfig(); } else { EditorUtility.DisplayDialog("Error", "Failed to parse credentials:\n" + m_PastedCredentials, OK); } } if (GUILayout.Button("Save")) { SaveConfig(); } if (GUILayout.Button("Advanced Mode")) { m_WizardMode = false; PlayerPrefs.SetInt("WizardMode", 0); } } else { cfg.TimeOut = EditorGUILayout.FloatField("Timeout", cfg.TimeOut); cfg.MaxRestConnections = EditorGUILayout.IntField("Max Connections", cfg.MaxRestConnections); #if ENABLE_GATEWAY cfg.EnableGateway = EditorGUILayout.ToggleLeft("Enable Gateway", cfg.EnableGateway); if (cfg.EnableGateway) { EditorGUI.indentLevel += 1; cfg.GatewayURL = EditorGUILayout.TextField("Gateway URL", cfg.GatewayURL); m_GatewayUser = EditorGUILayout.TextField("Gateway User", m_GatewayUser); m_GatewayPassword = EditorGUILayout.PasswordField("Gateway Password", m_GatewayPassword); cfg.ProductKey = EditorGUILayout.TextField("Product Key", cfg.ProductKey); if (GUILayout.Button("Create Product Key") && (string.IsNullOrEmpty(cfg.ProductKey) || EditorUtility.DisplayDialog("Confirm", "Please confirm you replacing your current key.", "Yes", "No"))) { cfg.ProductKey = Guid.NewGuid().ToString(); Dictionary <string, object> addKeyReq = new Dictionary <string, object>(); addKeyReq["robotKey"] = cfg.ProductKey; addKeyReq["groupName"] = Application.productName; addKeyReq["deviceLimit"] = "9999"; Dictionary <string, string> headers = new Dictionary <string, string>(); headers["Authorization"] = new Credentials(m_GatewayUser, m_GatewayPassword).CreateAuthorization(); headers["Content-Type"] = "application/json"; byte[] data = Encoding.UTF8.GetBytes(MiniJSON.Json.Serialize(addKeyReq)); WWW www = new WWW(cfg.GatewayURL + "/v1/admin/addKey", data, headers); while (!www.isDone) { ; } if (!string.IsNullOrEmpty(www.error)) { Log.Warning("ConfigEditor", "Register App Error: {0}", www.error); } bool bRegistered = false; if (!string.IsNullOrEmpty(www.text)) { IDictionary json = MiniJSON.Json.Deserialize(www.text) as IDictionary; if (json != null && json.Contains("status")) { bRegistered = (long)json["status"] != 0; } else { Log.Error("ConfigEditor", "Invalid response from gateway: {0}", www.text); } } if (bRegistered) { Dictionary <string, object> registerReq = new Dictionary <string, object>(); registerReq["robotKey"] = cfg.ProductKey; registerReq["robotName"] = Application.productName; registerReq["macId"] = "UnitySDK"; data = Encoding.UTF8.GetBytes(MiniJSON.Json.Serialize(registerReq)); www = new WWW(cfg.GatewayURL + "/v1/admin/addRobot", data, headers); while (!www.isDone) { ; } if (!string.IsNullOrEmpty(www.error)) { Log.Warning("ConfigEditor", "Register Secret Error: {0}", www.error); } bRegistered = false; if (!string.IsNullOrEmpty(www.text)) { IDictionary json = MiniJSON.Json.Deserialize(www.text) as IDictionary; if (json.Contains("status")) { bRegistered = (long)json["status"] != 0; } } } if (!bRegistered) { Config.Instance.ProductKey = string.Empty; EditorUtility.DisplayDialog("Error", "Failed to register product with gateway.", "OK"); } } EditorGUI.indentLevel -= 1; } #endif EditorGUILayout.LabelField("BlueMix Credentials"); EditorGUI.indentLevel += 1; for (int i = 0; i < cfg.Credentials.Count; ++i) { Config.CredentialInfo info = cfg.Credentials[i]; GUILayout.BeginHorizontal(); info.m_ServiceID = EditorGUILayout.TextField("ServiceID", info.m_ServiceID); if (m_ServiceStatus.ContainsKey(info.m_ServiceID)) { if (m_ServiceStatus[info.m_ServiceID]) { GUILayout.Label(m_StatusUp, GUILayout.Width(20)); } else { GUILayout.Label(m_StatusDown, GUILayout.Width(20)); } } else { GUILayout.Label(m_StatusUnknown, GUILayout.Width(20)); } GUILayout.EndHorizontal(); info.m_URL = EditorGUILayout.TextField("URL", info.m_URL); info.m_User = EditorGUILayout.TextField("User", info.m_User); info.m_Password = EditorGUILayout.TextField("Password", info.m_Password); if (GUILayout.Button("Delete")) { cfg.Credentials.RemoveAt(i--); } } if (GUILayout.Button("Add Credential")) { cfg.Credentials.Add(new Config.CredentialInfo()); } EditorGUI.indentLevel -= 1; EditorGUILayout.LabelField("Variables"); EditorGUI.indentLevel += 1; for (int i = 0; i < cfg.Variables.Count; ++i) { Config.Variable variable = cfg.Variables[i]; GUILayout.BeginHorizontal(); variable.Key = EditorGUILayout.TextField(variable.Key); EditorGUILayout.LabelField("=", GUILayout.Width(30)); variable.Value = EditorGUILayout.TextField(variable.Value); if (GUILayout.Button("Delete", GUILayout.Width(100))) { cfg.Variables.RemoveAt(i--); } GUILayout.EndHorizontal(); } if (GUILayout.Button("Add Variable")) { cfg.Variables.Add(new Config.Variable()); } EditorGUI.indentLevel -= 1; if (GUILayout.Button("Save")) { m_CheckServicesNow = true; SaveConfig(); } if (GUILayout.Button("Basic Mode")) { m_WizardMode = true; PlayerPrefs.SetInt("WizardMode", 1); } } EditorGUILayout.EndScrollView(); }