From 77b0a35d202e37a038c17b766ff282586d8de5dd Mon Sep 17 00:00:00 2001 From: Robert Linde Date: Wed, 9 Oct 2024 22:26:26 +0200 Subject: [PATCH] Add option for management client base url (#354) --- .../Contentful.AspNetCore.csproj | 10 +++--- .../ContentfulManagementClientTests.cs | 31 +++++++++++++++++++ .../Configuration/ContentfulOptions.cs | 8 ++++- Contentful.Core/Contentful.Core.csproj | 2 +- Contentful.Core/ContentfulManagementClient.cs | 2 ++ 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/Contentful.AspNetCore/Contentful.AspNetCore.csproj b/Contentful.AspNetCore/Contentful.AspNetCore.csproj index 4931331..e90efc1 100644 --- a/Contentful.AspNetCore/Contentful.AspNetCore.csproj +++ b/Contentful.AspNetCore/Contentful.AspNetCore.csproj @@ -3,7 +3,7 @@ Official .NET SDK for the Contentful Content Delivery and Management API for ASP.NET core. contentful.aspnetcore en-US - 8.2.0 + 8.2.1 netstandard2.0 Contentful Contentful GmbH. @@ -13,10 +13,10 @@ https://github.com/contentful/contentful.net MIT git - 8.2.0 - 8.2.0.0 + 8.2.1 + 8.2.1.0 https://github.com/contentful/contentful.net - 8.2.0.0 + 8.2.1.0 bin\Release\netstandard1.5\Contentful.AspNetCore.xml @@ -25,7 +25,7 @@ $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb - + diff --git a/Contentful.Core.Tests/ContentfulManagementClientTests.cs b/Contentful.Core.Tests/ContentfulManagementClientTests.cs index b5ff1e1..17931d9 100644 --- a/Contentful.Core.Tests/ContentfulManagementClientTests.cs +++ b/Contentful.Core.Tests/ContentfulManagementClientTests.cs @@ -284,6 +284,37 @@ public async Task DeletingASpaceShouldCallCorrectUrl(string id) Assert.Equal($"https://api.contentful.com/spaces/{id}", requestUrl); } + [Fact] + public async Task UpdatingBaseUrlShouldCallCorrectUrl() + { + //Arrange + + var client = new ContentfulManagementClient(_httpClient, new ContentfulOptions() + { + DeliveryApiKey = "123", + ManagementApiKey = "564", + SpaceId = "666", + UsePreviewApi = false, + ManagementBaseUrl = "https://apa.contentful.com/spaces/" + }); + _handler.Response = new HttpResponseMessage(); + var requestUrl = ""; + var requestMethod = HttpMethod.Trace; + _handler.VerifyRequest = (HttpRequestMessage request) => + { + requestMethod = request.Method; + requestUrl = request.RequestUri.ToString(); + }; + + + //Act + await client.DeleteSpace("123"); + + //Assert + Assert.Equal(HttpMethod.Delete, requestMethod); + Assert.Equal($"https://apa.contentful.com/spaces/123", requestUrl); + } + [Fact] public async Task GetContentTypesShouldDeserializeCorrectly() { diff --git a/Contentful.Core/Configuration/ContentfulOptions.cs b/Contentful.Core/Configuration/ContentfulOptions.cs index b893e1c..5c60dc8 100644 --- a/Contentful.Core/Configuration/ContentfulOptions.cs +++ b/Contentful.Core/Configuration/ContentfulOptions.cs @@ -52,11 +52,17 @@ public class ContentfulOptions public string Environment { get; set; } /// - /// Gets or sets the base URL for the Contentful API. + /// Gets or sets the base URL for the Contentful devliery API. /// Default is "https://cdn.contentful.com/spaces/". /// public string BaseUrl { get; set; } = "https://cdn.contentful.com/spaces/"; + /// + /// Gets or sets the base URL for the Contentful management API. + /// Default is "https://api.contentful.com/spaces/". + /// + public string ManagementBaseUrl { get; set; } = "https://api.contentful.com/spaces/"; + /// /// Whether to use HTTP compression for responses. Only disable if you are hosting in the same datacenter as contentful /// Default is true diff --git a/Contentful.Core/Contentful.Core.csproj b/Contentful.Core/Contentful.Core.csproj index ab9c511..cc4c166 100644 --- a/Contentful.Core/Contentful.Core.csproj +++ b/Contentful.Core/Contentful.Core.csproj @@ -4,7 +4,7 @@ contentful.csharp contentful.net en-US - 8.2.0 + 8.2.1 netstandard2.0 Contentful Contentful GmbH. diff --git a/Contentful.Core/ContentfulManagementClient.cs b/Contentful.Core/ContentfulManagementClient.cs index 0cf228c..18a2b1f 100644 --- a/Contentful.Core/ContentfulManagementClient.cs +++ b/Contentful.Core/ContentfulManagementClient.cs @@ -42,6 +42,8 @@ public ContentfulManagementClient(HttpClient httpClient, ContentfulOptions optio throw new ArgumentException("The ContentfulOptions cannot be null.", nameof(options)); } + _baseUrl = options.ManagementBaseUrl ?? _baseUrl; + SerializerSettings.Converters.Add(new ExtensionJsonConverter()); SerializerSettings.Converters.Add(new ContentJsonConverter()); }