From 7810cb01497988fa8be21261704b7e180b3431fd Mon Sep 17 00:00:00 2001 From: Huib Piguillet Date: Wed, 20 Oct 2021 18:00:07 +0200 Subject: [PATCH] Fix url path appending (#61) * Introduced UriBuilder.AppendPath extension to be used instead of UriBuilder.Path * Updated version --- .../Sdk/Api/RequestSender/ApiRequestSender.cs | 6 ++-- Bynder/Sdk/Bynder.Sdk.csproj | 8 +++--- Bynder/Sdk/Extensions/UriBuilderExtensions.cs | 28 +++++++++++++++++++ Bynder/Sdk/Service/OAuth/OAuthService.cs | 4 +-- .../Extensions/UriBuilderExtensionsTest.cs | 20 +++++++++++++ 5 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 Bynder/Sdk/Extensions/UriBuilderExtensions.cs create mode 100644 Bynder/Test/Extensions/UriBuilderExtensionsTest.cs diff --git a/Bynder/Sdk/Api/RequestSender/ApiRequestSender.cs b/Bynder/Sdk/Api/RequestSender/ApiRequestSender.cs index 5cc90f8..e2cb365 100644 --- a/Bynder/Sdk/Api/RequestSender/ApiRequestSender.cs +++ b/Bynder/Sdk/Api/RequestSender/ApiRequestSender.cs @@ -8,6 +8,7 @@ using System.Threading; using System.Threading.Tasks; using Bynder.Sdk.Api.Requests; +using Bynder.Sdk.Extensions; using Bynder.Sdk.Query.Decoder; using Bynder.Sdk.Service.OAuth; using Bynder.Sdk.Settings; @@ -132,10 +133,7 @@ private static class HttpRequestMessageFactory internal static HttpRequestMessage Create( string baseUrl, HttpMethod method, IDictionary requestParams, string urlPath) { - var builder = new UriBuilder(baseUrl) - { - Path = urlPath - }; + var builder = new UriBuilder(baseUrl).AppendPath(urlPath); if (HttpMethod.Get == method) { diff --git a/Bynder/Sdk/Bynder.Sdk.csproj b/Bynder/Sdk/Bynder.Sdk.csproj index 522319b..9d8cab6 100644 --- a/Bynder/Sdk/Bynder.Sdk.csproj +++ b/Bynder/Sdk/Bynder.Sdk.csproj @@ -1,13 +1,13 @@ netstandard2.1;net48 - 2.2.6.0 - 2.2.6.0 + 2.2.7.0 + 2.2.7.0 Bynder Bynder.Sdk Copyright © Bynder true - 2.2.6 + 2.2.7 BynderDevops The main goal of this SDK is to speed up the integration of Bynder customers who use C# making it easier to connect to the Bynder API (http://docs.bynder.apiary.io/) and executing requests on it. https://bynder.com/static/3.0/img/favicon-black.ico @@ -16,7 +16,7 @@ true BynderDevops https://github.com/Bynder/bynder-c-sharp-sdk - Added .NET framework as a separate target. + Resolved an issue regarding the construction of API urls. The main goal of this SDK is to speed up the integration of Bynder customers who use C# making it easier to connect to the Bynder API (http://docs.bynder.apiary.io/) and executing requests on it. Bynder API C# SDK Bynder.Sdk diff --git a/Bynder/Sdk/Extensions/UriBuilderExtensions.cs b/Bynder/Sdk/Extensions/UriBuilderExtensions.cs new file mode 100644 index 0000000..41145ce --- /dev/null +++ b/Bynder/Sdk/Extensions/UriBuilderExtensions.cs @@ -0,0 +1,28 @@ +using System; + +namespace Bynder.Sdk.Extensions +{ + public static class UriBuilderExtensions + { + /// + /// Allows to append a path to a url without overriding the existing path + /// (which happens when you would set the Path property). + /// + /// For example, UriBuilder("https://example.com/base") { Path = "path/to/something" } + /// will result in "https://example.com/path/to/something" (removing the "base" part) + /// + /// While UriBuilder("https://example.com/base").AppendPath("path/to/something") + /// will result in "https://example.com/base/path/to/something" + /// + /// It will automatically ensure that the paths are combined correctly using only one "/". + /// + /// extended UriBuilder instance + /// path to be appended to the full url + /// UriBuilder instance with the appended path + public static UriBuilder AppendPath(this UriBuilder uriBuilder, String path) + { + uriBuilder.Path = $"{uriBuilder.Path.TrimEnd('/')}/{path.TrimStart('/')}"; + return uriBuilder; + } + } +} diff --git a/Bynder/Sdk/Service/OAuth/OAuthService.cs b/Bynder/Sdk/Service/OAuth/OAuthService.cs index 5eadb29..e01547c 100644 --- a/Bynder/Sdk/Service/OAuth/OAuthService.cs +++ b/Bynder/Sdk/Service/OAuth/OAuthService.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Bynder.Sdk.Api.Requests; using Bynder.Sdk.Api.RequestSender; +using Bynder.Sdk.Extensions; using Bynder.Sdk.Model; using Bynder.Sdk.Query; using Bynder.Sdk.Settings; @@ -48,7 +49,6 @@ public string GetAuthorisationUrl(string state, string scopes) return new UriBuilder(_configuration.BaseUrl) { - Path = AuthPath, Query = Utils.Url.ConvertToQuery( new Dictionary { @@ -59,7 +59,7 @@ public string GetAuthorisationUrl(string state, string scopes) { "state", state } } ) - }.ToString(); + }.AppendPath(AuthPath).ToString(); } /// diff --git a/Bynder/Test/Extensions/UriBuilderExtensionsTest.cs b/Bynder/Test/Extensions/UriBuilderExtensionsTest.cs new file mode 100644 index 0000000..d0a6a45 --- /dev/null +++ b/Bynder/Test/Extensions/UriBuilderExtensionsTest.cs @@ -0,0 +1,20 @@ +using System; +using Bynder.Sdk.Extensions; +using Xunit; + +namespace Bynder.Test.Extensions +{ + public class UriBuilderExtensionsTest + { + [Fact] + public void PathsAreCorrectlyAppended() + { + var expectedUrl = "https://example.com:443/base/path/to/something"; + Assert.Equal(expectedUrl, new UriBuilder("https://example.com/base").AppendPath("path/to/something").ToString()); + Assert.Equal(expectedUrl, new UriBuilder("https://example.com/base").AppendPath("/path/to/something").ToString()); + Assert.Equal(expectedUrl, new UriBuilder("https://example.com/base/").AppendPath("path/to/something").ToString()); + Assert.Equal(expectedUrl, new UriBuilder("https://example.com/base/").AppendPath("/path/to/something").ToString()); + } + } + +}