Exemple #1
0
        public WorldOfWarcraftClient(WorldOfWarcraftClientSettings settings)
        {
            _clientSettings = settings ?? throw new ArgumentException("WorldOfWarcraftClient settings must be provided.");
            if ((string.IsNullOrWhiteSpace(_clientSettings.ClientId) || string.IsNullOrWhiteSpace(_clientSettings.ClientSecret)) && _clientSettings.Token == null)
            {
                throw new ArgumentException("Either the Client ID/Client Secret or the BattleNetToken must be provided.");
            }
            if (string.IsNullOrWhiteSpace(_clientSettings.Locale))
            {
                throw new ArgumentException("Locale must be provided.");
            }
            if (string.IsNullOrWhiteSpace(_clientSettings.Region))
            {
                throw new ArgumentException("Region must be provided.");
            }

            if (settings.Token == null)
            {
                _tokenClient = new TokenClient(new TokenClientSettings
                {
                    ClientId     = _clientSettings.ClientId,
                    ClientSecret = _clientSettings.ClientSecret,
                    Scope        = "wow.profile",
                    Region       = _clientSettings.Region
                });
                _clientSettings.Token = _tokenClient.GetClientCredentialsTokenAsync().GetAwaiter().GetResult();
            }
            _restClient = new RestClient(new RestClientSettings
            {
                Token = _clientSettings.Token,
            });
            _apiBaseUrl = $"https://{_clientSettings.Region}.api.blizzard.com";
            Characters  = new CharactersClient(this);
            Realms      = new RealmsClient(this);
        }
        protected override async Task <AccessToken> GetAccessTokenAsync(CancellationToken cancellationToken = default)
        {
            var response = await _tokenClient.GetClientCredentialsTokenAsync(_scope, cancellationToken)
                           .ConfigureAwait(false);

            //todo: handle errors
            if (!response.IsError)
            {
                return(Helpers.CreateAccessToken(response));
            }

            _logger.LogError(response.Exception,
                             "Error fetching client credentials token. {Error} ({ErrorDescription})", response.Error,
                             response.ErrorDescription);

            return(null);
        }