Skip to content

Commit

Permalink
Contentful client tweaks (#351)
Browse files Browse the repository at this point in the history
* Avoid waiting for complete response, if we are just checking if for success status code. Avoid evaluating OS & Version for every request. Avoid a possible MemoryStream leak. Other minor tweaks

* Dispose responses & requests, pass cancellationToken & avoid an array allocation

* Dispose cloned request, add sealed & readonly

* Avoid deserializing into a string by deserializing directly from the response stream. Then deserialization can start immediately instead of waiting for the entire response. Reuse JsonSerializerSettings. Avoid async Task methods where it adds no value.

* Use HTTP/2

* Allow HTTP response connection

* Update package and dependency

---------

Co-authored-by: Henrik Gedionsen <[email protected]>
  • Loading branch information
Roblinde and Henr1k80 authored Oct 2, 2024
1 parent b3ec71c commit 60f9e57
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 632 deletions.
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.1.0</VersionPrefix>
<VersionPrefix>8.2.0</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.1.0</Version>
<AssemblyVersion>8.1.0.0</AssemblyVersion>
<Version>8.2.0</Version>
<AssemblyVersion>8.2.0.0</AssemblyVersion>
<RepositoryUrl>https://github.com/contentful/contentful.net</RepositoryUrl>
<FileVersion>8.1.0.0</FileVersion>
<FileVersion>8.2.0.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.1.0" />
<PackageReference Include="contentful.csharp" Version="8.2.0" />
<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
16 changes: 11 additions & 5 deletions Contentful.AspNetCore/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Contentful.Core.Configuration;
using Microsoft.Extensions.Configuration;
using System.Net.Http;
using Contentful.Core;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Http;
using Contentful.Core.Models;

namespace Contentful.AspNetCore
Expand All @@ -31,7 +28,16 @@ public static IServiceCollection AddContentful(this IServiceCollection services,
services.AddOptions();
services.Configure<ContentfulOptions>(configuration.GetSection("ContentfulOptions"));
services.TryAddSingleton<HttpClient>();
services.AddHttpClient(HttpClientName);
services.AddHttpClient(HttpClientName).ConfigurePrimaryHttpMessageHandler(sp =>
{
var options = sp.GetService<IOptions<ContentfulOptions>>().Value;
var handler = new HttpClientHandler();
if (options.AllowHttpResponseCompression && handler.SupportsAutomaticDecompression)
{
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
}
return handler;
});
services.TryAddTransient<IContentfulClient>((sp) => {
var options = sp.GetService<IOptions<ContentfulOptions>>().Value;
var factory = sp.GetService<IHttpClientFactory>();
Expand Down
6 changes: 6 additions & 0 deletions Contentful.Core/Configuration/ContentfulOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,11 @@ public class ContentfulOptions
/// Default is "https://cdn.contentful.com/spaces/".
/// </summary>
public string BaseUrl { get; set; } = "https://cdn.contentful.com/spaces/";

/// <summary>
/// Whether to use HTTP compression for responses. Only disable if you are hosting in the same datacenter as contentful
/// Default is true
/// </summary>
public bool AllowHttpResponseCompression { get; set; } = true;
}
}
23 changes: 12 additions & 11 deletions Contentful.Core/Configuration/ExtensionJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Contentful.Core.Configuration
{
Expand All @@ -15,6 +13,17 @@ namespace Contentful.Core.Configuration
/// </summary>
public class ExtensionJsonConverter : JsonConverter
{
private static readonly JsonSerializerSettings JsonSerializerSettings = new()
{
ContractResolver = new CamelCasePropertyNamesContractResolver
{
NamingStrategy =
{
OverrideSpecifiedNames = false
}
},
};

/// <summary>
/// Determines whether this instance can convert the specified object type.
/// </summary>
Expand Down Expand Up @@ -75,15 +84,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
extension
};

var resolver = new CamelCasePropertyNamesContractResolver();
resolver.NamingStrategy.OverrideSpecifiedNames = false;

var settings = new JsonSerializerSettings
{
ContractResolver = resolver,
};

var jObject = JObject.FromObject(extensionStructure, JsonSerializer.Create(settings));
var jObject = JObject.FromObject(extensionStructure, JsonSerializer.Create(JsonSerializerSettings));

serializer.Serialize(writer, jObject);
}
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.1.0</VersionPrefix>
<VersionPrefix>8.2.0</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand Down
Loading

0 comments on commit 60f9e57

Please sign in to comment.