diff --git a/Contentful.AspNetCore/Contentful.AspNetCore.csproj b/Contentful.AspNetCore/Contentful.AspNetCore.csproj index 2dfa609..6caf74f 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 - 3.0.0 + 3.1.0 netstandard2.0 Contentful Contentful GmbH. @@ -14,14 +14,14 @@ https://github.com/contentful/contentful.net/master/LICENSE git git://github.com/contentful/contentful.net - 3.0.0 - 3.0.0.0 + 3.1.0 + 3.1.0.0 bin\Release\netstandard1.5\Contentful.AspNetCore.xml - + diff --git a/Contentful.Core.Tests/ContentfulManagementClientTests.cs b/Contentful.Core.Tests/ContentfulManagementClientTests.cs index 74bbca7..77e030a 100644 --- a/Contentful.Core.Tests/ContentfulManagementClientTests.cs +++ b/Contentful.Core.Tests/ContentfulManagementClientTests.cs @@ -2582,6 +2582,50 @@ public async Task CreateApiKeyShouldCallCorrectUrlWithData() Assert.Equal(@"{""name"":""Key sharp!"",""description"":""This is the desc""}", contentSet); } + [Fact] + public async Task UpdateApiKeyShouldCallCorrectUrlWithData() + { + //Arrange + _handler.Response = GetResponseFromFile(@"ApiKeysCollection.json"); + var contentSet = ""; + var url = ""; + var method = HttpMethod.Trace; + var headerSet = false; + _handler.VerificationBeforeSend = () => + { + headerSet = _httpClient.DefaultRequestHeaders.GetValues("X-Contentful-Version").First() == "5"; + }; + _handler.VerifyRequest = async (HttpRequestMessage request) => + { + method = request.Method; + url = request.RequestUri.ToString(); + contentSet = await (request.Content as StringContent).ReadAsStringAsync(); + }; + + //Act + var res = await _client.UpdateApiKey("some-id", "Key sharp2", "This is the desc again", 5); + + //Assert + Assert.True(headerSet); + Assert.Equal(HttpMethod.Put, method); + Assert.Equal("https://api.contentful.com/spaces/666/api_keys/some-id", url); + Assert.Equal(@"{""name"":""Key sharp2"",""description"":""This is the desc again""}", contentSet); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + public async Task UpdateApiKeyShouldThrowIfNoIdSet(string id) + { + //Arrange + + //Act + var ex = await Assert.ThrowsAsync(async () => await _client.UpdateApiKey(id, "Key sharp2", "This is the desc again", 5)); + + //Assert + Assert.Equal($"The id of the api key must be set.{Environment.NewLine}Parameter name: id", ex.Message); + } + [Fact] public async Task UploadingFileShouldYieldCorrectResult() { diff --git a/Contentful.Core/Contentful.Core.csproj b/Contentful.Core/Contentful.Core.csproj index 0d0e174..6c815d2 100644 --- a/Contentful.Core/Contentful.Core.csproj +++ b/Contentful.Core/Contentful.Core.csproj @@ -4,7 +4,7 @@ contentful.csharp contentful.net en-US - 3.0.0 + 3.1.0 netstandard2.0 Contentful Contentful GmbH. @@ -18,9 +18,9 @@ false false false - 3.0.0.0 - 3.0.0.0 - 3.0.0 + 3.1.0.0 + 3.1.0.0 + 3.1.0 diff --git a/Contentful.Core/ContentfulManagementClient.cs b/Contentful.Core/ContentfulManagementClient.cs index c1861d9..f2853c8 100644 --- a/Contentful.Core/ContentfulManagementClient.cs +++ b/Contentful.Core/ContentfulManagementClient.cs @@ -1979,6 +1979,42 @@ public async Task CreateApiKey(string name, string description, string s return jsonObject.ToObject(Serializer); } + /// + /// Updates an in a space. + /// + /// The id of the API key to update. + /// The name of the API key to update. + /// The description of the API key to update. + /// The last known version of the api key. + /// The id of the space. Will default to the one set when creating the client. + /// The optional cancellation token to cancel the operation. + /// The updated . + /// There was an error when communicating with the Contentful API. + public async Task UpdateApiKey(string id, string name, string description, int version, string spaceId = null, CancellationToken cancellationToken = default) + { + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentException("The name of the api key must be set.", nameof(name)); + } + + if (string.IsNullOrEmpty(id)) + { + throw new ArgumentException("The id of the api key must be set.", nameof(id)); + } + + AddVersionHeader(version); + + var res = await PutAsync($"{_baseUrl}{spaceId ?? _options.SpaceId}/api_keys/{id}", ConvertObjectToJsonStringContent(new { name, description }), cancellationToken).ConfigureAwait(false); + + RemoveVersionHeader(); + + await EnsureSuccessfulResult(res).ConfigureAwait(false); + + var jsonObject = JObject.Parse(await res.Content.ReadAsStringAsync().ConfigureAwait(false)); + + return jsonObject.ToObject(Serializer); + } + /// /// Deletes an api key by the specified id. /// diff --git a/Contentful.Core/IContentfulManagementClient.cs b/Contentful.Core/IContentfulManagementClient.cs index 4cfb4c9..3096e25 100644 --- a/Contentful.Core/IContentfulManagementClient.cs +++ b/Contentful.Core/IContentfulManagementClient.cs @@ -52,6 +52,19 @@ public interface IContentfulManagementClient /// The created . Task CreateApiKey(string name, string description, string spaceId = null, CancellationToken cancellationToken = default); + /// + /// Updates an in a space. + /// + /// The id of the API key to update. + /// The name of the API key to update. + /// The description of the API key to update. + /// The last known version of the api key. + /// The id of the space. Will default to the one set when creating the client. + /// The optional cancellation token to cancel the operation. + /// The updated . + /// There was an error when communicating with the Contentful API. + Task UpdateApiKey(string id, string name, string description, int version, string spaceId = null, CancellationToken cancellationToken = default); + /// /// Creates a locale in the specified . ///