コード例 #1
0
        private void GetModelsResponse(RESTConnector.Request req, RESTConnector.Response resp)
        {
            TranslationModels           result     = new TranslationModels();
            fsData                      data       = null;
            Dictionary <string, object> customData = ((GetModelsReq)req).CustomData;

            customData.Add(Constants.String.RESPONSE_HEADERS, resp.Headers);

            if (resp.Success)
            {
                try
                {
                    fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
                    if (!r.Succeeded)
                    {
                        throw new WatsonException(r.FormattedMessages);
                    }

                    object obj = result;
                    r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
                    if (!r.Succeeded)
                    {
                        throw new WatsonException(r.FormattedMessages);
                    }

                    customData.Add("json", data);
                }
                catch (Exception e)
                {
                    Log.Error("LanguageTranslator.GetModelsResponse()", "GetModels Exception: {0}", e.ToString());
                    resp.Success = false;
                }
            }

            if (resp.Success)
            {
                if (((GetModelsReq)req).SuccessCallback != null)
                {
                    ((GetModelsReq)req).SuccessCallback(result, customData);
                }
            }
            else
            {
                if (((GetModelsReq)req).FailCallback != null)
                {
                    ((GetModelsReq)req).FailCallback(resp.Error, customData);
                }
            }
        }
コード例 #2
0
        private void GetModelsResponse(RESTConnector.Request req, RESTConnector.Response resp)
        {
            TranslationModels models = new TranslationModels();
            fsData            data   = null;

            if (resp.Success)
            {
                try
                {
                    fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
                    if (!r.Succeeded)
                    {
                        throw new WatsonException(r.FormattedMessages);
                    }

                    object obj = models;
                    r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
                    if (!r.Succeeded)
                    {
                        throw new WatsonException(r.FormattedMessages);
                    }
                }
                catch (Exception e)
                {
                    Log.Error("Natural Language Classifier", "GetModels Exception: {0}", e.ToString());
                    resp.Success = false;
                }
            }

            string customData = ((GetModelsReq)req).Data;

            if (((GetModelsReq)req).Callback != null)
            {
                ((GetModelsReq)req).Callback(resp.Success ? models : null, (!string.IsNullOrEmpty(customData) ? customData : data.ToString()));
            }
        }