Example #1
0
        static DiscogsApiClient CreateDiscogsApiClient(string discogsApiToken)
        {
            var credentials = DiscogsApiClientCredentials.FromToken(discogsApiToken);
            var environment = DiscogsApiClientEnvironment.CreateDefault(credentials);

            return(DiscogsApiClient.Create(environment));
        }
        public static async Task <T> Get <T>(
            this DiscogsApiClientEnvironment environment,
            string url,
            CancellationToken cancellationToken = default
            )
        {
            var httpMessageInvoker = environment.HttpMessageInvoker;
            var resp = await httpMessageInvoker.SendAsync(
                new HttpRequestMessage(HttpMethod.Get, url),
                cancellationToken
                ).ConfigureAwait(false);

            var json = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);

            var error = DeserializeJson <Error>(json);

            if (resp.StatusCode == HttpStatusCode.InternalServerError)
            {
                throw new DiscogsApiServerErrorException(error.Message);
            }

            if (resp.StatusCode == HttpStatusCode.NotFound)
            {
                throw new DiscorgsApiNotFoundException(error.Message);
            }

            if (error.Message != null)
            {
                if (error.Message == DiscogsApiQuotaException.ApiErrorMessage)
                {
                    throw new DiscogsApiQuotaException();
                }

                // TODO: rename the exception
                throw new DiscogsApiServerErrorException(error.Message);
            }

            return(DeserializeJson <T>(json));
        }