示例#1
0
        public async Task ActionInvokerSerializeEnum_EnumIsSerializedAsString()
        {
            var httpClient    = new FakeManagementHttpClient();
            var actionInvoker = new ActionInvoker(httpClient, new MessageCreator("{api_key}"));

            var assetUpsertModel = new AssetUpsertModel()
            {
                Title        = "Asset",
                Descriptions = new []
                {
                    new AssetDescription()
                    {
                        Description = "Description",
                        Language    = LanguageIdentifier.DEFAULT_LANGUAGE
                    },
                },
                FileReference = new FileReference()
                {
                    Id   = "ab7bdf75-781b-4bf9-aed8-501048860402",
                    Type = FileReferenceTypeEnum.Internal
                }
            };

            await actionInvoker.InvokeMethodAsync <AssetUpsertModel, dynamic>("{endpoint_url}", HttpMethod.Put, assetUpsertModel);

            var expectedRequestBody = "{\"file_reference\":{\"id\":\"ab7bdf75-781b-4bf9-aed8-501048860402\",\"type\":\"internal\"},\"descriptions\":[{\"language\":{\"id\":\"00000000-0000-0000-0000-000000000000\"},\"description\":\"Description\"}],\"title\":\"Asset\"}";

            Assert.Equal(expectedRequestBody, httpClient.requestBody);
        }
        public static async Task <AssetModel> UpsertAssetByExternalIdAsync(this ManagementClient client, string externalId, FileContentSource fileContent, IEnumerable <AssetDescription> descriptions)
        {
            if (string.IsNullOrEmpty(externalId))
            {
                throw new ArgumentException("The external id is not specified.", nameof(externalId));
            }

            if (fileContent == null)
            {
                throw new ArgumentNullException(nameof(fileContent));
            }

            if (descriptions == null)
            {
                throw new ArgumentNullException(nameof(descriptions));
            }

            var fileResult = await client.UploadFileAsync(fileContent);

            var asset = new AssetUpsertModel
            {
                FileReference = fileResult,
                Descriptions  = descriptions
            };

            var response = await client.UpsertAssetByExternalIdAsync(externalId, asset);

            return(response);
        }
 private static void UpdateAssetTitle(AssetUpdateModel updatedAsset, AssetUpsertModel asset)
 {
     if (updatedAsset.Title != null)
     {
         asset.Title = updatedAsset.Title;
     }
 }
示例#4
0
    public async Task ActionInvokerSerializeEnum_EnumIsSerializedAsString()
    {
        var httpClient    = new FakeManagementHttpClient();
        var actionInvoker = new _ActionInvoker(httpClient, new MessageCreator("{api_key}"));

        var assetUpsertModel = new AssetUpsertModel()
        {
            Title        = "Asset",
            Descriptions = new[]
            {
                new AssetDescription()
                {
                    Description = "Description",
                    Language    = Reference.ById(Guid.Empty)
                },
            },
            FileReference = new FileReference()
            {
                Id   = "ab7bdf75-781b-4bf9-aed8-501048860402",
                Type = FileReferenceTypeEnum.Internal
            }
        };

        await actionInvoker.InvokeMethodAsync <AssetUpsertModel, dynamic>("{endpoint_url}", HttpMethod.Put, assetUpsertModel);

        Assert.Contains("\"type\":\"internal\"", httpClient._requestBody);
    }
        /// <summary>
        /// Creates asset.
        /// </summary>
        /// <param name="client"></param>
        /// <param name="fileContent">Represents the content of the file.</param>
        /// <param name="assetUpdateModel">Updated values for asset.</param>
        public static async Task <AssetModel> CreateAssetAsync(this ManagementClient client, FileContentSource fileContent, AssetUpdateModel assetUpdateModel)
        {
            if (fileContent == null)
            {
                throw new ArgumentNullException(nameof(fileContent));
            }
            if (assetUpdateModel == null)
            {
                throw new ArgumentNullException(nameof(assetUpdateModel));
            }

            if (assetUpdateModel.Descriptions == null)
            {
                throw new ArgumentNullException(nameof(assetUpdateModel.Descriptions));
            }

            var fileResult = await client.UploadFileAsync(fileContent);

            var asset = new AssetUpsertModel
            {
                FileReference = fileResult,
                Descriptions  = assetUpdateModel.Descriptions,
                Title         = assetUpdateModel.Title
            };

            var response = await client.CreateAssetAsync(asset);

            return(response);
        }
示例#6
0
    /// <inheritdoc />
    public async Task <AssetModel <T> > UpsertAssetAsync <T>(Reference identifier, AssetUpsertModel <T> asset) where T : new()
    {
        if (asset == null)
        {
            throw new ArgumentNullException(nameof(asset));
        }

        var result = await UpsertAssetAsync(identifier, _modelProvider.GetAssetUpsertModel(asset));

        return(_modelProvider.GetAssetModel <T>(result));
    }
示例#7
0
    public async Task UpsertAssetAsync_DynamicallyTyped_ByCodename_UpsertsAsset()
    {
        var client = _fileSystemFixture.CreateMockClientWithoutResponse();

        var updateModel = new AssetUpsertModel {
            Title = "xxx"
        };

        await client.Invoking(c => c.UpsertAssetAsync(Reference.ByCodename("c"), updateModel))
        .Should().ThrowExactlyAsync <InvalidOperationException>();
    }
示例#8
0
    public async Task UpsertAssetAsync_DynamicallyTyped_WithFileContent_FileContentIsNull_Throws()
    {
        var client = _fileSystemFixture.CreateMockClientWithoutResponse();

        var updateModel = new AssetUpsertModel {
            Title = "xxx"
        };

        await client.Invoking(c => c.UpsertAssetAsync(Reference.ByExternalId("externalId"), null, updateModel))
        .Should().ThrowExactlyAsync <ArgumentNullException>();
    }
        /// <summary>
        /// Creates asset.
        /// </summary>
        /// <param name="asset">Represents asset which will be created.</param>
        /// <returns>The <see cref="AssetModel"/> instance that represents created asset.</returns>
        public async Task <AssetModel> CreateAssetAsync(AssetUpsertModel asset)
        {
            if (asset == null)
            {
                throw new ArgumentNullException(nameof(asset));
            }

            var endpointUrl = _urlBuilderV2.BuildAssetsUrl();
            var response    = await _actionInvoker.InvokeMethodAsync <AssetUpsertModel, AssetModel>(endpointUrl, HttpMethod.Post, asset);

            return(response);
        }
示例#10
0
    public async Task UpsertAssetAsync_DynamicallyTyped_IdentifierIsNull_Throws()
    {
        var client = _fileSystemFixture.CreateMockClientWithoutResponse();

        var updateModel = new AssetUpsertModel
        {
            Title = "xxx"
        };

        await client.Invoking(c => c.UpsertAssetAsync(null, updateModel))
        .Should().ThrowExactlyAsync <ArgumentNullException>();
    }
示例#11
0
    public async Task UpsertAssetAsync_DynamicallyTyped_WithFileContent_IdentifierIsNull_Throws()
    {
        var client = _fileSystemFixture.CreateMockClientWithoutResponse();

        var content = new FileContentSource(
            new MemoryStream(Encoding.UTF8.GetBytes("Hello world from CM API .NET SDK")),
            "Hello.txt",
            "text/plain");

        var updateModel = new AssetUpsertModel {
            Title = "xxx"
        };

        await client.Invoking(c => c.UpsertAssetAsync(null, content, updateModel))
        .Should().ThrowExactlyAsync <ArgumentNullException>();
    }
示例#12
0
    public async Task UpsertAssetAsync_DynamicallyTyped_ByExternalId_UpsertsAsset()
    {
        var client = _fileSystemFixture.CreateMockClientWithResponse("Asset.json");

        var expected = GetExpectedDynamicAssetModel();

        var updateModel = new AssetUpsertModel
        {
            Title    = expected.Title,
            Elements = expected.Elements
        };

        var response = await client.UpsertAssetAsync(Reference.ByExternalId(expected.ExternalId), updateModel);

        response.Should().BeEquivalentTo(expected);
    }
示例#13
0
    /// <inheritdoc />
    public async Task <AssetModel> UpsertAssetAsync(Reference identifier, AssetUpsertModel asset)
    {
        if (identifier == null)
        {
            throw new ArgumentNullException(nameof(identifier));
        }

        if (asset == null)
        {
            throw new ArgumentNullException(nameof(asset));
        }

        var endpointUrl = _urlBuilder.BuildAssetsUrl(identifier);
        var response    = await _actionInvoker.InvokeMethodAsync <AssetUpsertModel, AssetModel>(endpointUrl, HttpMethod.Put, asset);

        return(response);
    }
示例#14
0
    public async Task UpsertAssetAsync_DynamicallyTyped_WithFileContent_UpsertsAsset()
    {
        var client = _fileSystemFixture.CreateMockClientWithResponse("File.json", "Asset.json");

        var expected = GetExpectedDynamicAssetModel();

        var stream      = new MemoryStream(Encoding.UTF8.GetBytes("Hello world from CM API .NET SDK"));
        var fileName    = "Hello.txt";
        var contentType = "text/plain";

        var updateModel = new AssetUpsertModel
        {
            Title    = expected.Title,
            Elements = expected.Elements
        };

        var content = new FileContentSource(stream, fileName, contentType);

        var response = await client.UpsertAssetAsync(Reference.ById(expected.Id), content, updateModel);

        response.Should().BeEquivalentTo(expected);
    }
示例#15
0
        public static async Task <AssetModel> CreateAssetAsync(this ContentManagementClient client, FileContentSource fileContent, IEnumerable <AssetDescription> descriptions)
        {
            if (fileContent == null)
            {
                throw new ArgumentNullException(nameof(fileContent));
            }

            if (descriptions == null)
            {
                throw new ArgumentNullException(nameof(descriptions));
            }

            var fileResult = await client.UploadFileAsync(fileContent);

            var asset = new AssetUpsertModel
            {
                FileReference = fileResult,
                Descriptions  = descriptions
            };

            var response = await client.CreateAssetAsync(asset);

            return(response);
        }
        /// <summary>
        /// Inserts or updates asset according to external identifier.
        /// </summary>
        /// <param name="externalId">The external identifier of the content item.</param>
        /// <param name="asset">Represents asset which will be created.</param>
        /// <returns>The <see cref="AssetModel"/> instance that represents inserted or updated asset.</returns>
        public async Task <AssetModel> UpsertAssetByExternalIdAsync(string externalId, AssetUpsertModel asset)
        {
            if (string.IsNullOrEmpty(externalId))
            {
                throw new ArgumentException("The external id is not specified.", nameof(externalId));
            }

            if (asset == null)
            {
                throw new ArgumentNullException(nameof(asset));
            }

            var endpointUrl = _urlBuilderV2.BuildAssetsUrlFromExternalId(externalId);
            var response    = await _actionInvoker.InvokeMethodAsync <AssetUpsertModel, AssetModel>(
                endpointUrl,
                HttpMethod.Put,
                asset
                );

            return(response);
        }
示例#17
0
    /// <summary>
    /// Creates or updates the given asset.
    /// </summary>
    /// <param name="client">Content management client instance.</param>
    /// <param name="identifier">The identifier of the asset.</param>
    /// <param name="fileContent">Represents the content of the file.</param>
    /// <param name="upsertModel">Updated values for the asset.</param>
    /// <returns>The <see cref="AssetModel{T}"/> instance that represents created or updated strongly typed asset.</returns>
    public async static Task <AssetModel <T> > UpsertAssetAsync <T>(this IManagementClient client, Reference identifier, FileContentSource fileContent, AssetUpsertModel <T> upsertModel) where T : new()
    {
        if (identifier == null)
        {
            throw new ArgumentNullException(nameof(identifier));
        }

        if (fileContent == null)
        {
            throw new ArgumentNullException(nameof(fileContent));
        }

        if (upsertModel == null)
        {
            throw new ArgumentNullException(nameof(upsertModel));
        }

        var fileResult = await client.UploadFileAsync(fileContent);

        upsertModel.FileReference = fileResult;

        var response = await client.UpsertAssetAsync(identifier, upsertModel);

        return(response);
    }