Skip to content

Commit

Permalink
add type resolution support, refactor, adjust method descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
pokornyd committed Oct 24, 2023
1 parent bae868a commit f58c20a
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Kontent.Ai.Delivery.Abstractions;

/// <summary>
/// Represents a response from Kontent.ai Delivery API that contains a continuation token and .
/// Represents a response from Kontent.ai Sync API. Response includes continuation token for subsequent synchronization calls. Sync initialization should always return an empty list.
/// </summary>
public interface IDeliverySyncInitResponse : IResponse
{
Expand Down
5 changes: 4 additions & 1 deletion Kontent.Ai.Delivery.Abstractions/Sync/ISyncItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ namespace Kontent.Ai.Delivery.Abstractions;
/// </summary>
public interface ISyncItem
{
ISyncItemData Data { get; }
/// <summary>
/// Retrieves content item information and element values.
/// </summary>
object Data { get; }

/// <summary>
/// Gets the information whether the content item was modified or deleted since the last synchronization.
Expand Down
12 changes: 0 additions & 12 deletions Kontent.Ai.Delivery.Abstractions/Sync/ISyncItemData.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Kontent.Ai.Delivery.Caching/DeliveryClientCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public async Task<IDeliveryLanguageListingResponse> GetLanguagesAsync(IEnumerabl
/// <summary>
/// Initializes synchronization of changes in content items based on the specified parameters. After the initialization, you'll get an X-Continuation token in the response.
/// </summary>
/// <param name="parameters">A collection of query parameters, for example, for filtering.</param>
/// <param name="parameters">A collection of query parameters, for example to limit synchronization to only a subset of collections or content types.</param>
/// <returns>The <see cref="IDeliverySyncInitResponse"/> instance that represents the sync init response that contains continuation token needed for further sync execution.</returns>
public Task<IDeliverySyncInitResponse> PostSyncInitAsync(IEnumerable<IQueryParameter> parameters = null)
{
Expand Down
26 changes: 24 additions & 2 deletions Kontent.Ai.Delivery/DeliveryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ public async Task<IDeliveryLanguageListingResponse> GetLanguagesAsync(IEnumerabl
return new DeliveryLanguageListingResponse(response, languages.ToList<ILanguage>(), pagination);
}

/// <summary>
/// Initializes synchronization of changes in content items based on the specified parameters. After the initialization, you'll get an X-Continuation token in the response.
/// </summary>
/// <param name="parameters">A collection of query parameters, for example to limit synchronization to only a subset of collections or content types.</param>
/// <returns>The <see cref="IDeliverySyncInitResponse"/> instance that represents the sync init response that contains continuation token needed for further sync execution.</returns>
public async Task<IDeliverySyncInitResponse> PostSyncInitAsync(IEnumerable<IQueryParameter> parameters = null)
{
var endpointUrl = UrlBuilder.GetSyncInitUrl(parameters);
Expand All @@ -329,6 +334,10 @@ public async Task<IDeliverySyncInitResponse> PostSyncInitAsync(IEnumerable<IQuer
return new DeliverySyncInitResponse(response, items.ToList<ISyncItem>());
}

/// <summary>
/// Retrieve a list of delta updates to recently changed content items in the specified project. The types of items you get is determined by the X-Continuation token you use.
/// </summary>
/// <returns>The <see cref="IDeliverySyncResponse"/> instance that represents the sync response that contains collection of delta updates and continuation token needed for further sync execution.</returns>
public async Task<IDeliverySyncResponse> GetSyncAsync(string continuationToken)
{
var endpointUrl = UrlBuilder.GetSyncUrl();
Expand All @@ -340,10 +349,23 @@ public async Task<IDeliverySyncResponse> GetSyncAsync(string continuationToken)
}

var content = await response.GetJsonContentAsync();
var items = content["items"].ToObject<List<SyncItem>>(Serializer);
return new DeliverySyncResponse(response, items.ToList<ISyncItem>());
var syncItems = content["items"].ToObject<List<SyncItem>>(Serializer);

var itemModels = await Task.WhenAll(syncItems.Select(async syncItem =>
{
// use TypeProvider from DI container to select a model
var mappedModel = await ModelProvider.GetContentItemModelAsync<object>(syncItem.Data, new JObject());
if (mappedModel == null)
{
// return JObject if no suitable model is found
return new SyncItem(syncItem.Data, syncItem.ChangeType, syncItem.Timestamp);
}
return new SyncItem(mappedModel, syncItem.ChangeType, syncItem.Timestamp);
}));
return new DeliverySyncResponse(response, itemModels);
}


private async Task<ApiResponse> GetDeliveryResponseAsync(string endpointUrl, HttpMethod httpMethod, string continuationToken = null)
{
if (DeliveryOptions.CurrentValue.UsePreviewApi && DeliveryOptions.CurrentValue.UseSecureAccess)
Expand Down
2 changes: 1 addition & 1 deletion Kontent.Ai.Delivery/Sync/DeliverySyncResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal DeliverySyncResponse(IApiResponse response, IList<ISyncItem> syncItems)
SyncItems = syncItems;
}

internal DeliverySyncResponse(IApiResponse response)
internal DeliverySyncResponse(IApiResponse response)
: base(response)
{
}
Expand Down
23 changes: 12 additions & 11 deletions Kontent.Ai.Delivery/Sync/SyncItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
namespace Kontent.Ai.Delivery.Sync;

/// <inheritdoc/>
internal sealed class SyncItem : ISyncItem
/// <summary>
/// Constructor used for deserialization (e.g. for caching purposes), contains no logic.
/// </summary>
[method: JsonConstructor]
/// <<inheritdoc/>>
internal sealed class SyncItem(object data, string changeType, DateTime timestamp) : ISyncItem
{
/// <inheritdoc/>
[JsonProperty("data")]
public ISyncItemData Data { get; internal set; }
public object Data { get; internal set; } = data;

/// <inheritdoc/>
[JsonProperty("change_type")]
public string ChangeType { get; internal set; }
public string ChangeType { get; internal set; } = changeType;

/// <inheritdoc/>
[JsonProperty("timestamp")]
public DateTime Timestamp { get; internal set; }

[JsonConstructor]
public SyncItem(ISyncItemData data, string changeType, DateTime timestamp) {
Data = data;
ChangeType = changeType;
Timestamp = timestamp;
}
public DateTime Timestamp { get; internal set; } = timestamp;
}
22 changes: 0 additions & 22 deletions Kontent.Ai.Delivery/Sync/SyncItemData.cs

This file was deleted.

0 comments on commit f58c20a

Please sign in to comment.