public void Go(ContentstackClient stack, OutputWriter writer) { // https://www.contentstack.com/docs/developers/apis/content-delivery-api/#initial-sync // https://www.contentstack.com/docs/platforms/dot-net/api-reference/api/Contentstack.Core.Models.SyncStack.html SyncStack syncStack = stack.SyncRecursive( null, /*Locale*/ SyncType.All, null, /* ContentTypeUID */ DateTime.MinValue).Result; //WARN: in case of multiple events for an Entry, threads running out of event date sequence could load stale data foreach (JObject entryEvent in syncStack.items) { //WARN: https://stackoverflow.com/questions/271440/captured-variable-in-a-loop-in-c-sharp JObject myEntryEvent = entryEvent; ProcessMessage(stack, myEntryEvent, writer); } string syncToken = syncStack.sync_token; // https://www.contentstack.com/docs/developers/apis/content-delivery-api/#subsequent-sync SyncStack updatesSinceSync = stack.SyncToken(syncToken).Result; writer.Message(this, updatesSinceSync.items.Count() + " update(s) since last sync"); foreach (JObject entryEvent in updatesSinceSync.items) { //WARN: https://stackoverflow.com/questions/271440/captured-variable-in-a-loop-in-c-sharp JObject myEntryEvent = entryEvent; ProcessMessage(stack, myEntryEvent, writer); } }
/// <summary> /// Syncs the recursive with language. /// </summary> /// <returns>The recursive with language.</returns> /// <param name="SyncType">Sync type.</param> /// <param name="ContentTypeUid">Content type uid.</param> /// <param name="StartFrom">Start from Date.</param> /// <param name="Lang">Lang.</param> /// <example> /// <code> /// //"blt5d4sample2633b" is a dummy Stack API key /// //"blt6d0240b5sample254090d" is dummy access token. /// ContentstackClient stack = new ContentstackClinet("blt5d4sample2633b", "blt6d0240b5sample254090d", "stag"); /// stack.SyncRecursiveLanguage("SyncType", "Language"); /// </code> /// </example> /// public async Task <SyncStack> SyncRecursiveLanguage(Language Lang, SyncType SyncType = SyncType.All, string ContentTypeUid = null, DateTime?StartFrom = null) { SyncStack syncStack = await SyncLanguage(Lang : Lang, SyncType : SyncType, ContentTypeUid : ContentTypeUid, StartFrom : StartFrom); syncStack = await SyncPageinationRecursive(syncStack); return(syncStack); }
public async Task SyncTypeWithStartFrom() { SyncStack result = await client.SyncRecursive(SyncType : SyncType.EntryPublished, StartFrom : DateTime.Now); if (result == null) { Assert.False(true, "Entry.Fetch is not match with expected result."); } else { Assert.True(true); } }
public async Task SyncTypeWithContentType() { SyncStack result = await client.SyncRecursive(SyncType : SyncType.EntryPublished, ContentTypeUid : "source"); if (result == null) { Assert.False(true, "Entry.Fetch is not match with expected result."); } else { Assert.True(true); } }
public async Task SyncSyncType() { SyncStack result = await client.SyncRecursive(SyncType : SyncType.AssetPublished); if (result == null) { Assert.False(true, "Entry.Fetch is not match with expected result."); } else { Assert.True(true); } }
private async Task <SyncStack> SyncPageinationRecursive(SyncStack syncStack) { while (syncStack.PaginationToken != null) { SyncStack newSyncStack = await SyncPaginationToken(syncStack.PaginationToken); syncStack.Items = syncStack.Items.Concat(newSyncStack.Items); syncStack.PaginationToken = newSyncStack.PaginationToken; syncStack.TotalCount = newSyncStack.TotalCount; syncStack.SyncToken = newSyncStack.SyncToken; } return(syncStack); }
private async Task <SyncStack> SyncPageinationRecursive(SyncStack syncStack) { while (syncStack.pagination_token != null) { SyncStack newSyncStack = await SyncPaginationToken(syncStack.pagination_token); syncStack.items = syncStack.items.Concat(newSyncStack.items); syncStack.pagination_token = newSyncStack.pagination_token; syncStack.skip = newSyncStack.skip; syncStack.total_count = newSyncStack.total_count; syncStack.sync_token = newSyncStack.sync_token; } return(syncStack); }
private async Task <SyncStack> GetResultAsync(string Init = "false", string ContentTypeUid = null, DateTime?StartFrom = null, string SyncToken = null, string PaginationToken = null, string Lang = null) { //mainJson = null; Dictionary <string, object> mainJson = new Dictionary <string, object>(); if (Init != "false") { mainJson.Add("init", "true"); mainJson.Add("environment", config.Environment); } if (StartFrom != null) { DateTime startFrom = StartFrom ?? DateTime.MinValue; mainJson.Add("start_from", startFrom.ToString("yyyy-MM-dd")); } if (SyncToken != null) { mainJson.Add("sync_token", SyncToken); } if (PaginationToken != null) { mainJson.Add("pagination_token", PaginationToken); } if (ContentTypeUid != null) { mainJson.Add("content_type_uid", ContentTypeUid); } if (Lang != null) { mainJson.Add("locale", Lang); } try { HTTPRequestHandler requestHandler = new HTTPRequestHandler(); string js = await requestHandler.ProcessRequest(_SyncUrl, _LocalHeaders, mainJson); SyncStack stackSyncOutput = JsonConvert.DeserializeObject <SyncStack>(js); return(stackSyncOutput); } catch (Exception ex) { throw GetContentstackError(ex); } }
private async Task <SyncStack> GetResultAsync(string Init = "false", SyncType SyncType = SyncType.All, string ContentTypeUid = null, DateTime?StartFrom = null, string SyncToken = null, string PaginationToken = null, string Locale = null) { //mainJson = null; Dictionary <string, object> mainJson = new Dictionary <string, object>(); if (Init != "false") { mainJson.Add("init", "true"); mainJson.Add("environment", this.Config.Environment); } if (StartFrom != null) { DateTime startFrom = StartFrom ?? DateTime.MinValue; mainJson.Add("start_from", startFrom.ToString("yyyy-MM-ddTHH\\:mm\\:ss.sssZ")); } if (SyncToken != null) { mainJson.Add("sync_token", SyncToken); } if (PaginationToken != null) { mainJson.Add("pagination_token", PaginationToken); } if (ContentTypeUid != null) { mainJson.Add("content_type_uid", ContentTypeUid); } if (Locale != null) { mainJson.Add("locale", Locale); } if (SyncType.HasFlag(SyncType.All)) { } if (SyncType.HasFlag(SyncType.All)) { mainJson.Add("type", "entry_published,asset_published,entry_unpublished,asset_unpublished,entry_deleted,asset_deleted,content_type_deleted"); } else { List <string> Type = new List <string>(); if (SyncType.HasFlag(SyncType.EntryDeleted)) { Type.Add("entry_deleted"); } if (SyncType.HasFlag(SyncType.EntryPublished)) { Type.Add("entry_published"); } if (SyncType.HasFlag(SyncType.EntryUnpublished)) { Type.Add("entry_unpublished"); } if (SyncType.HasFlag(SyncType.AssetDeleted)) { Type.Add("asset_deleted"); } if (SyncType.HasFlag(SyncType.AssetPublished)) { Type.Add("asset_published"); } if (SyncType.HasFlag(SyncType.AssetUnpublished)) { Type.Add("asset_unpublished"); } if (SyncType.HasFlag(SyncType.ContentTypeDeleted)) { Type.Add("content_type_deleted"); } mainJson.Add("type", String.Join(",", Type.ToArray())); } try { HttpRequestHandler requestHandler = new HttpRequestHandler(); string js = await requestHandler.ProcessRequest(_SyncUrl, _LocalHeaders, mainJson); SyncStack stackSyncOutput = JsonConvert.DeserializeObject <SyncStack>(js); return(stackSyncOutput); } catch (Exception ex) { throw GetContentstackError(ex); } }