Example #1
0
        public async Task <IActionResult> SetConfigurationSettings()
        {
            _logger.LogInformation("Setting the configuration settings.");

            string payload;

            using (var sr = new StreamReader(Request.Body))
            {
                payload = await sr.ReadToEndAsync();
            }

            var tenantId                    = Request.HttpContext.User.Claims.Single(c => c.Type == "X-LC-Tenant").Value;
            var configurationValues         = Newtonsoft.Json.JsonConvert.DeserializeObject <List <ConfigurationValueModel> >(payload);
            var configurationSettingsResult = await _accountService.SaveOrUpdateConfigurationSettings(tenantId, configurationValues, CancellationToken.None).ConfigureAwait(false);

            var resultValue = Content(JsonSerializer.Serialize(configurationSettingsResult, JsonSettings.Default()), "application/json", Encoding.UTF8);

            resultValue.StatusCode = 200;

            return(resultValue);
        }
Example #2
0
        public async Task <IActionResult> GetConfigurationSettings()
        {
            // All configuration settings must be returned to the caller.
            // Configurations that are secret will be returned with the value set to "*****", if they have a value.

            _logger.LogInformation("Retrieving the configuration settings.");

            var tenantId = Request.HttpContext.User.Claims.Single(c => c.Type == "X-LC-Tenant").Value;
            var configurationSettingsResult = await _accountService.GetConfigurationSettings(tenantId, true, CancellationToken.None).ConfigureAwait(false);

            var resultValue = Content(JsonSerializer.Serialize(configurationSettingsResult, JsonSettings.Default()), "application/json", Encoding.UTF8);

            resultValue.StatusCode = 200;

            return(resultValue);
        }
Example #3
0
        public async Task <IActionResult> AddonLifecycle()
        {
            string payload;

            using (var sr = new StreamReader(Request.Body))
            {
                payload = await sr.ReadToEndAsync();
            }

            var lifecycle = JsonSerializer.Deserialize <AddOnLifecycleEvent>(payload, JsonSettings.Default());

            switch (lifecycle.Id)
            {
            case AddOnLifecycleEventEnum.REGISTERED:
                _logger.LogInformation("Addon Registered Event Received.");
                // This is the event notifying that the Add-On has been registered in Language Cloud.
                // No further details are available for that event.
                break;

            case AddOnLifecycleEventEnum.ACTIVATED:
                // This is an Activation event, tenant id and it's public key must be saved to db.
                _logger.LogInformation("Addon Activated Event Received.");
                var activatedEvent = JsonSerializer.Deserialize <AddOnLifecycleEvent <ActivatedEvent> >(payload, JsonSettings.Default());

                var tenantId = activatedEvent.Data.TenantId;
                _logger.LogInformation($"Addon Activated Event Received for tenant id {tenantId}.");

                await _accountService.SaveAccountInfo(activatedEvent.Data, CancellationToken.None).ConfigureAwait(false);

                break;

            case AddOnLifecycleEventEnum.UNREGISTERED:
                // This is the event notifying that the Add-On has been unregistered/deleted from Language Cloud.
                // No further details are available for that event.
                _logger.LogInformation("Addon Unregistered Event Received.");
                // All the tenant information should be removed.
                await _accountService.RemoveAccounts(CancellationToken.None).ConfigureAwait(false);

                break;
            }

            return(Ok());
        }
Example #4
0
        public async Task <IActionResult> AccountLifecycle()
        {
            // This endpoint receives events related to an Account where the Add-On has been activated
            // and the requests are authenticated.
            // Currently this is only the DEACTIVATED event.
            var tenantId = Request.HttpContext.User.Claims.Single(c => c.Type == "X-LC-Tenant").Value;

            var sr      = new StreamReader(Request.Body);
            var payload = await sr.ReadToEndAsync();

            var lifecycle = JsonSerializer.Deserialize <AccountLifecycleEvent>(payload, JsonSettings.Default());

            switch (lifecycle.Id)
            {
            case AccountLifecycleEventEnum.DEACTIVATED:
                // This is the event notifying that the Add-On has been uninstalled from a tenant account.
                // No further details are available for that event.
                _logger.LogInformation("Addon Deactivated Event Received.");
                await _accountService.RemoveAccountInfo(tenantId, CancellationToken.None).ConfigureAwait(false);

                break;
            }

            return(Ok());
        }
        private List <string> GetTranslations(TranslationRequest translationRequest, TranslationEngine translationEngine, string apiKey, string formality)
        {
            var translations = new List <string>();

            Parallel.ForEach(translationRequest.Contents, (text) =>
            {
                var encodedText      = HttpUtility.UrlEncode(text);
                using var httpClient = new HttpClient();

                var content = new StringContent($"text={encodedText}" +
                                                $"&source_lang={translationEngine.EngineSourceLanguage}" +
                                                $"&target_lang={translationEngine.EngineTargetLanguage}" +
                                                $"&formality={formality.ToLower()}" +
                                                "&preserve_formatting=1" +
                                                "&tag_handling=xml" +
                                                $"&auth_key={apiKey}", Encoding.UTF8, "application/x-www-form-urlencoded");

                var response = httpClient.PostAsync("https://api.deepl.com/v2/translate", content).Result;
                if (response.IsSuccessStatusCode)
                {
                    var translationResponse = response.Content?.ReadAsStringAsync().Result;
                    var translatedObject    = JsonSerializer.Deserialize <DeeplTranslationResponse>(translationResponse, JsonSettings.Default());
                    var translatedText      = translatedObject.Translations[0].Text;

                    translations.Add(translatedText);
                }
                else
                {
                    var responseContent = response.Content?.ReadAsStringAsync().Result;

                    throw new AddonValidationException($"Request to DeepL Translate endpoint failed with status code {response.StatusCode}", new Details {
                        Code = ErrorCodes.GeneralError, Value = responseContent
                    });
                }
            });
            return(translations);
        }