Skip to content

Commit

Permalink
Merge pull request #116 from contentful/feature/add-post-create-asset
Browse files Browse the repository at this point in the history
Feature/add post create asset
  • Loading branch information
Roblinde authored Oct 4, 2017
2 parents 11ecaf2 + a4558b9 commit fab30e0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
6 changes: 3 additions & 3 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>1.3.1</VersionPrefix>
<VersionPrefix>1.4.0</VersionPrefix>
<TargetFramework>netstandard1.6</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand All @@ -14,13 +14,13 @@
<PackageLicenseUrl>https://github.com/contentful/contentful.net/master/LICENSE</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>git://github.com/contentful/contentful.net</RepositoryUrl>
<Version>1.3.1</Version>
<Version>1.4.0</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\netstandard1.5\Contentful.AspNetCore.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="contentful.csharp" Version="1.3.1" />
<PackageReference Include="contentful.csharp" Version="1.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Razor" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="1.1.2" />
Expand Down
34 changes: 33 additions & 1 deletion Contentful.Core.Tests/ContentfulManagementClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ public async Task CreateAssetShouldThrowIfIdNotSet()
}

[Fact]
public async Task CreateAssetShouldCallCorrectUrlWithCorrectData()
public async Task CreateOrUpdateAssetShouldCallCorrectUrlWithCorrectData()
{
//Arrange
var asset = new ManagementAsset()
Expand Down Expand Up @@ -1189,6 +1189,38 @@ public async Task CreateAssetShouldCallCorrectUrlWithCorrectData()
Assert.Equal("https://api.contentful.com/spaces/666/assets/424", requestUrl);
}

[Fact]
public async Task CreateAssetShouldCallCorrectUrlWithCorrectData()
{
//Arrange
var asset = new ManagementAsset()
{
Title = new Dictionary<string, string>()
};
asset.Title["en-US"] = "Burton Green";
asset.SystemProperties = new SystemProperties()
{
Id = "424"
};
_handler.Response = GetResponseFromFile(@"SampleAssetManagement.json");
var requestUrl = "";
var contentSet = "";
var requestMethod = HttpMethod.Trace;
_handler.VerifyRequest = async (HttpRequestMessage request) =>
{
contentSet = await (request.Content as StringContent).ReadAsStringAsync();
requestMethod = request.Method;
requestUrl = request.RequestUri.ToString();
};

//Act
var res = await _client.CreateAssetAsync(asset);
//Assert
Assert.Equal(HttpMethod.Post, requestMethod);
Assert.Contains(@"""en-US"":""Burton Green""", contentSet);
Assert.Equal("https://api.contentful.com/spaces/666/assets", requestUrl);
}

[Fact]
public async Task CreateAssetShouldSetVersionHeaderCorrectly()
{
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>1.3.1</VersionPrefix>
<VersionPrefix>1.4.0</VersionPrefix>
<TargetFramework>netstandard1.4</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand All @@ -18,9 +18,9 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<AssemblyVersion>1.3.1.0</AssemblyVersion>
<FileVersion>1.3.1.0</FileVersion>
<Version>1.3.1</Version>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.0</FileVersion>
<Version>1.4.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options">
Expand Down
21 changes: 21 additions & 0 deletions Contentful.Core/ContentfulManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,27 @@ public ContentfulManagementClient(HttpClient httpClient, string managementApiKey
return updatedAsset;
}

/// <summary>
/// Creates an <see cref="Contentful.Core.Models.Management.ManagementAsset"/> with a randomly created id.
/// </summary>
/// <param name="asset">The asset to create.</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 created <see cref="Contentful.Core.Models.Management.ManagementAsset"/></returns>
/// <exception cref="ContentfulException">There was an error when communicating with the Contentful API.</exception>
public async Task<ManagementAsset> CreateAssetAsync(ManagementAsset asset, string spaceId = null, CancellationToken cancellationToken = default(CancellationToken))
{
var res = await PostAsync($"{_baseUrl}{spaceId ?? _options.SpaceId}/assets",
ConvertObjectToJsonStringContent(new { fields = new { title = asset.Title, description = asset.Description, file = asset.Files } }), cancellationToken).ConfigureAwait(false);

await EnsureSuccessfulResultAsync(res).ConfigureAwait(false);

var jsonObject = JObject.Parse(await res.Content.ReadAsStringAsync().ConfigureAwait(false));
var createdAsset = jsonObject.ToObject<ManagementAsset>(Serializer);

return createdAsset;
}

/// <summary>
/// Gets all locales in a <see cref="Space"/>.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions Contentful.Core/IContentfulManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public interface IContentfulManagementClient
/// <returns>The updated <see cref="Contentful.Core.Models.Management.ManagementAsset"/></returns>
Task<ManagementAsset> CreateOrUpdateAssetAsync(ManagementAsset asset, string spaceId = null, int? version = default(int?), CancellationToken cancellationToken = default(CancellationToken));


/// <summary>
/// Creates an <see cref="Contentful.Core.Models.Management.ManagementAsset"/> with a randomly created id.
/// </summary>
/// <param name="asset">The asset to create.</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 created <see cref="Contentful.Core.Models.Management.ManagementAsset"/></returns>
Task<ManagementAsset> CreateAssetAsync(ManagementAsset asset, string spaceId = null, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Creates or updates a ContentType. Updates if a content type with the same id already exists.
/// </summary>
Expand Down

0 comments on commit fab30e0

Please sign in to comment.