Ejemplo n.º 1
0
        /// <summary>
        ///     Retrieve an item of type <typeparamref name="T"/> from the Blizzard World of Warcraft Game Data or Profile API.
        /// </summary>
        /// <typeparam name="T">
        ///     The return type.
        /// </typeparam>
        /// <param name="requestUri">
        ///     The URI the request is sent to.
        /// </param>
        /// <param name="accessToken">
        ///     The OAuth access token.
        /// </param>
        /// <returns>
        ///     The JSON response, deserialized to an object of type <typeparamref name="T"/>.
        /// </returns>
        private async Task <RequestResult <T> > GetAsync <T>(string requestUri, string accessToken)
        {
            // Add an authentication header with the token.
            _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

            //Wait for access to send
            _queueManager.WaitForAccessToSend();

            // Retrieve the response.
            HttpResponseMessage response = await _client.GetAsync(requestUri).ConfigureAwait(false);

            if (!response.IsSuccessStatusCode)
            {
                // Check if the request was successful and made it to the Blizzard API.
                // The API will always send back content if successful.
                if (response.Content != null)
                {
                    string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    if (!string.IsNullOrEmpty(content))
                    {
                        RequestResult <T> requestError = JsonSerializer.Deserialize <RequestError>(content);
                        return(requestError);
                    }
                }

                // If not then it is most likely a problem on our end due to an HTTP error.
                string message = $"Response code {(int)response.StatusCode} ({response.ReasonPhrase}) does not indicate success. Request: {requestUri}";

                throw new HttpRequestException(message);
            }

            // Deserialize an object of type T from the JSON string.
            string json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            try
            {
                RequestResult <T> requestResult = JsonSerializer.Deserialize <T>(json, s_jsonSerializerOptions);
                return(requestResult);
            }
            catch (JsonException ex)
            {
                var requestError = new RequestError
                {
                    Code   = null,
                    Detail = ex.Message,
                    Type   = typeof(JsonException).ToString()
                };
                return(new RequestResult <T>(requestError));
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 ///     Initializes a request result with an error.
 /// </summary>
 public RequestResult(RequestError error)
 {
     Error   = error ?? throw new ArgumentNullException(nameof(error));
     Success = false;
 }