Пример #1
0
        async public Task <dynamic> PairsAsync(string srtName)
        {
            string url = String.Format("ai/text/translate/routing/{0}/pairs", srtName);

            dynamic jsonResult;

            using (HttpConnector conn = new HttpConnector(Intento))
                jsonResult = await conn.GetAsync(url);

            return(jsonResult);
        }
Пример #2
0
        /// <summary>
        /// Detailed information on provider features
        /// </summary>
        /// <param name="provider">Provider id</param>
        /// <returns>dynamic (json) with requested information</returns>
        async public Task <dynamic> ProviderAsync(string providerId, Dictionary <string, string> additionalParams = null)
        {
            string path = string.Format("ai/text/translate/{0}", providerId);

            dynamic jsonResult;

            // Call to Intento API and get json result
            using (HttpConnector conn = new HttpConnector(Intento))
                jsonResult = await conn.GetAsync(path, additionalParams : additionalParams);

            return(jsonResult);
        }
Пример #3
0
        async public Task <IList <dynamic> > ProvidersAsync(string to = null, string from = null, bool lang_detect = false, bool bulk = false,
                                                            Dictionary <string, string> filter = null)
        {
            Dictionary <string, string> f = filter == null ? new Dictionary <string, string>() : new Dictionary <string, string>(filter);

            if (!string.IsNullOrEmpty(to))
            {
                f["to"] = to;
            }
            if (!string.IsNullOrEmpty(from))
            {
                f["from"] = from;
            }
            if (lang_detect)
            {
                f["lang_detect"] = "true";
            }
            if (bulk)
            {
                f["bulk"] = "true";
            }

            List <string> p = new List <string>();

            foreach (KeyValuePair <string, string> pair in f)
            {
                p.Add(string.Format("{0}={1}", pair.Key, HttpUtility.UrlEncode(pair.Value)));
            }
            string url = "ai/text/translate";

            if (p.Count != 0)
            {
                url += "?" + string.Join("&", p);
            }

            dynamic jsonResult;

            // Call to Intento API and get json result
            using (HttpConnector conn = new HttpConnector(Intento))
                jsonResult = await conn.GetAsync(url);

            List <dynamic> providers = new List <dynamic>();

            foreach (dynamic providerInfo in jsonResult)
            {
                providers.Add(providerInfo);
            }

            return(providers);
        }
        /// <summary>
        /// Detailed information on provider features
        /// </summary>
        /// <param name="provider">Provider id</param>
        /// <returns>dynamic (json) with requested information</returns>
        async public Task <dynamic> ProviderAsync(string providerId, string additionalParams = null)
        {
            string path = string.Format("ai/text/translate/{0}", providerId);

            if (additionalParams != null)
            {
                path += additionalParams;
            }

            // Call to Intento API and get json result
            HttpConnector conn       = new HttpConnector(Intento);
            dynamic       jsonResult = await conn.GetAsync(path);

            return(jsonResult);
        }
Пример #5
0
        async public Task <dynamic> CheckAsyncJobAsync(string asyncId)
        {
            // Open connection to Intento API and set ApiKey
            Log(string.Format("CheckAsyncJobAsync-1: {0}ms", asyncId));
            HttpConnector client = new HttpConnector(this);

            Log(string.Format("CheckAsyncJobAsync-2: {0}ms", asyncId));

            // async operations inside
            Log(string.Format("CheckAsyncJobAsync-3: {0}ms", asyncId));
            dynamic result = await client.GetAsync(string.Format("operations/{0}", asyncId));

            Log(string.Format("CheckAsyncJobAsync-4: {0}ms", asyncId));
            return(result);
        }
        async public Task <IList <dynamic> > LanguagesAsync()
        {
            string url = "ai/text/translate/languages";

            // Call to Intento API and get json result
            HttpConnector conn       = new HttpConnector(Intento);
            dynamic       jsonResult = await conn.GetAsync(url);

            List <dynamic> languages = new List <dynamic>();

            foreach (dynamic languageInfo in jsonResult)
            {
                languages.Add(languageInfo);
            }

            return(languages);
        }
Пример #7
0
        public async Task <IList <dynamic> > AgnosticGlossariesTypesAsync()
        {
            var     path = $"ai/text/glossaries/v2/cs_types";
            dynamic jsonResult;

            // Call to Intento API and get json result
            using (HttpConnector conn = new HttpConnector(Intento))
            {
                jsonResult = await conn.GetAsync(path);
            }
            var types = new List <dynamic>();

            foreach (dynamic type in jsonResult.types)
            {
                types.Add(type);
            }
            return(types);
        }
Пример #8
0
        async public Task <IList <dynamic> > DelegatedCredentialsAsync(Dictionary <string, string> additionalParams = null)
        {
            string path = "delegated_credentials";

            dynamic jsonResult;

            // Call to Intento API and get json result
            using (HttpConnector conn = new HttpConnector(Intento))
                jsonResult = await conn.GetAsync(path, additionalParams : additionalParams);

            List <dynamic> credentials = new List <dynamic>();

            foreach (dynamic credential in jsonResult)
            {
                credentials.Add(credential);
            }

            return(credentials);
        }
Пример #9
0
        /// <summary>
        /// Details of the models stored by the provider
        /// </summary>
        /// <param name="provider">Provider id</param>
        /// <param name="additionalParams">additional url params</param>
        /// <returns>dynamic (json) with requested information</returns>
        async public Task <IList <dynamic> > RoutingAsync(Dictionary <string, string> additionalParams = null)
        {
            string path = "ai/text/translate/routing";

            dynamic jsonResult;

            // Call to Intento API and get json result
            using (HttpConnector conn = new HttpConnector(Intento))
                jsonResult = await conn.GetAsync(path, additionalParams : additionalParams);

            List <dynamic> models = new List <dynamic>();

            foreach (dynamic model in ((JContainer)jsonResult).First.First)
            {
                models.Add(model);
            }

            return(models);
        }
Пример #10
0
        /// <summary>
        /// Details of the models stored by the provider
        /// </summary>
        /// <param name="provider">Provider id</param>
        /// <param name="additionalParams">additional url params</param>
        /// <returns>dynamic (json) with requested information</returns>
        async public Task <IList <dynamic> > AccountsAsync(string providerId = null, Dictionary <string, string> additionalParams = null)
        {
            string path = string.Format("accounts" + (providerId != null ? $"?provider={providerId}" : null));

            dynamic jsonResult;

            // Call to Intento API and get json result
            using (HttpConnector conn = new HttpConnector(Intento))
                jsonResult = await conn.GetAsync(path, additionalParams : additionalParams);

            List <dynamic> models = new List <dynamic>();

            foreach (dynamic model in ((JContainer)jsonResult).First.First)
            {
                models.Add(model);
            }

            return(models);
        }
Пример #11
0
        /// <summary>
        /// Get a list of available delegated credentials
        /// </summary>
        /// <param name="additionalParams">additional url params</param>
        /// <returns>dynamic (json) with requested information</returns>
        async public Task <IList <dynamic> > DelegatedCredentialsAsync(string additionalParams = null)
        {
            string path = "delegated_credentials";

            if (additionalParams != null)
            {
                path += additionalParams;
            }

            // Call to Intento API and get json result
            HttpConnector conn       = new HttpConnector(Intento);
            dynamic       jsonResult = await conn.GetAsync(path);

            List <dynamic> credentials = new List <dynamic>();

            foreach (dynamic credential in jsonResult)
            {
                credentials.Add(credential);
            }

            return(credentials);
        }
Пример #12
0
        /// <summary>
        /// Details of the glossaries stored by the provider
        /// </summary>
        /// <param name="provider">Provider id</param>
        /// <param name="credential_id">Credential id</param>
        /// <param name="additionalParams">additional url params</param>
        /// <returns>dynamic (json) with requested information</returns>
        async public Task <IList <dynamic> > GlossariesAsync(string providerId, Dictionary <string, string> credential_id, string additionalParams = null)
        {
            string path = string.Format("ai/text/translate/glossaries?provider={0}", providerId);

            if (credential_id != null)
            {
                string json;
                if (credential_id.Count != 0)
                {
                    if (credential_id.Count == 1 && credential_id.ContainsKey("credential_id"))
                    {
                        json = credential_id["credential_id"];
                    }
                    else
                    {
                        json = JsonConvert.SerializeObject(credential_id, Formatting.Indented);
                        json = HttpUtility.UrlEncode(json);
                    }
                    path += String.Format("&credential_id={0}", json);
                }
            }
            if (additionalParams != null)
            {
                path += additionalParams;
            }

            // Call to Intento API and get json result
            HttpConnector conn       = new HttpConnector(Intento);
            dynamic       jsonResult = await conn.GetAsync(path);

            List <dynamic> glossaries = new List <dynamic>();

            foreach (dynamic glossary in ((JContainer)jsonResult).First.First)
            {
                glossaries.Add(glossary);
            }

            return(glossaries);
        }
Пример #13
0
        /// <summary>
        /// Details of the models stored by the provider
        /// </summary>
        /// <param name="provider">Provider id</param>
        /// <param name="credential_id">Credential id</param>
        /// <param name="additionalParams">additional url params</param>
        /// <returns>dynamic (json) with requested information</returns>
        async public Task <IList <dynamic> > ModelsAsync(string providerId, Dictionary <string, string> credentials, Dictionary <string, string> additionalParams = null)
        {
            string path = string.Format("ai/text/translate/models?provider={0}", providerId);

            if (credentials != null)
            {
                string json;
                if (credentials.Count != 0)
                {
                    if (credentials.Count == 1 && credentials.ContainsKey("credential_id"))
                    {
                        json = credentials["credential_id"];
                    }
                    else
                    {
                        json = JsonConvert.SerializeObject(credentials, Formatting.None);
                        json = HttpUtility.UrlEncode(json);
                    }
                    path += String.Format("&credential_id={0}", json);
                }
            }

            dynamic jsonResult;

            // Call to Intento API and get json result
            using (HttpConnector conn = new HttpConnector(Intento))
                jsonResult = await conn.GetAsync(path, additionalParams : additionalParams);

            List <dynamic> models = new List <dynamic>();

            foreach (dynamic model in ((JContainer)jsonResult).First.First)
            {
                models.Add(model);
            }

            return(models);
        }
Пример #14
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);
        }
Пример #15
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,
                                                 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);
        }