public void UpdateLanguageMappings()
 {
     try
     {
         LanguageMappings = LanguageMappingsService?.GetLanguageMappingSettings()?.LanguageMappings?.ToList();
     }
     catch
     {
         // catch all;
         // reset the setttings to default in the event that the lanauge model structure has changed (for whatever reason);
         LanguageMappingsService?.RemoveLanguageMappingSettings();
         LanguageMappings = new List <LanguageMappingModel>();
     }
 }
示例#2
0
        public SdlMTCloudTranslator(string server, SdlMTCloudTranslationOptions options)
        {
            try
            {
                _messageBoxService      = new MessageBoxService();
                _languageMappingService = new LanguageMappingsService();
                _languageMappings       = _languageMappingService.GetLanguageMappingSettings()?.LanguageMappings?.ToList();
                _client = new RestClient(string.Format($"{server}/v4"));

                Utils.LogServerIPAddresses();

                IRestRequest request;
                if (options.AuthenticationMethod.Equals("ClientLogin"))
                {
                    request = new RestRequest("/token", Method.POST)
                    {
                        RequestFormat = DataFormat.Json
                    };
                    request.AddBody(new { clientId = options.ClientId, clientSecret = options.ClientSecret });
                }
                else
                {
                    request = new RestRequest("/token/user", Method.POST)
                    {
                        RequestFormat = DataFormat.Json
                    };
                    request.AddBody(new { username = options.ClientId, password = options.ClientSecret });
                }
                AddTraceId(request);
                request.RequestFormat = DataFormat.Json;

                var response = _client.Execute(request);
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new Exception(Constants.TokenFailed + response.Content);
                }
                dynamic json = JsonConvert.DeserializeObject(response.Content);
                _client.AddDefaultHeader("Authorization", $"Bearer {json.accessToken}");
            }
            catch (Exception ex)
            {
                Log.Logger.Error($"{Constants.BeGlobalV4Translator} {ex.Message}\n {ex.StackTrace}");
            }
        }