Example #1
0
        /// <inheritdoc />
        public IEnumerable <IDocument> Execute(IReadOnlyList <IDocument> inputs, IExecutionContext context)
        {
            Space space = null;

            try
            {
                space = _client.GetSpaceAsync().Result;
            }
            catch (AggregateException ae)
            {
                ae.Handle((ex) => {
                    if (ex is ContentfulException)
                    {
                        Trace.TraceError($"Error when fetching space from Contentful: {ex.Message}");
                        Trace.TraceError($"Details: {(ex as ContentfulException).ErrorDetails.Errors}");
                        Trace.TraceError($"ContentfulRequestId:{(ex as ContentfulException).RequestId}");
                    }

                    return(false);
                });
            }

            var queryBuilder = CreateQueryBuilder();
            ContentfulCollection <Entry <dynamic> > entries = null;

            try
            {
                entries = _client.GetEntriesAsync(queryBuilder).Result;
            }
            catch (AggregateException ae)
            {
                ae.Handle((ex) => {
                    if (ex is ContentfulException)
                    {
                        Trace.TraceError($"Error when fetching entries from Contentful: {ex.Message}");
                        Trace.TraceError($"Details: {(ex as ContentfulException).ErrorDetails.Errors}");
                        Trace.TraceError($"ContentfulRequestId:{(ex as ContentfulException).RequestId}");
                    }

                    return(false);
                });
            }

            if (_recursive && entries.Total > entries.Items.Count())
            {
                entries = FetchEntriesRecursively(entries);
            }

            var includedAssets  = entries.IncludedAssets;
            var includedEntries = entries.IncludedEntries;

            var locales = space.Locales.Where(l => l.Default);

            if (_locale == "*")
            {
                locales = space.Locales;
            }
            else if (!string.IsNullOrEmpty(_locale))
            {
                locales = space.Locales.Where(l => l.Code == _locale);
            }

            if (!locales.Any())
            {
                //Warn or throw here?
                throw new ArgumentException($"Locale {_locale} not found for space. Note that locale codes are case-sensitive.");
            }

            foreach (var entry in entries)
            {
                foreach (var locale in locales)
                {
                    var localeCode = locale.Code;

                    var items = (entry.Fields as IEnumerable <KeyValuePair <string, JToken> >).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                    var content = items.ContainsKey(_contentField) ? items[_contentField][localeCode].ToString() : "No content";

                    if (string.IsNullOrEmpty(_contentField))
                    {
                        content = "";
                    }

                    var metaData = items.Select(c => new KeyValuePair <string, object>(c.Key, c.Value[localeCode])).ToList();
                    metaData.Add(new KeyValuePair <string, object>(ContentfulKeys.EntryId, $"{entry.SystemProperties.Id}"));
                    metaData.Add(new KeyValuePair <string, object>(ContentfulKeys.EntryLocale, localeCode));
                    metaData.Add(new KeyValuePair <string, object>(ContentfulKeys.IncludedAssets, includedAssets));
                    metaData.Add(new KeyValuePair <string, object>(ContentfulKeys.IncludedEntries, includedEntries));
                    var doc = context.GetDocument(context.GetContentStream(content), metaData);

                    yield return(doc);
                }
            }
        }