Skip to content

Commit

Permalink
Merge pull request #145 from contentful/feature/add-update-api-key-en…
Browse files Browse the repository at this point in the history
…dpoint

Feature/add update api key endpoint
  • Loading branch information
Roblinde authored Jun 28, 2018
2 parents 8afa965 + 9e3070f commit 3cdbee5
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 8 deletions.
8 changes: 4 additions & 4 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>3.0.0</VersionPrefix>
<VersionPrefix>3.1.0</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand All @@ -14,14 +14,14 @@
<PackageLicenseUrl>https://github.com/contentful/contentful.net/master/LICENSE</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>git://github.com/contentful/contentful.net</RepositoryUrl>
<Version>3.0.0</Version>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<Version>3.1.0</Version>
<AssemblyVersion>3.1.0.0</AssemblyVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\netstandard1.5\Contentful.AspNetCore.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="contentful.csharp" Version="3.0.0" />
<PackageReference Include="contentful.csharp" Version="3.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Razor" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="2.1.0" />
Expand Down
44 changes: 44 additions & 0 deletions Contentful.Core.Tests/ContentfulManagementClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentException>(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()
{
Expand Down
8 changes: 4 additions & 4 deletions 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>3.0.0</VersionPrefix>
<VersionPrefix>3.1.0</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand All @@ -18,9 +18,9 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<FileVersion>3.0.0.0</FileVersion>
<Version>3.0.0</Version>
<AssemblyVersion>3.1.0.0</AssemblyVersion>
<FileVersion>3.1.0.0</FileVersion>
<Version>3.1.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
Expand Down
36 changes: 36 additions & 0 deletions Contentful.Core/ContentfulManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,42 @@ public async Task<ApiKey> CreateApiKey(string name, string description, string s
return jsonObject.ToObject<ApiKey>(Serializer);
}

/// <summary>
/// Updates an <see cref="Contentful.Core.Models.Management.ApiKey"/> in a space.
/// </summary>
/// <param name="id">The id of the API key to update.</param>
/// <param name="name">The name of the API key to update.</param>
/// <param name="description">The description of the API key to update.</param>
/// <param name="version">The last known version of the api key.</param>
/// <param name="spaceId">The id of the space. Will default to the one set when creating the client.</param>
/// <param name="cancellationToken">The optional cancellation token to cancel the operation.</param>
/// <returns>The updated <see cref="Contentful.Core.Models.Management.ApiKey"/>.</returns>
/// <exception cref="ContentfulException">There was an error when communicating with the Contentful API.</exception>
public async Task<ApiKey> 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<ApiKey>(Serializer);
}

/// <summary>
/// Deletes an api key by the specified id.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions Contentful.Core/IContentfulManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ public interface IContentfulManagementClient
/// <returns>The created <see cref="Contentful.Core.Models.Management.ApiKey"/>.</returns>
Task<ApiKey> CreateApiKey(string name, string description, string spaceId = null, CancellationToken cancellationToken = default);

/// <summary>
/// Updates an <see cref="Contentful.Core.Models.Management.ApiKey"/> in a space.
/// </summary>
/// <param name="id">The id of the API key to update.</param>
/// <param name="name">The name of the API key to update.</param>
/// <param name="description">The description of the API key to update.</param>
/// <param name="version">The last known version of the api key.</param>
/// <param name="spaceId">The id of the space. Will default to the one set when creating the client.</param>
/// <param name="cancellationToken">The optional cancellation token to cancel the operation.</param>
/// <returns>The updated <see cref="Contentful.Core.Models.Management.ApiKey"/>.</returns>
/// <exception cref="Contentful.Core.Errors.ContentfulException">There was an error when communicating with the Contentful API.</exception>
Task<ApiKey> UpdateApiKey(string id, string name, string description, int version, string spaceId = null, CancellationToken cancellationToken = default);

/// <summary>
/// Creates a locale in the specified <see cref="Space"/>.
/// </summary>
Expand Down

0 comments on commit 3cdbee5

Please sign in to comment.