Skip to content

Commit

Permalink
refactor null checks, convert lambdas to method groups
Browse files Browse the repository at this point in the history
  • Loading branch information
pokornyd committed Aug 12, 2024
1 parent cf54056 commit 742f458
Show file tree
Hide file tree
Showing 26 changed files with 122 additions and 428 deletions.
2 changes: 1 addition & 1 deletion Kontent.Ai.Management.Tests/Data/ElementsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private static dynamic GetRichTextAsDynamic(Guid? elementId, string value, IEnum
dynamic element = new ExpandoObject();
element.element = GetElement(elementId.Value.ToString("d"));
element.value = value;
element.components = compoments.Select(x => GetRichTextComponentAsDynamic(x));
element.components = compoments.Select(GetRichTextComponentAsDynamic);

return element;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task ListAssetsAsync_DynamicallyTyped_WithMorePages_ListsAssets()
"00000000-0000-0000-0000-000000000000",
"10000000-0000-0000-0000-000000000000",
"20000000-0000-0000-0000-000000000000"
}.Select(x => GetExpectedDynamicAssetModel(assetId: x));
}.Select(GetExpectedDynamicAssetModel);

var response = await client.ListAssetsAsync().GetAllAsync();

Expand All @@ -49,7 +49,7 @@ public async Task ListAssetsAsync_StronglyTyped_WithMorePages_ListsAssets()
"00000000-0000-0000-0000-000000000000",
"10000000-0000-0000-0000-000000000000",
"20000000-0000-0000-0000-000000000000"
}.Select(x => GetExpectedStronglyTypedAssetModel(assetId: x));
}.Select(GetExpectedStronglyTypedAssetModel);

var response = await client.ListAssetsAsync<ComplexTestModel>().GetAllAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void GetKontentElementCodename_ThrowsIfNoAttribute()
{
var property = typeof(PropertyInfoExtensionsTestsSampleClass).GetProperty("Property1");

Assert.Throws<InvalidOperationException>(() => property.GetKontentElementCodename());
Assert.Throws<InvalidOperationException>(property.GetKontentElementCodename);
}

[Fact]
Expand Down
60 changes: 12 additions & 48 deletions Kontent.Ai.Management/Extensions/ManagementClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ public static class ManagementClientExtensions
/// <returns>The <see cref="ContentItemModel"/> instance that represents updated content item.</returns>
public async static Task<ContentItemModel> UpsertContentItemAsync(this IManagementClient client, Reference identifier, ContentItemModel contentItem)
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}
ArgumentNullException.ThrowIfNull(identifier);

if (contentItem == null)
{
throw new ArgumentNullException(nameof(contentItem));
}
ArgumentNullException.ThrowIfNull(contentItem);

var contentItemUpdateModel = new ContentItemUpsertModel
{
Expand All @@ -52,14 +46,8 @@ public async static Task<ContentItemModel> UpsertContentItemAsync(this IManageme
/// <param name="assetCreateModel">Updated values for the asset.</param>
public async static Task<AssetModel> CreateAssetAsync(this IManagementClient client, FileContentSource fileContent, AssetCreateModel assetCreateModel)
{
if (fileContent == null)
{
throw new ArgumentNullException(nameof(fileContent));
}
if (assetCreateModel == null)
{
throw new ArgumentNullException(nameof(assetCreateModel));
}
ArgumentNullException.ThrowIfNull(fileContent);
ArgumentNullException.ThrowIfNull(assetCreateModel);

var fileResult = await client.UploadFileAsync(fileContent);

Expand All @@ -78,14 +66,8 @@ public async static Task<AssetModel> CreateAssetAsync(this IManagementClient cli
/// <param name="assetCreateModel">Updated values for the strongly typed asset.</param>
public async static Task<AssetModel<T>> CreateAssetAsync<T>(this IManagementClient client, FileContentSource fileContent, AssetCreateModel<T> assetCreateModel) where T : new()
{
if (fileContent == null)
{
throw new ArgumentNullException(nameof(fileContent));
}
if (assetCreateModel == null)
{
throw new ArgumentNullException(nameof(assetCreateModel));
}
ArgumentNullException.ThrowIfNull(fileContent);
ArgumentNullException.ThrowIfNull(assetCreateModel);

var fileResult = await client.UploadFileAsync(fileContent);

Expand All @@ -106,20 +88,11 @@ public async static Task<AssetModel> CreateAssetAsync(this IManagementClient cli
/// <returns>The <see cref="AssetModel"/> instance that represents created or updated asset.</returns>
public async static Task<AssetModel> UpsertAssetAsync(this IManagementClient client, Reference identifier, FileContentSource fileContent, AssetUpsertModel upsertModel)
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}
ArgumentNullException.ThrowIfNull(identifier);

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

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

var fileResult = await client.UploadFileAsync(fileContent);

Expand All @@ -140,20 +113,11 @@ public async static Task<AssetModel> UpsertAssetAsync(this IManagementClient cli
/// <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));
}
ArgumentNullException.ThrowIfNull(identifier);

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

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

var fileResult = await client.UploadFileAsync(fileContent);

Expand Down
40 changes: 8 additions & 32 deletions Kontent.Ai.Management/ManagementClient.Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ public async Task<IListingResponseModel<AssetModel>> ListAssetsAsync()
/// <inheritdoc />
public async Task<AssetModel> GetAssetAsync(Reference identifier)
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}
ArgumentNullException.ThrowIfNull(identifier);

var endpointUrl = _urlBuilder.BuildAssetsUrl(identifier);
var response = await _actionInvoker.InvokeReadOnlyMethodAsync<AssetModel>(endpointUrl, HttpMethod.Get);
Expand All @@ -61,15 +58,9 @@ public async Task<AssetModel> GetAssetAsync(Reference identifier)
/// <inheritdoc />
public async Task<AssetModel> UpsertAssetAsync(Reference identifier, AssetUpsertModel asset)
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}
ArgumentNullException.ThrowIfNull(identifier);

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

var endpointUrl = _urlBuilder.BuildAssetsUrl(identifier);
var response = await _actionInvoker.InvokeMethodAsync<AssetUpsertModel, AssetModel>(endpointUrl, HttpMethod.Put, asset);
Expand All @@ -80,10 +71,7 @@ public async Task<AssetModel> UpsertAssetAsync(Reference identifier, AssetUpsert
/// <inheritdoc />
public async Task<AssetModel<T>> UpsertAssetAsync<T>(Reference identifier, AssetUpsertModel<T> asset) where T : new()
{
if (asset == null)
{
throw new ArgumentNullException(nameof(asset));
}
ArgumentNullException.ThrowIfNull(asset);

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

Expand All @@ -93,10 +81,7 @@ public async Task<AssetModel> UpsertAssetAsync(Reference identifier, AssetUpsert
/// <inheritdoc />
public async Task<AssetModel> CreateAssetAsync(AssetCreateModel asset)
{
if (asset == null)
{
throw new ArgumentNullException(nameof(asset));
}
ArgumentNullException.ThrowIfNull(asset);

var endpointUrl = _urlBuilder.BuildAssetsUrl();
var response = await _actionInvoker.InvokeMethodAsync<AssetCreateModel, AssetModel>(endpointUrl, HttpMethod.Post, asset);
Expand All @@ -107,10 +92,7 @@ public async Task<AssetModel> CreateAssetAsync(AssetCreateModel asset)
/// <inheritdoc />
public async Task<AssetModel<T>> CreateAssetAsync<T>(AssetCreateModel<T> asset) where T : new()
{
if (asset == null)
{
throw new ArgumentNullException(nameof(asset));
}
ArgumentNullException.ThrowIfNull(asset);

var result = await CreateAssetAsync(_modelProvider.GetAssetCreateModel(asset));

Expand All @@ -120,10 +102,7 @@ public async Task<AssetModel> CreateAssetAsync(AssetCreateModel asset)
/// <inheritdoc />
public async Task DeleteAssetAsync(Reference identifier)
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}
ArgumentNullException.ThrowIfNull(identifier);

var endpointUrl = _urlBuilder.BuildAssetsUrl(identifier);
await _actionInvoker.InvokeMethodAsync(endpointUrl, HttpMethod.Delete);
Expand All @@ -132,10 +111,7 @@ public async Task DeleteAssetAsync(Reference identifier)
/// <inheritdoc />
public async Task<FileReference> UploadFileAsync(FileContentSource fileContent)
{
if (fileContent == null)
{
throw new ArgumentNullException(nameof(fileContent));
}
ArgumentNullException.ThrowIfNull(fileContent);

var stream = fileContent.OpenReadStream();
try
Expand Down
10 changes: 2 additions & 8 deletions Kontent.Ai.Management/ManagementClient.AssetFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ public async Task<AssetFoldersModel> GetAssetFoldersAsync()
/// <inheritdoc />
public async Task<AssetFoldersModel> CreateAssetFoldersAsync(AssetFolderCreateModel folder)
{
if (folder == null)
{
throw new ArgumentNullException(nameof(folder));
}
ArgumentNullException.ThrowIfNull(folder);

var endpointUrl = _urlBuilder.BuildAssetFoldersUrl();
var response = await _actionInvoker.InvokeMethodAsync<AssetFolderCreateModel, AssetFoldersModel>(endpointUrl, HttpMethod.Post, folder);
Expand All @@ -35,10 +32,7 @@ public async Task<AssetFoldersModel> CreateAssetFoldersAsync(AssetFolderCreateMo
/// <inheritdoc />
public async Task<AssetFoldersModel> ModifyAssetFoldersAsync(IEnumerable<AssetFolderOperationBaseModel> changes)
{
if (changes == null)
{
throw new ArgumentNullException(nameof(changes));
}
ArgumentNullException.ThrowIfNull(changes);

var endpointUrl = _urlBuilder.BuildAssetFoldersUrl();
var response = await _actionInvoker.InvokeMethodAsync<IEnumerable<AssetFolderOperationBaseModel>, AssetFoldersModel>(endpointUrl, new HttpMethod("PATCH"), changes);
Expand Down
30 changes: 6 additions & 24 deletions Kontent.Ai.Management/ManagementClient.AssetRendition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ public partial class ManagementClient
/// <inheritdoc />
public async Task<IListingResponseModel<AssetRenditionModel>> ListAssetRenditionsAsync(Reference assetIdentifier)
{
if (assetIdentifier == null)
{
throw new ArgumentNullException(nameof(assetIdentifier));
}
ArgumentNullException.ThrowIfNull(assetIdentifier);

var endpointUrl = _urlBuilder.BuildAssetRenditionsUrl(assetIdentifier);
var response = await _actionInvoker.InvokeReadOnlyMethodAsync<AssetRenditionsListingResponseServerModel>(endpointUrl, HttpMethod.Get);
Expand All @@ -29,10 +26,7 @@ public async Task<IListingResponseModel<AssetRenditionModel>> ListAssetRendition
/// <inheritdoc />
public async Task<AssetRenditionModel> GetAssetRenditionAsync(AssetRenditionIdentifier identifier)
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}
ArgumentNullException.ThrowIfNull(identifier);

var endpointUrl = _urlBuilder.BuildAssetRenditionsUrl(identifier);
return await _actionInvoker.InvokeReadOnlyMethodAsync<AssetRenditionModel>(endpointUrl, HttpMethod.Get);
Expand All @@ -41,14 +35,8 @@ public async Task<AssetRenditionModel> GetAssetRenditionAsync(AssetRenditionIden
/// <inheritdoc />
public async Task<AssetRenditionModel> UpdateAssetRenditionAsync(AssetRenditionIdentifier identifier, AssetRenditionUpdateModel updateModel)
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}
if (updateModel == null)
{
throw new ArgumentNullException(nameof(updateModel));
}
ArgumentNullException.ThrowIfNull(identifier);
ArgumentNullException.ThrowIfNull(updateModel);

var endpointUrl = _urlBuilder.BuildAssetRenditionsUrl(identifier);
return await _actionInvoker.InvokeMethodAsync<AssetRenditionUpdateModel, AssetRenditionModel>(endpointUrl, HttpMethod.Put, updateModel);
Expand All @@ -57,15 +45,9 @@ public async Task<AssetRenditionModel> UpdateAssetRenditionAsync(AssetRenditionI
/// <inheritdoc />
public async Task<AssetRenditionModel> CreateAssetRenditionAsync(Reference assetIdentifier, AssetRenditionCreateModel createModel)
{
if (assetIdentifier == null)
{
throw new ArgumentNullException(nameof(assetIdentifier));
}
ArgumentNullException.ThrowIfNull(assetIdentifier);

if (createModel == null)
{
throw new ArgumentNullException(nameof(createModel));
}
ArgumentNullException.ThrowIfNull(createModel);

var endpointUrl = _urlBuilder.BuildAssetRenditionsUrl(assetIdentifier);
return await _actionInvoker.InvokeMethodAsync<AssetRenditionCreateModel, AssetRenditionModel>(endpointUrl, HttpMethod.Post, createModel);
Expand Down
5 changes: 1 addition & 4 deletions Kontent.Ai.Management/ManagementClient.Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ public async Task<CollectionsModel> ListCollectionsAsync()
/// <inheritdoc />
public async Task<CollectionsModel> ModifyCollectionAsync(IEnumerable<CollectionOperationBaseModel> changes)
{
if (changes == null)
{
throw new ArgumentNullException(nameof(changes));
}
ArgumentNullException.ThrowIfNull(changes);

var endpointUrl = _urlBuilder.BuildCollectionsUrl();
return await _actionInvoker.InvokeMethodAsync<IEnumerable<CollectionOperationBaseModel>, CollectionsModel>(endpointUrl, new HttpMethod("PATCH"), changes);
Expand Down
27 changes: 6 additions & 21 deletions Kontent.Ai.Management/ManagementClient.ContentItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task<IListingResponseModel<ContentItemModel>> ListContentItemsAsync
var response = await _actionInvoker.InvokeReadOnlyMethodAsync<ContentItemListingResponseServerModel>(endpointUrl, HttpMethod.Get);

return new ListingResponseModel<ContentItemModel>(
(token, url) => GetNextListingPageAsync<ContentItemListingResponseServerModel, ContentItemModel>(token, url),
GetNextListingPageAsync<ContentItemListingResponseServerModel, ContentItemModel>,
response.Pagination?.Token,
endpointUrl,
response.Items);
Expand All @@ -24,10 +24,7 @@ public async Task<IListingResponseModel<ContentItemModel>> ListContentItemsAsync
/// <inheritdoc />
public async Task<ContentItemModel> GetContentItemAsync(Reference identifier)
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}
ArgumentNullException.ThrowIfNull(identifier);

var endpointUrl = _urlBuilder.BuildItemUrl(identifier);
var response = await _actionInvoker.InvokeReadOnlyMethodAsync<ContentItemModel>(endpointUrl, HttpMethod.Get);
Expand All @@ -38,10 +35,7 @@ public async Task<ContentItemModel> GetContentItemAsync(Reference identifier)
/// <inheritdoc />
public async Task<ContentItemModel> CreateContentItemAsync(ContentItemCreateModel contentItem)
{
if (contentItem == null)
{
throw new ArgumentNullException(nameof(contentItem));
}
ArgumentNullException.ThrowIfNull(contentItem);

var endpointUrl = _urlBuilder.BuildItemsUrl();
var response = await _actionInvoker.InvokeMethodAsync<ContentItemCreateModel, ContentItemModel>(endpointUrl, HttpMethod.Post, contentItem);
Expand All @@ -52,15 +46,9 @@ public async Task<ContentItemModel> CreateContentItemAsync(ContentItemCreateMode
/// <inheritdoc />
public async Task<ContentItemModel> UpsertContentItemAsync(Reference identifier, ContentItemUpsertModel contentItem)
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}
ArgumentNullException.ThrowIfNull(identifier);

if (contentItem == null)
{
throw new ArgumentNullException(nameof(contentItem));
}
ArgumentNullException.ThrowIfNull(contentItem);

var endpointUrl = _urlBuilder.BuildItemUrl(identifier);
var response = await _actionInvoker.InvokeMethodAsync<ContentItemUpsertModel, ContentItemModel>(endpointUrl, HttpMethod.Put, contentItem);
Expand All @@ -71,10 +59,7 @@ public async Task<ContentItemModel> UpsertContentItemAsync(Reference identifier,
/// <inheritdoc />
public async Task DeleteContentItemAsync(Reference identifier)
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}
ArgumentNullException.ThrowIfNull(identifier);

var endpointUrl = _urlBuilder.BuildItemUrl(identifier);

Expand Down
Loading

0 comments on commit 742f458

Please sign in to comment.