From f771f209a278bdcb7be25a5506f0ae8a4748b47b Mon Sep 17 00:00:00 2001 From: zdenekj Date: Thu, 29 Feb 2024 09:12:00 +0100 Subject: [PATCH 1/2] Add support for exclude parameter --- .../DeliveryClientTests.cs | 6 ++-- .../QueryParameters/ExcludeParameter.cs | 35 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 Kontent.Ai.Urls/Delivery/QueryParameters/ExcludeParameter.cs diff --git a/Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs b/Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs index 7ed77e24..12855b08 100644 --- a/Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs +++ b/Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs @@ -732,9 +732,8 @@ public async Task GetLanguagesAsync() [Fact] public async Task QueryParameters() { - string url = $"{_baseUrl}/items?elements.personas%5Ball%5D=barista%2Ccoffee%2Cblogger&elements.personas%5Bany%5D=barista%2Ccoffee%2Cblogger&system.sitemap_locations%5Bcontains%5D=cafes&elements.product_name%5Beq%5D=Hario%20V60&elements.product_name%5Bneq%5D=Hario%20V42&elements.price%5Bgt%5D=1000&elements.price%5Bgte%5D=50&system.type%5Bin%5D=cafe%2Ccoffee&system.type%5Bnin%5D=article%2Cblog_post&elements.price%5Blt%5D=10&elements.price%5Blte%5D=4&elements.country%5Brange%5D=Guatemala%2CNicaragua&elements.price%5Bempty%5D&elements.country%5Bnempty%5D&depth=2&elements=price%2Cproduct_name&limit=10&order=elements.price%5Bdesc%5D&skip=2&language=en&includeTotalCount"; - _mockHttp - .When($"{url}") + _mockHttp.Expect($"{_baseUrl}/items") + .WithExactQueryString("elements.personas%5Ball%5D=barista%2Ccoffee%2Cblogger&elements.personas%5Bany%5D=barista%2Ccoffee%2Cblogger&system.sitemap_locations%5Bcontains%5D=cafes&elements.product_name%5Beq%5D=Hario%20V60&elements.product_name%5Bneq%5D=Hario%20V42&elements.price%5Bgt%5D=1000&elements.price%5Bgte%5D=50&system.type%5Bin%5D=cafe%2Ccoffee&system.type%5Bnin%5D=article%2Cblog_post&elements.price%5Blt%5D=10&elements.price%5Blte%5D=4&elements.country%5Brange%5D=Guatemala%2CNicaragua&elements.price%5Bempty%5D&elements.country%5Bnempty%5D&depth=2&elements=price%2Cproduct_name&exclude=product_description%2Cproduct_thumbnail&limit=10&order=elements.price%5Bdesc%5D&skip=2&language=en&includeTotalCount") .Respond("application/json", " { 'items': [],'modular_content': {},'pagination': {'skip': 2,'limit': 10,'count': 0, 'total_count': 0, 'next_page': ''}}"); var client = InitializeDeliveryClientWithACustomTypeProvider(_mockHttp); @@ -757,6 +756,7 @@ public async Task QueryParameters() new NotEmptyFilter("elements.country"), new DepthParameter(2), new ElementsParameter("price", "product_name"), + new ExcludeParameter("product_description", "product_thumbnail"), new LimitParameter(10), new OrderParameter("elements.price", SortOrder.Descending), new SkipParameter(2), diff --git a/Kontent.Ai.Urls/Delivery/QueryParameters/ExcludeParameter.cs b/Kontent.Ai.Urls/Delivery/QueryParameters/ExcludeParameter.cs new file mode 100644 index 00000000..efde9abf --- /dev/null +++ b/Kontent.Ai.Urls/Delivery/QueryParameters/ExcludeParameter.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Kontent.Ai.Delivery.Abstractions; + +namespace Kontent.Ai.Urls.Delivery.QueryParameters +{ + /// + /// Specifies the content elements to exclude from response. + /// + public sealed class ExcludeParameter : IQueryParameter + { + /// + /// Gets the list of codenames of content elements that should be excluded from response. + /// + public IReadOnlyList ElementCodenames { get; } + + /// + /// Initializes a new instance of the class using the specified content elements codenames. + /// + /// An array that contains zero or more codenames of the content elements that should be excluded from response. + public ExcludeParameter(params string[] elementCodenames) + { + ElementCodenames = elementCodenames.ToList().AsReadOnly(); + } + + /// + /// Returns the query string representation of the query parameter. + /// + public string GetQueryStringParameter() + { + return $"exclude={string.Join(Uri.EscapeDataString(","), ElementCodenames.Select(Uri.EscapeDataString))}"; + } + } +} \ No newline at end of file From 4553b37f36051e11142b4b692268a046c7b50d4a Mon Sep 17 00:00:00 2001 From: zdenekj Date: Mon, 4 Mar 2024 11:55:58 +0100 Subject: [PATCH 2/2] Rename ExcludeParameter to ExcludeElementsParameter --- Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs | 4 ++-- .../{ExcludeParameter.cs => ExcludeElementsParameter.cs} | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) rename Kontent.Ai.Urls/Delivery/QueryParameters/{ExcludeParameter.cs => ExcludeElementsParameter.cs} (75%) diff --git a/Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs b/Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs index 12855b08..2b0a4e6e 100644 --- a/Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs +++ b/Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs @@ -733,7 +733,7 @@ public async Task GetLanguagesAsync() public async Task QueryParameters() { _mockHttp.Expect($"{_baseUrl}/items") - .WithExactQueryString("elements.personas%5Ball%5D=barista%2Ccoffee%2Cblogger&elements.personas%5Bany%5D=barista%2Ccoffee%2Cblogger&system.sitemap_locations%5Bcontains%5D=cafes&elements.product_name%5Beq%5D=Hario%20V60&elements.product_name%5Bneq%5D=Hario%20V42&elements.price%5Bgt%5D=1000&elements.price%5Bgte%5D=50&system.type%5Bin%5D=cafe%2Ccoffee&system.type%5Bnin%5D=article%2Cblog_post&elements.price%5Blt%5D=10&elements.price%5Blte%5D=4&elements.country%5Brange%5D=Guatemala%2CNicaragua&elements.price%5Bempty%5D&elements.country%5Bnempty%5D&depth=2&elements=price%2Cproduct_name&exclude=product_description%2Cproduct_thumbnail&limit=10&order=elements.price%5Bdesc%5D&skip=2&language=en&includeTotalCount") + .WithExactQueryString("elements.personas%5Ball%5D=barista%2Ccoffee%2Cblogger&elements.personas%5Bany%5D=barista%2Ccoffee%2Cblogger&system.sitemap_locations%5Bcontains%5D=cafes&elements.product_name%5Beq%5D=Hario%20V60&elements.product_name%5Bneq%5D=Hario%20V42&elements.price%5Bgt%5D=1000&elements.price%5Bgte%5D=50&system.type%5Bin%5D=cafe%2Ccoffee&system.type%5Bnin%5D=article%2Cblog_post&elements.price%5Blt%5D=10&elements.price%5Blte%5D=4&elements.country%5Brange%5D=Guatemala%2CNicaragua&elements.price%5Bempty%5D&elements.country%5Bnempty%5D&depth=2&elements=price%2Cproduct_name&excludeElements=product_description%2Cproduct_thumbnail&limit=10&order=elements.price%5Bdesc%5D&skip=2&language=en&includeTotalCount") .Respond("application/json", " { 'items': [],'modular_content': {},'pagination': {'skip': 2,'limit': 10,'count': 0, 'total_count': 0, 'next_page': ''}}"); var client = InitializeDeliveryClientWithACustomTypeProvider(_mockHttp); @@ -756,7 +756,7 @@ public async Task QueryParameters() new NotEmptyFilter("elements.country"), new DepthParameter(2), new ElementsParameter("price", "product_name"), - new ExcludeParameter("product_description", "product_thumbnail"), + new ExcludeElementsParameter("product_description", "product_thumbnail"), new LimitParameter(10), new OrderParameter("elements.price", SortOrder.Descending), new SkipParameter(2), diff --git a/Kontent.Ai.Urls/Delivery/QueryParameters/ExcludeParameter.cs b/Kontent.Ai.Urls/Delivery/QueryParameters/ExcludeElementsParameter.cs similarity index 75% rename from Kontent.Ai.Urls/Delivery/QueryParameters/ExcludeParameter.cs rename to Kontent.Ai.Urls/Delivery/QueryParameters/ExcludeElementsParameter.cs index efde9abf..f4fd1215 100644 --- a/Kontent.Ai.Urls/Delivery/QueryParameters/ExcludeParameter.cs +++ b/Kontent.Ai.Urls/Delivery/QueryParameters/ExcludeElementsParameter.cs @@ -8,7 +8,7 @@ namespace Kontent.Ai.Urls.Delivery.QueryParameters /// /// Specifies the content elements to exclude from response. /// - public sealed class ExcludeParameter : IQueryParameter + public sealed class ExcludeElementsParameter : IQueryParameter { /// /// Gets the list of codenames of content elements that should be excluded from response. @@ -16,10 +16,10 @@ public sealed class ExcludeParameter : IQueryParameter public IReadOnlyList ElementCodenames { get; } /// - /// Initializes a new instance of the class using the specified content elements codenames. + /// Initializes a new instance of the class using the specified content elements codenames. /// /// An array that contains zero or more codenames of the content elements that should be excluded from response. - public ExcludeParameter(params string[] elementCodenames) + public ExcludeElementsParameter(params string[] elementCodenames) { ElementCodenames = elementCodenames.ToList().AsReadOnly(); } @@ -29,7 +29,7 @@ public ExcludeParameter(params string[] elementCodenames) /// public string GetQueryStringParameter() { - return $"exclude={string.Join(Uri.EscapeDataString(","), ElementCodenames.Select(Uri.EscapeDataString))}"; + return $"excludeElements={string.Join(Uri.EscapeDataString(","), ElementCodenames.Select(Uri.EscapeDataString))}"; } } } \ No newline at end of file