Ejemplo n.º 1
0
        /// <summary>
        /// Do a Web Get for the given request Uri .
        /// </summary>
        /// <typeparam name="T">The Type of object to serialize the object into.</typeparam>
        /// <param name="requestUri">The request URL to append to the end of the base Uri.</param>
        /// <returns>The given <see cref="T"/> object.</returns>
        protected async Task <T> WebGetAsync <T>(Uri requestUri) where T : class
        {
            RateLimitManager.Instance.HandleRate(ApiKey);

            using (var client = new HttpClient())
            {
                client.BaseAddress = BaseUri;
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.GetAsync(BuildUri(requestUri));

                if (response.IsSuccessStatusCode)
                {
                    string jsonString = response.Content.ReadAsStringAsync().Result;

                    T obj = WebUtility.DeSerializeJson <T>(jsonString);
                    return(obj);
                }
                else
                {
                    string message = await response.Content.ReadAsStringAsync();

                    switch ((int)response.StatusCode)
                    {
                    case (int)LoLApiErrors.BadRequest:
                        throw new LoLApiException <BadRequest>(message);

                    case (int)LoLApiErrors.Unauthorized:
                        throw new LoLApiException <Unauthorized>(message);

                    case (int)LoLApiErrors.DataNotFound:
                        throw new LoLApiException <DataNotFound>(message);

                    case (int)LoLApiErrors.RateLimitExceeded:
                        throw new LoLApiException <RateLimitExceeded>(message);

                    case (int)LoLApiErrors.InternalServerError:
                        throw new LoLApiException <InternalServerError>(message);

                    case (int)LoLApiErrors.ServiceUnavailable:
                        throw new LoLApiException <ServiceUnavailable>(message);

                    default:
                        response.EnsureSuccessStatusCode();
                        break;
                    }
                }
            }

            return(null);
        }