Ejemplo n.º 1
0
        private IDeliveryClient GetDeliveryClient(Action mockAction)
        {
            mockAction();
            var deliveryHttpClient = new DeliveryHttpClient(mockHttp.ToHttpClient());
            var deliveryOptions    = DeliveryOptionsFactory.CreateMonitor(new DeliveryOptions {
                ProjectId = guid
            });
            var contentLinkUrlResolver = A.Fake <IContentLinkUrlResolver>();
            var contentLinkResolver    = new ContentLinkResolver(contentLinkUrlResolver);
            var contentItemsProcessor  = A.Fake <IInlineContentItemsProcessor>();
            var contentPropertyMapper  = new PropertyMapper();
            var contentTypeProvider    = new CustomTypeProvider();
            var modelProvider          = new ModelProvider(contentLinkResolver, contentItemsProcessor, contentTypeProvider, contentPropertyMapper);
            var retryPolicy            = A.Fake <IRetryPolicy>();
            var retryPolicyProvider    = A.Fake <IRetryPolicyProvider>();

            A.CallTo(() => retryPolicyProvider.GetRetryPolicy()).Returns(retryPolicy);
            A.CallTo(() => retryPolicy.ExecuteAsync(A <Func <Task <HttpResponseMessage> > > ._))
            .ReturnsLazily(call => call.GetArgument <Func <Task <HttpResponseMessage> > >(0)());
            var client = new DeliveryClient(
                deliveryOptions,
                contentLinkResolver,
                null,
                modelProvider,
                retryPolicyProvider,
                contentTypeProvider,
                null,
                deliveryHttpClient
                );

            return(client);
        }
        public object GetPropertyValue(PropertyInfo property, JToken elementData, ResolvingContext context)
        {
            if (!typeof(IRichTextContent).IsAssignableFrom(property.PropertyType))
            {
                throw new InvalidOperationException($"Type of property {property.Name} must implement {nameof(IRichTextContent)} in order to receive rich text content.");
            }

            var element = ((JObject)elementData);

            if (element == null)
            {
                return(null);
            }

            var links = element.Property("links")?.Value;
            var value = element.Property("value")?.Value?.ToObject <string>();

            // Handle rich_text link resolution
            if (links != null && elementData != null && context.ContentLinkUrlResolver != null)
            {
                value = new ContentLinkResolver(context.ContentLinkUrlResolver).ResolveContentLinks(value, links);
            }

            var blocks = new List <IRichTextBlock>();

            var htmlInput = new HtmlParser().Parse(value);

            foreach (var block in htmlInput.Body.Children)
            {
                if (block.TagName?.Equals("object", StringComparison.OrdinalIgnoreCase) == true && block.GetAttribute("type") == "application/kenticocloud" && block.GetAttribute("data-type") == "item")
                {
                    var codename = block.GetAttribute("data-codename");
                    blocks.Add(new InlineContentItem {
                        ContentItem = context.GetLinkedItem(codename)
                    });
                }
                else if (block.TagName?.Equals("figure", StringComparison.OrdinalIgnoreCase) == true)
                {
                    var img = block.Children.FirstOrDefault(child => child.TagName?.Equals("img", StringComparison.OrdinalIgnoreCase) == true);
                    if (img != null)
                    {
                        blocks.Add(new InlineImage {
                            Src = img.GetAttribute("src"), AltText = img.GetAttribute("alt")
                        });
                    }
                }
                else
                {
                    blocks.Add(new HtmlContent {
                        Html = block.OuterHtml
                    });
                }
            }

            return(new RichTextContent
            {
                Blocks = blocks
            });
        }
Ejemplo n.º 3
0
        private string ResolveContentLinks(string text, CustomContentLinkUrlResolver linkUrlResolver)
        {
            var linkResolver = new ContentLinkResolver(linkUrlResolver);
            var links        = JObject.FromObject(new
            {
                CID = new
                {
                    codename = "about_us",
                    type     = "article",
                    url_slug = "about-us"
                }
            });

            return(linkResolver.ResolveContentLinks(text, links));
        }
Ejemplo n.º 4
0
        private async Task <string> ResolveContentLinks(string text, CustomContentLinkUrlResolver linkUrlResolver)
        {
            var          linkResolver = new ContentLinkResolver(linkUrlResolver);
            IContentLink link         = new ContentLink()
            {
                ContentTypeCodename = "article",
                UrlSlug             = "about-us",
                Codename            = "about_us"
            };

            link.Id = ContentItemIdA;
            var links = new Dictionary <Guid, IContentLink> {
                { ContentItemIdA, link }
            };

            return(await linkResolver.ResolveContentLinksAsync(text, links));
        }
Ejemplo n.º 5
0
        public async void ResolveLinksInStronglyTypedModel()
        {
            var    mockHttp = new MockHttpMessageHandler();
            string guid     = Guid.NewGuid().ToString();
            string url      = $"https://deliver.kontent.ai/{guid}/items/coffee_processing_techniques";

            mockHttp.When(url).
            Respond("application/json", File.ReadAllText(Path.Combine(Environment.CurrentDirectory, $"Fixtures{Path.DirectorySeparatorChar}ContentLinkResolver{Path.DirectorySeparatorChar}coffee_processing_techniques.json")));

            var deliveryOptions = DeliveryOptionsFactory.CreateMonitor(new DeliveryOptions {
                ProjectId = guid
            });
            var options = DeliveryOptionsFactory.Create(new DeliveryOptions {
                ProjectId = guid
            });
            var deliveryHttpClient       = new DeliveryHttpClient(mockHttp.ToHttpClient());
            var resiliencePolicyProvider = new DefaultRetryPolicyProvider(options);
            var contentLinkUrlResolver   = new CustomContentLinkUrlResolver();
            var contentLinkResolver      = new ContentLinkResolver(contentLinkUrlResolver);
            var contentItemsProcessor    = InlineContentItemsProcessorFactory.Create();
            var modelProvider            = new ModelProvider(contentLinkResolver, contentItemsProcessor, new CustomTypeProvider(), new PropertyMapper());
            var client = new DeliveryClient(
                deliveryOptions,
                contentLinkResolver,
                contentItemsProcessor,
                modelProvider,
                resiliencePolicyProvider,
                null,
                null,
                deliveryHttpClient
                );


            string expected = "Check out our <a data-item-id=\"0c9a11bb-6fc3-409c-b3cb-f0b797e15489\" href=\"http://example.org/brazil-natural-barra-grande\">Brazil Natural Barra Grande</a> coffee for a tasty example.";
            var    item     = await client.GetItemAsync <Article>("coffee_processing_techniques");

            Assert.Contains(expected, item.Item.BodyCopy);
        }