diff --git a/Contentful.AspNetCore/Contentful.AspNetCore.csproj b/Contentful.AspNetCore/Contentful.AspNetCore.csproj index 5410347..21dee74 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 - 1.3.1 + 1.4.0 netstandard1.6 Contentful Contentful GmbH. @@ -14,13 +14,13 @@ https://github.com/contentful/contentful.net/master/LICENSE git git://github.com/contentful/contentful.net - 1.3.1 + 1.4.0 bin\Release\netstandard1.5\Contentful.AspNetCore.xml - + diff --git a/Contentful.Core.Tests/ContentfulManagementClientTests.cs b/Contentful.Core.Tests/ContentfulManagementClientTests.cs index 1648bbb..1eb6d92 100644 --- a/Contentful.Core.Tests/ContentfulManagementClientTests.cs +++ b/Contentful.Core.Tests/ContentfulManagementClientTests.cs @@ -1158,7 +1158,7 @@ public async Task CreateAssetShouldThrowIfIdNotSet() } [Fact] - public async Task CreateAssetShouldCallCorrectUrlWithCorrectData() + public async Task CreateOrUpdateAssetShouldCallCorrectUrlWithCorrectData() { //Arrange var asset = new ManagementAsset() @@ -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() + }; + 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() { diff --git a/Contentful.Core/Contentful.Core.csproj b/Contentful.Core/Contentful.Core.csproj index 8dcccda..afb5527 100644 --- a/Contentful.Core/Contentful.Core.csproj +++ b/Contentful.Core/Contentful.Core.csproj @@ -4,7 +4,7 @@ contentful.csharp contentful.net en-US - 1.3.1 + 1.4.0 netstandard1.4 Contentful Contentful GmbH. @@ -18,9 +18,9 @@ false false false - 1.3.1.0 - 1.3.1.0 - 1.3.1 + 1.4.0.0 + 1.4.0.0 + 1.4.0 diff --git a/Contentful.Core/ContentfulManagementClient.cs b/Contentful.Core/ContentfulManagementClient.cs index abd6ba8..63b5395 100644 --- a/Contentful.Core/ContentfulManagementClient.cs +++ b/Contentful.Core/ContentfulManagementClient.cs @@ -1096,6 +1096,27 @@ public ContentfulManagementClient(HttpClient httpClient, string managementApiKey return updatedAsset; } + /// + /// Creates an with a randomly created id. + /// + /// The asset to create. + /// The id of the space. Will default to the one set when creating the client. + /// The optional cancellation token to cancel the operation. + /// The created + /// There was an error when communicating with the Contentful API. + public async Task 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(Serializer); + + return createdAsset; + } + /// /// Gets all locales in a . /// diff --git a/Contentful.Core/IContentfulManagementClient.cs b/Contentful.Core/IContentfulManagementClient.cs index d9ac6a4..9231d9d 100644 --- a/Contentful.Core/IContentfulManagementClient.cs +++ b/Contentful.Core/IContentfulManagementClient.cs @@ -71,6 +71,16 @@ public interface IContentfulManagementClient /// The updated Task CreateOrUpdateAssetAsync(ManagementAsset asset, string spaceId = null, int? version = default(int?), CancellationToken cancellationToken = default(CancellationToken)); + + /// + /// Creates an with a randomly created id. + /// + /// The asset to create. + /// The id of the space. Will default to the one set when creating the client. + /// The optional cancellation token to cancel the operation. + /// The created + Task CreateAssetAsync(ManagementAsset asset, string spaceId = null, CancellationToken cancellationToken = default(CancellationToken)); + /// /// Creates or updates a ContentType. Updates if a content type with the same id already exists. ///