コード例 #1
0
        private void OnCreateModelResponse(RESTConnector.Request req, RESTConnector.Response resp)
        {
            TranslationModel            result     = new TranslationModel();
            fsData                      data       = null;
            Dictionary <string, object> customData = ((CreateModelRequest)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.OnCreateModelResponse()", "Create model Exception: {0}", e.ToString());
                    resp.Success = false;
                }
            }

            if (resp.Success)
            {
                if (((CreateModelRequest)req).SuccessCallback != null)
                {
                    ((CreateModelRequest)req).SuccessCallback(result, customData);
                }
            }
            else
            {
                if (((CreateModelRequest)req).FailCallback != null)
                {
                    ((CreateModelRequest)req).FailCallback(resp.Error, customData);
                }
            }
        }
コード例 #2
0
        private void OnCreateModelResponse(RESTConnector.Request req, RESTConnector.Response resp)
        {
            TranslationModel result = new TranslationModel();
            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 = result;
                    r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
                    if (!r.Succeeded)
                    {
                        throw new WatsonException(r.FormattedMessages);
                    }
                }
                catch (Exception e)
                {
                    Log.Error("Discovery", "Create model Exception: {0}", e.ToString());
                    resp.Success = false;
                }
            }

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

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