public BeGlobalV4Translator(string flavor) { _flavor = flavor; var studioCredentials = new StudioCredentials(); var accessToken = string.Empty; Application.Current?.Dispatcher?.Invoke(() => { accessToken = studioCredentials.GetToken(); }); accessToken = studioCredentials.GetToken(); _client = new RestClient($"{_url}/v4"); if (!string.IsNullOrEmpty(accessToken)) { _client.AddDefaultHeader("Authorization", $"Bearer {accessToken}"); } }
public BeGlobalV4Translator(BeGlobalTranslationOptions beGlobalTranslationOptions, MessageBoxService messageBoxService, TranslationProviderCredential credentials) { try { _messageBoxService = messageBoxService; _flavor = beGlobalTranslationOptions.Model; _authenticationMethod = beGlobalTranslationOptions.AuthenticationMethod; _studioCredentials = new StudioCredentials(); _client = new RestClient($"{Url}/v4") { CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore) }; if (!string.IsNullOrEmpty(_authenticationMethod)) { if (_authenticationMethod.Equals(Enums.GetDisplayName(Enums.LoginOptions.APICredentials))) { var splitedCredentials = credentials?.Credential.Split('#'); // the below condition is needed in case the ClientId is not set and credentials exists if (string.IsNullOrEmpty(beGlobalTranslationOptions.ClientId) && splitedCredentials.Length == 2 && !string.IsNullOrEmpty(splitedCredentials[0]) && !string.IsNullOrEmpty(splitedCredentials[1])) { beGlobalTranslationOptions.ClientId = splitedCredentials[0]; beGlobalTranslationOptions.ClientSecret = splitedCredentials[1]; } if (!string.IsNullOrEmpty(beGlobalTranslationOptions.ClientId) && !string.IsNullOrEmpty(beGlobalTranslationOptions.ClientSecret)) { var request = new RestRequest("/token", Method.POST) { RequestFormat = DataFormat.Json }; request.AddBody(new { clientId = beGlobalTranslationOptions.ClientId, clientSecret = beGlobalTranslationOptions.ClientSecret }); 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}"); } } else { var accessToken = string.Empty; Application.Current?.Dispatcher?.Invoke(() => { accessToken = _studioCredentials.GetToken(); }); accessToken = _studioCredentials.GetToken(); if (!string.IsNullOrEmpty(accessToken)) { _client.AddDefaultHeader("Authorization", $"Bearer {accessToken}"); } } } } catch (Exception ex) { Log.Logger.Error($"{Constants.BeGlobalV4Translator} {ex.Message}\n {ex.StackTrace}"); } }