Exemple #1
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();
        }
Exemple #2
0
        private static void FetchClientToken(WitConfiguration configuration, Action action)
        {
            if (!string.IsNullOrEmpty(configuration.application?.id))
            {
                var tokenRequest = configuration.GetClientToken(configuration.application.id);
                tokenRequest.onResponse = (r) =>
                {
                    if (r.StatusCode == 200)
                    {
                        var token = r.ResponseData["client_token"];

                        EditorForegroundRunner.Run(() =>
                        {
                            SerializedObject so = new SerializedObject(configuration);
                            so.FindProperty("clientAccessToken").stringValue =
                                r.ResponseData["client_token"];
                            so.ApplyModifiedProperties();

                            configuration.clientAccessToken = token;
                            EditorUtility.SetDirty(configuration);
                            action?.Invoke();
                        });
                    }
                    else
                    {
                        Debug.LogError(r.StatusDescription);
                    }
                };
                tokenRequest.Request();
            }
        }
Exemple #3
0
 private static void ListEntities(WitRequest r, WitConfiguration configuration, Action onUpdateComplete)
 {
     EditorForegroundRunner.Run(() =>
     {
         var entitiesRequest        = configuration.ListEntitiesRequest();
         entitiesRequest.onResponse = (er) => ListTraits(er, configuration, onUpdateComplete);
         OnUpdateData(r, (response) => UpdateIntentList(configuration, response),
                      entitiesRequest.Request);
     });
 }
Exemple #4
0
 public void Initialize()
 {
     WitAuthUtility.InitEditorTokens();
     configuration = target as WitConfiguration;
     currentToken  = WitAuthUtility.GetAppServerToken(configuration);
     if (WitAuthUtility.IsServerTokenValid(currentToken) &&
         !string.IsNullOrEmpty(configuration?.clientAccessToken))
     {
         configuration?.UpdateData(() =>
         {
             EditorForegroundRunner.Run(() => EditorUtility.SetDirty(configuration));
         });
     }
 }
Exemple #5
0
        private static void OnUpdateData(WitRequest request,
                                         Action <WitResponseNode> updateComponent, Action onUpdateComplete)
        {
            if (request.StatusCode == 200)
            {
                updateComponent(request.ResponseData);
            }
            else
            {
                Debug.LogError($"Request for {request} failed: {request.StatusDescription}");
            }

            EditorForegroundRunner.Run(onUpdateComplete);
        }
Exemple #6
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;
            });
        }
Exemple #7
0
        private static void DoUpdateData(WitConfiguration configuration, Action onUpdateComplete)
        {
            EditorForegroundRunner.Run(() =>
            {
                if (!string.IsNullOrEmpty(
                        WitAuthUtility.GetAppServerToken(configuration.application.id)))
                {
                    var intentsRequest        = configuration.ListIntentsRequest();
                    intentsRequest.onResponse =
                        (r) => ListEntities(r, configuration, onUpdateComplete);

                    configuration.application?.UpdateData(intentsRequest.Request);
                }
            });
        }
Exemple #8
0
 private static void ListTraits(WitRequest er, WitConfiguration configuration, Action onUpdateComplete)
 {
     EditorForegroundRunner.Run(() =>
     {
         var traitsRequest        = configuration.ListTraitsRequest();
         traitsRequest.onResponse =
             (tr) =>
         {
             EditorForegroundRunner.Run(() =>
             {
                 OnUpdateData(tr,
                              (dataResponse) => UpdateTraitList(configuration, dataResponse),
                              onUpdateComplete);
             });
         };
         OnUpdateData(er,
                      (entityResponse) => UpdateEntityList(configuration, entityResponse),
                      traitsRequest.Request);
     });
 }
Exemple #9
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();
        }