예제 #1
0
        private void SubmitUtterance()
        {
            if (Application.isPlaying && !HasWit)
            {
                SetDefaultWit();
            }

            if (wit && Application.isPlaying)
            {
                wit.Activate(utterance);
                // Hack to watch for loading to complete. Response does not
                // come back on the main thread so Repaint in onResponse in
                // the editor does nothing.
                EditorApplication.update += WatchForWitResponse;
            }
            else
            {
                // Hack to watch for loading to complete. Response does not
                // come back on the main thread so Repaint in onResponse in
                // the editor does nothing.
                EditorApplication.update += WatchForResponse;

                submitStart        = System.DateTime.Now;
                request            = witConfiguration.MessageRequest(utterance, new WitRequestOptions());
                request.onResponse = OnResponse;
                request.Request();
            }
        }
예제 #2
0
 // Perform a configuration wit request and then apply configuration data
 private static void PerformConfigRequest(WitConfiguration configuration, WitRequest request, Action <WitConfiguration, WitResponseNode, Action <string> > onApply, Action <string> onComplete)
 {
     PerformRequest(request, (response, onRequestComplete) =>
     {
         onApply(configuration, response, onRequestComplete);
     }, onComplete);
 }
예제 #3
0
        private void SubmitUtterance()
        {
            // Remove response
            _response = null;

            if (Application.isPlaying)
            {
                if (service)
                {
                    _status       = WitTexts.Texts.UnderstandingViewerListeningLabel;
                    _responseText = _status;
                    service.Activate(_utterance);
                    // Hack to watch for loading to complete. Response does not
                    // come back on the main thread so Repaint in onResponse in
                    // the editor does nothing.
                    EditorApplication.update += WatchForWitResponse;
                }
            }
            else
            {
                _status       = WitTexts.Texts.UnderstandingViewerLoadingLabel;
                _responseText = _status;
                _submitStart  = System.DateTime.Now;
                _request      = witConfiguration.MessageRequest(_utterance, new WitRequestOptions());
                _request.onPartialResponse += (r) => OnPartialResponse(r?.ResponseData);
                _request.onResponse        += (r) => OnResponse(r?.ResponseData);
                _request.Request();
            }
        }
예제 #4
0
 private void OnRequestStarted(WitRequest r)
 {
     // The raw response comes back on a different thread. We store the
     // message received for display on the next frame.
     if (showJson)
     {
         r.onRawResponse = (response) => pendingText = response;
     }
 }
예제 #5
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);
     });
 }
예제 #6
0
        private void OnUpdateData(WitRequest request, Action onUpdateComplete)
        {
            if (request.StatusCode == 200)
            {
                UpdateData(request.ResponseData);
            }
            else
            {
                Debug.LogError(request.StatusDescription);
            }

            onUpdateComplete?.Invoke();
        }
예제 #7
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);
        }
예제 #8
0
 private void OnResponse(WitRequest request)
 {
     responseCode = request.StatusCode;
     if (null != request.ResponseData)
     {
         ShowResponse(request.ResponseData);
     }
     else if (!string.IsNullOrEmpty(request.StatusDescription))
     {
         responseText = request.StatusDescription;
     }
     else
     {
         responseText = "No response. Status: " + request.StatusCode;
     }
     EditorForegroundRunner.Run(Repaint);
 }
예제 #9
0
 private void OnRequestStarted(WitRequest request)
 {
     // The raw response comes back on a different thread. We store the
     // message received for display on the next frame.
     if (showJson)
     {
         request.onRawResponse += (response) => pendingText = response;
     }
     request.onResponse += (r) =>
     {
         if (r.StatusCode == (int)HttpStatusCode.OK)
         {
             OnResponse(r.ResponseData);
         }
         else
         {
             OnError($"Error {r.StatusCode}", r.StatusDescription);
         }
     };
 }
예제 #10
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);
     });
 }
예제 #11
0
        // Perform a wit request and then apply data
        private static void PerformRequest(WitRequest request, Action <WitResponseNode, Action <string> > onApply, Action <string> onComplete)
        {
            // Add response delegate
            request.onResponse += (response) =>
            {
                // Get status
                int status = response.StatusCode;
                // Failed
                if (status != 200)
                {
                    onComplete($"Request Failed [{status}]: {response.StatusDescription}\nPath: {request}");
                }
                // Success
                else
                {
                    // Apply
                    onApply(response.ResponseData, (error) =>
                    {
                        // Apply failed
                        if (!string.IsNullOrEmpty(error))
                        {
                            onComplete?.Invoke($"Request Set Failed: {status}\nPath: {request}\nError: {error}");
                        }
                        // Complete
                        else
                        {
                            Log($"Request Success\nType: {request}", false);
                            onComplete?.Invoke("");
                        }
                    });
                }
            };

            // Perform
            Log($"Request Begin\nType: {request}", false);
            request.Request();
        }
예제 #12
0
 // Apply headers
 private static void ApplyHeaders(WitConfiguration configuration, UnityWebRequest unityRequest)
 {
     // Set authorization
     unityRequest.SetRequestHeader("Authorization", GetAuthorization(configuration));
     // Set user agent
     unityRequest.SetRequestHeader("User-Agent", WitRequest.GetUserAgent(configuration));
     // Set timeout
     unityRequest.timeout = configuration ? Mathf.CeilToInt(configuration.timeoutMS / 1000f) : 10;
     // Set custom headers
     if (OnProvideCustomHeaders != null)
     {
         foreach (Func <Dictionary <string, string> > del in OnProvideCustomHeaders.GetInvocationList())
         {
             Dictionary <string, string> customHeaders = del();
             if (customHeaders != null)
             {
                 foreach (var key in customHeaders.Keys)
                 {
                     unityRequest.SetRequestHeader(key, customHeaders[key]);
                 }
             }
         }
     }
 }
예제 #13
0
 private void OnRequestCreated(WitRequest request)
 {
     this.request = request;
     ResetStartTime();
 }