Пример #1
0
        // Apply all application data
        private static void ApplyAllApplicationData(string serverToken, WitResponseNode witResponse, Action <string> onComplete)
        {
            var applications = witResponse.AsArray;

            for (int i = 0; i < applications.Count; i++)
            {
                // Get application
                var    application = WitApplication.FromJson(applications[i]);
                string appID       = application?.id;
                // Apply app server token if applicable
                if (applications[i]["is_app_for_token"].AsBool)
                {
                    WitAuthUtility.SetAppServerToken(appID, serverToken);
                }
                // Apply to configuration
                int witConfigIndex = Array.FindIndex(WitConfigs, (configuration) => string.Equals(appID, configuration?.application?.id));
                if (witConfigIndex != -1)
                {
                    WitConfiguration configuration = _witConfigs[witConfigIndex];
                    configuration.application = application;
                    EditorUtility.SetDirty(configuration);
                    configuration.RefreshData();
                }
            }
            onComplete("");
        }
Пример #2
0
        public static void UpdateTokenData(string serverToken, Action updateComplete = null)
        {
            if (!WitAuthUtility.IsServerTokenValid(serverToken))
            {
                return;
            }

            var listRequest = WitRequestFactory.ListAppsRequest(serverToken, 10000);

            listRequest.onResponse = (r) =>
            {
                if (r.StatusCode == 200)
                {
                    var applications = r.ResponseData.AsArray;
                    for (int i = 0; i < applications.Count; i++)
                    {
                        if (applications[i]["is_app_for_token"].AsBool)
                        {
                            var application = WitApplication.FromJson(applications[i]);
                            EditorForegroundRunner.Run(() =>
                            {
                                WitAuthUtility.SetAppServerToken(application.id, serverToken);
                                updateComplete?.Invoke();
                            });
                            break;
                        }
                    }
                }
                else
                {
                    Debug.LogError(r.StatusDescription);
                }
            };
            listRequest.Request();
        }
Пример #3
0
        private void RefreshAppData(string appId, string token = "")
        {
            var refreshToken = WitAuthUtility.GetAppServerToken(appId, token);

            if (string.IsNullOrEmpty(refreshToken) &&
                string.IsNullOrEmpty(appId) &&
                !string.IsNullOrEmpty(configuration?.application?.id))
            {
                refreshToken = WitAuthUtility.GetAppServerToken(configuration.application.id,
                                                                WitAuthUtility.ServerToken);
                appId = WitAuthUtility.GetAppId(refreshToken);
                if (string.IsNullOrEmpty(appId))
                {
                    UpdateTokenData(refreshToken, () =>
                    {
                        appId = WitAuthUtility.GetAppId(refreshToken);
                        if (appId == configuration.application.id)
                        {
                            configuration.FetchAppConfigFromServerToken(refreshToken, () =>
                            {
                                currentToken = refreshToken;
                                WitAuthUtility.SetAppServerToken(configuration.application.id, currentToken);
                                EditorUtility.SetDirty(configuration);
                                EditorForegroundRunner.Run(Repaint);
                                appConfigurationFoldout = false;
                            });
                        }
                    });
                    return;
                }

                if (appId == configuration.application.id)
                {
                    refreshToken = WitAuthUtility.ServerToken;
                }
            }

            if (currentToken != refreshToken)
            {
                currentToken = refreshToken;
            }

            configuration.FetchAppConfigFromServerToken(refreshToken, () =>
            {
                currentToken = refreshToken;
                EditorForegroundRunner.Run(Repaint);
                appConfigurationFoldout = false;
            });
        }
Пример #4
0
        // Refresh client data
        private static void SetClientData(WitConfiguration configuration, string serverToken, Action <string> onSetComplete)
        {
            // Invalid app ID
            string appID = GetAppID(configuration);

            if (string.IsNullOrEmpty(appID))
            {
                SetConfigServerTokenComplete(configuration, serverToken, "Invalid App ID", onSetComplete);
                return;
            }
            // Set server token
            WitAuthUtility.SetAppServerToken(appID, serverToken);
            // Clear client token
            ApplyClientToken(configuration, string.Empty, null);
            // Find client id
            PerformConfigRequest(configuration, configuration.GetClientToken(appID), ApplyClientToken, (error) =>
            {
                SetConfigServerTokenComplete(configuration, serverToken, error, onSetComplete);
            });
        }
Пример #5
0
        private static void FetchApplicationFromServerToken(WitConfiguration configuration,
                                                            string serverToken, Action response)
        {
            var listRequest = WitRequestFactory.ListAppsRequest(serverToken, 10000);

            listRequest.onResponse = (r) =>
            {
                if (r.StatusCode == 200)
                {
                    var applications = r.ResponseData.AsArray;
                    for (int i = 0; i < applications.Count; i++)
                    {
                        if (applications[i]["is_app_for_token"].AsBool)
                        {
                            if (null != configuration.application)
                            {
                                configuration.application.UpdateData(applications[i]);
                            }
                            else
                            {
                                configuration.application =
                                    WitApplication.FromJson(applications[i]);
                            }

                            EditorForegroundRunner.Run(() =>
                            {
                                WitAuthUtility.SetAppServerToken(configuration.application.id,
                                                                 serverToken);
                                response?.Invoke();
                            });
                            break;
                        }
                    }
                }
                else
                {
                    Debug.LogError(r.StatusDescription);
                }
            };
            listRequest.Request();
        }