Exemplo n.º 1
0
        public async Task SettingPreviewApiShouldUseCorrectApiKey()
        {
            //Arrange
            _handler = new FakeMessageHandler();
            var httpClient = new HttpClient(_handler);

            _client = new ContentfulClient(httpClient, new ContentfulOptions()
            {
                DeliveryApiKey              = "123",
                ManagementApiKey            = "123",
                PreviewApiKey               = "ABC-PREVIEW",
                SpaceId                     = "666",
                UsePreviewApi               = true,
                MaxNumberOfRateLimitRetries = 3
            });
            var authHeader = "";

            _handler.Response      = GetResponseFromFile(@"SampleEntry.json");
            _handler.VerifyRequest = (HttpRequestMessage request) =>
            {
                authHeader = request.Headers.GetValues("Authorization").First();
            };

            //Act
            var res = await _client.GetEntry <Entry <TestEntryModel> >("12");

            //Assert
            Assert.Equal("Bearer ABC-PREVIEW", authHeader);
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Contact()
        {
            HttpClient httpClient = new HttpClient();
            var        options    = new ContentfulOptions
            {
                DeliveryApiKey = "4df4c5c7ed276605d36b9d880f1455aea05fe9b47e2da6acf518c0771d300625",
                PreviewApiKey  = "3aaec008db04a17e144539715f539354ff73cf8d82c2548c3045bbcfc2ee32a2",
                SpaceId        = "bda2gc49gm0w"
            };

            var client   = new ContentfulClient(httpClient, options);
            var PageInfo = await client.GetEntry <Pages>("6pe3lPKCzpsyz9nZBpJzbr");

            List <PageContent> currentPageContents = new List <PageContent>();

            foreach (var item in PageInfo.modules)
            {
                currentPageContents.Add(await client.GetEntry <PageContent>(item["sys"]["id"].ToString()));
            }

            return(View(currentPageContents));
        }
        public async Task Contentful()
        {
            var httpClient = new HttpClient();
            var options    = new ContentfulOptions()
            {
                DeliveryApiKey = "c66a798c5adb7e11727de7831e48f2cf5039b2c2f2595be6f83b67c03fac3e4a",
                PreviewApiKey  = "ae5d997b2a95d49e250939bc350397778bb17ebe56560e8e2442b320668b2550",
                SpaceId        = "fgj5iptu3uwg"
            };
            var client = new ContentfulClient(httpClient, options);

            var entry = await client.GetEntry <Product>("3r6V9g1XzGaKAY4syUuyIg");

            Assert.That(entry.Sys.Id, Is.Not.Null);
        }
Exemplo n.º 4
0
        public async Task GetEntryShouldSerializeResponseToArbitraryModelCorrectly()
        {
            //Arrange
            _handler.Response = GetResponseFromFile(@"SampleEntry.json");

            //Act
            var res = await _client.GetEntry <TestEntryModel>("12");

            //Assert
            Assert.Equal("SoSo Wall Clock", res.ProductName);
            Assert.Equal("soso-wall-clock", res.Slug);
        }
Exemplo n.º 5
0
        public async Task RateLimitWithRetryShouldStopCallingOnSuccess()
        {
            //Arrange
            _handler = new FakeMessageHandler();
            var httpClient = new HttpClient(_handler);

            _client = new ContentfulClient(httpClient, new ContentfulOptions()
            {
                DeliveryApiKey              = "123",
                ManagementApiKey            = "123",
                SpaceId                     = "666",
                UsePreviewApi               = false,
                MaxNumberOfRateLimitRetries = 3
            });

            var response = GetResponseFromFile(@"ErrorRateLimit.json");

            response.StatusCode = (HttpStatusCode)429;
            response.Headers.Add("X-Contentful-RateLimit-Reset", "1");

            _handler.Response = response;
            var numberOfTimesCalled = 0;

            _handler.VerificationBeforeSend = () => { numberOfTimesCalled++; };
            _handler.VerifyRequest          = (HttpRequestMessage msg) => { response.RequestMessage = msg; };

            _handler.Responses.Enqueue(response);
            _handler.Responses.Enqueue(GetResponseFromFile(@"SampleEntry.json"));
            //Act
            var res = await _client.GetEntry <TestEntryModel>("12");

            //Assert
            //1 request + 1 retries
            Assert.Equal(2, numberOfTimesCalled);
            Assert.Equal("SoSo Wall Clock", res.ProductName);
        }
Exemplo n.º 6
0
        public async Task RateLimitWithRetryShouldCallSeveralTimes()
        {
            //Arrange
            _handler = new FakeMessageHandler();
            var httpClient = new HttpClient(_handler);

            _client = new ContentfulClient(httpClient, new ContentfulOptions()
            {
                DeliveryApiKey              = "123",
                ManagementApiKey            = "123",
                SpaceId                     = "666",
                UsePreviewApi               = false,
                MaxNumberOfRateLimitRetries = 3
            });

            var response = GetResponseFromFile(@"ErrorRateLimit.json");

            response.StatusCode = (HttpStatusCode)429;
            response.Headers.Add("X-Contentful-RateLimit-Reset", "1");

            _handler.Response = response;
            var numberOfTimesCalled = 0;

            _handler.VerificationBeforeSend = () => { numberOfTimesCalled++; };
            _handler.VerifyRequest          = (HttpRequestMessage msg) => { response.RequestMessage = msg; };
            //Act
            var ex = await Assert.ThrowsAsync <ContentfulRateLimitException>(async() => await _client.GetEntry <TestEntryModel>("12"));

            //Assert
            Assert.Equal(1, ex.SecondsUntilNextRequest);
            //1 request + 3 retries
            Assert.Equal(4, numberOfTimesCalled);
        }