public void ParseHeadersTest()
        {
            try
            {
                // Test exception is thrown.
                MtgApiController.ParseHeaders(null);
                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                Assert.AreEqual("headers", ex.ParamName);
            }
            catch
            {
                Assert.Fail();
            }

            HttpClient          client   = new HttpClient();
            HttpResponseMessage response = new HttpResponseMessage();

            // No headers.
            MtgApiController.ParseHeaders(response.Headers);
            Assert.IsNull(MtgApiController.Link);
            Assert.AreEqual(0, MtgApiController.PageSize);
            Assert.AreEqual(0, MtgApiController.Count);
            Assert.AreEqual(0, MtgApiController.TotalCount);
            Assert.AreEqual(0, MtgApiController.RatelimitLimit);
            Assert.AreEqual(0, MtgApiController.RatelimitRemaining);

            response.Headers.Add("Link", "fakelink");
            MtgApiController.ParseHeaders(response.Headers);
            Assert.AreEqual("fakelink", MtgApiController.Link);

            response.Headers.Add("Page-Size", "2000");
            MtgApiController.ParseHeaders(response.Headers);
            Assert.AreEqual(2000, MtgApiController.PageSize);

            response.Headers.Add("Count", "1000");
            MtgApiController.ParseHeaders(response.Headers);
            Assert.AreEqual(1000, MtgApiController.Count);

            response.Headers.Add("Total-Count", "3000");
            MtgApiController.ParseHeaders(response.Headers);
            Assert.AreEqual(3000, MtgApiController.TotalCount);

            response.Headers.Add("Ratelimit-Limit", "500");
            MtgApiController.ParseHeaders(response.Headers);
            Assert.AreEqual(500, MtgApiController.RatelimitLimit);

            response.Headers.Add("Ratelimit-Remaining", "250");
            MtgApiController.ParseHeaders(response.Headers);
            Assert.AreEqual(250, MtgApiController.RatelimitRemaining);

            var privateController = new PrivateType(typeof(MtgApiController));

            privateController.SetStaticFieldOrProperty("RatelimitLimit", 2000);
        }
        public void ParseHeadersTest()
        {
            try
            {
                // Test exception is thrown.
                MtgApiController.ParseHeaders(null);
                Assert.True(false);
            }
            catch (ArgumentNullException ex)
            {
                Assert.Equal("headers", ex.ParamName);
            }
            catch
            {
                Assert.True(false);
            }

            HttpClient          client   = new HttpClient();
            HttpResponseMessage response = new HttpResponseMessage();

            // No headers.
            MtgApiController.ParseHeaders(response.Headers);
            Assert.Null(MtgApiController.Link);
            Assert.Equal(0, MtgApiController.PageSize);
            Assert.Equal(0, MtgApiController.Count);
            Assert.Equal(0, MtgApiController.TotalCount);
            Assert.Equal(0, MtgApiController.RatelimitLimit);
            Assert.Equal(0, MtgApiController.RatelimitRemaining);

            response.Headers.Add("Link", "fakelink");
            MtgApiController.ParseHeaders(response.Headers);
            Assert.Equal("fakelink", MtgApiController.Link);

            response.Headers.Add("Page-Size", "2000");
            MtgApiController.ParseHeaders(response.Headers);
            Assert.Equal(2000, MtgApiController.PageSize);

            response.Headers.Add("Count", "1000");
            MtgApiController.ParseHeaders(response.Headers);
            Assert.Equal(1000, MtgApiController.Count);

            response.Headers.Add("Total-Count", "3000");
            MtgApiController.ParseHeaders(response.Headers);
            Assert.Equal(3000, MtgApiController.TotalCount);

            response.Headers.Add("Ratelimit-Limit", "500");
            MtgApiController.ParseHeaders(response.Headers);
            Assert.Equal(500, MtgApiController.RatelimitLimit);

            response.Headers.Add("Ratelimit-Remaining", "250");
            MtgApiController.ParseHeaders(response.Headers);
            Assert.Equal(250, MtgApiController.RatelimitRemaining);
        }
Example #3
0
        /// <summary>
        /// Do a Web Get for the given request Uri .
        /// </summary>
        /// <typeparam name="T">The type to serialize into.</typeparam>
        /// <param name="requestUri">The URL to call.</param>
        /// <returns>The serialized response.</returns>
        public async Task <T> WebGetAsync <T>(Uri requestUri)
            where T : MtgResponseBase
        {
            if (requestUri == null)
            {
                throw new ArgumentNullException("requestUri");
            }

            using (var client = new HttpClient())
            {
                using (var response = await client.GetAsync(requestUri).ConfigureAwait(false))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        MtgApiController.ParseHeaders(response.Headers);
                        return(JsonConvert.DeserializeObject <T>(await response.Content.ReadAsStringAsync().ConfigureAwait(false)));
                    }
                    else
                    {
                        switch ((int)response.StatusCode)
                        {
                        case (int)MtgApiError.BadRequest:
                            throw new MtgApiException <BadRequestException>(MtgApiError.BadRequest.GetDescription());

                        case (int)MtgApiError.Forbidden:
                            throw new MtgApiException <ForbiddenException>(MtgApiError.Forbidden.GetDescription());

                        case (int)MtgApiError.InternalServerError:
                            throw new MtgApiException <InternalServerErrorException>(MtgApiError.InternalServerError.GetDescription());

                        case (int)MtgApiError.NotFound:
                            throw new MtgApiException <NotFoundException>(MtgApiError.NotFound.GetDescription());

                        case (int)MtgApiError.ServiceUnavailable:
                            throw new MtgApiException <ServiceUnavailableException>(MtgApiError.ServiceUnavailable.GetDescription());

                        default:
                            response.EnsureSuccessStatusCode();
                            return(null);
                        }
                    }
                }
            }
        }