Пример #1
0
        async public Task <dynamic> FulfillAsync(object text, string to, string from = null, string provider = null,
                                                 bool async            = false, bool wait_async       = false, string format = null, object auth = null,
                                                 string custom_model   = null, string glossary        = null, int[] intentoGlossary = null,
                                                 object pre_processing = null, object post_processing = null,
                                                 bool failover         = false, object failover_list  = null, string routing = null, bool trace = false,
                                                 Dictionary <string, string> special_headers = null, bool useSyncwrapper = false)
        {
            dynamic preProcessingJson  = GetJson(pre_processing, "pre_processing");
            dynamic postProcessingJson = GetJson(post_processing, "post_processing");

            dynamic json = new JObject();

            // ------ context section
            dynamic context = new JObject();

            int textLength = 0;

            // text
            if (text == null)
            {
                context.text = "";
            }
            else if (text is IEnumerable <string> )
            {
                context.text = GetJson(((IEnumerable <string>)text).Select(i => i == null ? "" : i), "text");
                textLength   = ((IEnumerable <string>)text).Where(x => x != null).Sum(i => i.Length);
            }
            else
            {
                context.text = GetJson(text.ToString(), "text") ?? "";
                textLength   = ((string)text).Length;
            }

            // determination of the possibility of using the syncwrapper
            // maximum 10k characters
            if (useSyncwrapper)
            {
                if (textLength < 10000)
                {
                    async = !useSyncwrapper;
                }
                else
                {
                    useSyncwrapper = false;
                }
            }

            // to
            context.to = to;

            // from
            if (!string.IsNullOrWhiteSpace(from))
            {
                context.from = from;
            }

            // format
            if (!string.IsNullOrWhiteSpace(format))
            {
                context.format = format;
            }

            // custom_model
            if (!string.IsNullOrWhiteSpace(custom_model))
            {
                context.category = custom_model;
            }

            // glossary
            if (!string.IsNullOrWhiteSpace(glossary))
            {
                context.glossary = glossary;
            }

            // intento glossary
            if (intentoGlossary != null && intentoGlossary.Length > 0)
            {
                dynamic intentoG = JArray.FromObject(intentoGlossary.Select(g => JObject.FromObject(new { id = g })));
                context.glossary = intentoG;
            }

            json.context = context;

            // ----- service section
            dynamic service = new JObject();

            // provider
            if (!string.IsNullOrWhiteSpace(provider))
            {
                service.provider = provider;
            }

            // async parameter
            if (async)
            {
                service.async = true;
            }

            // auth parameter
            service.auth = GetJson(auth, "auth");

            // routing
            if (!string.IsNullOrEmpty(routing))
            {
                service.routing = routing;
            }

            // pre-post processing paramters
            if (preProcessingJson != null || postProcessingJson != null)
            {
                dynamic processing = new JObject();
                if (preProcessingJson != null)
                {
                    processing.pre = preProcessingJson;
                }
                if (postProcessingJson != null)
                {
                    processing.post = postProcessingJson;
                }
                service.processing = processing;
            }

            // failover parameters
            if (failover)
            {
                service.failover = true;

                JArray failoverJson;
                if (failover_list != null)
                {
                    if (failover_list is string)
                    {
                        failoverJson = JArray.Parse((string)failover_list);
                    }
                    else if (failover_list is JArray)
                    {
                        failoverJson = (JArray)failover_list;
                    }
                    else
                    {
                        throw new IntentoInvalidParameterException("failover_list", "need to json-list-string or Newtonsoft JArray");
                    }
                    service.failover_list = failover_list;
                }
            }

            json.service = service;

            dynamic jsonResult;
            string  url = "ai/text/translate";

            if (trace)
            {
                url += "?trace=true";
            }

            // Call to Intento API and get json result
            using (HttpConnector conn = new HttpConnector(Intento))
            {
                jsonResult = await conn.PostAsync(url, json, useSyncwrapper : useSyncwrapper);
            }

            if (async && wait_async)
            {   // async opertation (in terms of IntentoApi) and we need to wait result of it
                string id = jsonResult.id;

                // In case of Sandbox key and some errors in parameters request to IntentoAPI may return:
                // 1. id: async operation started
                // 2. result of translation: Sandcox key
                // 3. error: validation of arameters failed (like request to translate html with provider with no such capabilities)
                if (id == null)
                {
                    // Not a (1) - return result immediately, nothing to wait
                    return(jsonResult);
                }

                jsonResult = await Intento.WaitAsyncJobAsync(id);
            }
            if (useSyncwrapper)
            {
                JObject response = new JObject();
                response["results"] = jsonResult.results;
                response["meta"]    = jsonResult.meta;
                response["service"] = jsonResult.service;
                ((JObject)jsonResult)["response"] = new JArray()
                {
                    response
                };
                jsonResult.meta["providers"] = new JArray()
                {
                    jsonResult.service.provider
                };
            }

            return(jsonResult);
        }
        async public Task <dynamic> FulfillAsync(object text, string to, string from = null, string provider = null,
                                                 bool async            = false, bool wait_async       = false, string format = null, object auth = null,
                                                 string custom_model   = null, string glossary        = null,
                                                 object pre_processing = null, object post_processing = null,
                                                 bool failover         = false, object failover_list  = null, string routing = null, bool trace = false, bool deserialize_text = false)
        {
            dynamic preProcessingJson  = GetJson(pre_processing, "pre_processing");
            dynamic postProcessingJson = GetJson(post_processing, "post_processing");

            dynamic json = new JObject();

            // ------ context section
            dynamic context = new JObject();

            // text
            if (text == null)
            {
                context.text = "";
            }
            else if (text is IEnumerable <string> )
            {
                context.text = GetJson(((IEnumerable <string>)text).Select(i => i == null ? "" : i), "text");
            }
            else
            {
                context.text = GetJson(text.ToString(), "text") ?? "";
            }

            // to
            context.to = to;

            // from
            if (!string.IsNullOrWhiteSpace(from))
            {
                context.from = from;
            }

            // format
            if (!string.IsNullOrWhiteSpace(format))
            {
                context.format = format;
            }

            // custom_model
            if (!string.IsNullOrWhiteSpace(custom_model))
            {
                context.category = custom_model;
            }

            // glossary
            if (!string.IsNullOrWhiteSpace(glossary))
            {
                context.glossary = glossary;
            }

            json.context = context;

            // ----- service section
            dynamic service = new JObject();

            // provider
            if (!string.IsNullOrWhiteSpace(provider))
            {
                service.provider = provider;
            }

            // async parameter
            if (async)
            {
                service.async = true;
            }

            // auth parameter
            service.auth = GetJson(auth, "auth");

            // routing
            if (!string.IsNullOrEmpty(routing))
            {
                service.routing = routing;
            }

            // pre-post processing paramters
            if (preProcessingJson != null || postProcessingJson != null)
            {
                dynamic processing = new JObject();
                if (preProcessingJson != null)
                {
                    processing.pre = preProcessingJson;
                }
                if (postProcessingJson != null)
                {
                    processing.post = postProcessingJson;
                }
                service.processing = processing;
            }

            // failover parameters
            if (failover)
            {
                service.failover = true;

                JArray failoverJson;
                if (failover_list != null)
                {
                    if (failover_list is string)
                    {
                        failoverJson = JArray.Parse((string)failover_list);
                    }
                    else if (failover_list is JArray)
                    {
                        failoverJson = (JArray)failover_list;
                    }
                    else
                    {
                        throw new IntentoInvalidParameterException("failover_list", "need to json-list-string or Newtonsoft JArray");
                    }
                    service.failover_list = failover_list;
                }
            }

            json.service = service;

            // Call to Intento API and get json result
            HttpConnector conn = new HttpConnector(Intento);
            string        url  = "ai/text/translate";

            if (trace)
            {
                url += "?trace=true";
            }

            dynamic jsonResult = await conn.PostAsync(url, json);

            if (async && wait_async)
            {   // async opertation (in terms of IntentoApi) and we need to wait result of it
                string id = jsonResult.id;

                // In case of Sandbox key and some errors in parameters request to IntentoAPI may return:
                // 1. id: async operation started
                // 2. result of translation: Sandcox key
                // 3. error: validation of arameters failed (like request to translate html with provider with no such capabilities)
                if (id == null)
                {
                    // Not a (1) - return result immediately, nothing to wait
                    return(jsonResult);
                }

                jsonResult = await Intento.WaitAsyncJobAsync(id);
            }

            return(jsonResult);
        }