Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Contentful.AspNetCore/Contentful.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Description>Official .NET SDK for the Contentful Content Delivery and Management API for ASP.NET core.</Description>
<PackageId>contentful.aspnetcore</PackageId>
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>8.3.1</VersionPrefix>
<VersionPrefix>8.3.2</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand All @@ -13,10 +13,10 @@
<PackageProjectUrl>https://github.com/contentful/contentful.net</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<Version>8.3.1</Version>
<AssemblyVersion>8.3.1.0</AssemblyVersion>
<Version>8.3.2</Version>
<AssemblyVersion>8.3.2.0</AssemblyVersion>
<RepositoryUrl>https://github.com/contentful/contentful.net</RepositoryUrl>
<FileVersion>8.3.1.0</FileVersion>
<FileVersion>8.3.2.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\netstandard1.5\Contentful.AspNetCore.xml</DocumentationFile>
Expand All @@ -25,7 +25,7 @@
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="contentful.csharp" Version="8.3.1" />
<PackageReference Include="contentful.csharp" Version="8.3.2" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.2.0" />
Expand Down
32 changes: 32 additions & 0 deletions Contentful.Core.Tests/ContentfulClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,38 @@ public async Task CreateEmbargoedAssetKeyShouldReturnCorrectKey()
Assert.Equal("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjE6MSJ9.eyJleHAiOjE2Mzc2MjM4MDAsInN1YiI6InRlc3RzcGFjZXZhbHVlIiwiYXVkIjoiYWRuIiwianRpIjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIiwiY3RmOnVucHViIjp0cnVlfQ.BHtBHdrInzu3KtGxTJ7FLxGF0WN9HEdJ9C5CeB3hx7g", res.Policy);
}

[Fact]
public async Task RateLimitWithMultipleRetriesShouldNotThrowObjectDisposedException()
{
//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 & Assert
// This should not throw ObjectDisposedException
var ex = await Assert.ThrowsAsync<ContentfulRateLimitException>(async () => await _client.GetEntry<TestEntryModel>("12"));

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

private ContentfulClient GetClientWithEnvironment(string env = "special")
{
var httpClient = new HttpClient(_handler);
Expand Down
2 changes: 1 addition & 1 deletion Contentful.Core/Contentful.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PackageId>contentful.csharp</PackageId>
<AssemblyTitle>contentful.net</AssemblyTitle>
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>8.3.1</VersionPrefix>
<VersionPrefix>8.3.2</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand Down
8 changes: 6 additions & 2 deletions Contentful.Core/ContentfulClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ protected async Task<HttpResponseMessage> EnsureSuccessfulResult(HttpResponseMes
{
if (!response.IsSuccessStatusCode)
{
// Store the original request message to prevent disposal issues during retries
var requestMessage = response.RequestMessage;

if(response.StatusCode == System.Net.HttpStatusCode.NotModified)
{
return response;
Expand All @@ -320,14 +323,15 @@ protected async Task<HttpResponseMessage> EnsureSuccessfulResult(HttpResponseMes
{
try
{
await CreateExceptionForFailedRequest(response).ConfigureAwait(false); ;
await CreateExceptionForFailedRequest(response).ConfigureAwait(false);
}
catch (ContentfulRateLimitException ex)
{
await Task.Delay(ex.SecondsUntilNextRequest * 1000).ConfigureAwait(false);
}

using var clonedMessage = await CloneHttpRequest(response.RequestMessage);
// Clone from the original request message to avoid disposed message issues
using var clonedMessage = await CloneHttpRequest(requestMessage);

response = await _httpClient.SendAsync(clonedMessage, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);

Expand Down