Skip to content

Commit

Permalink
Fix url path appending (#61)
Browse files Browse the repository at this point in the history
* Introduced UriBuilder.AppendPath extension to be used instead of UriBuilder.Path

* Updated version
  • Loading branch information
Huib Piguillet authored Oct 20, 2021
1 parent d2a3e61 commit 7810cb0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
6 changes: 2 additions & 4 deletions Bynder/Sdk/Api/RequestSender/ApiRequestSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,10 +133,7 @@ private static class HttpRequestMessageFactory
internal static HttpRequestMessage Create(
string baseUrl, HttpMethod method, IDictionary<string, string> requestParams, string urlPath)
{
var builder = new UriBuilder(baseUrl)
{
Path = urlPath
};
var builder = new UriBuilder(baseUrl).AppendPath(urlPath);

if (HttpMethod.Get == method)
{
Expand Down
8 changes: 4 additions & 4 deletions Bynder/Sdk/Bynder.Sdk.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.1;net48</TargetFrameworks>
<AssemblyVersion>2.2.6.0</AssemblyVersion>
<FileVersion>2.2.6.0</FileVersion>
<AssemblyVersion>2.2.7.0</AssemblyVersion>
<FileVersion>2.2.7.0</FileVersion>
<Company>Bynder</Company>
<Product>Bynder.Sdk</Product>
<Copyright>Copyright © Bynder</Copyright>
<PackOnBuild>true</PackOnBuild>
<PackageVersion>2.2.6</PackageVersion>
<PackageVersion>2.2.7</PackageVersion>
<Authors>BynderDevops</Authors>
<Description>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.</Description>
<PackageIconUrl>https://bynder.com/static/3.0/img/favicon-black.ico</PackageIconUrl>
Expand All @@ -16,7 +16,7 @@
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Owners>BynderDevops</Owners>
<PackageProjectUrl>https://github.com/Bynder/bynder-c-sharp-sdk</PackageProjectUrl>
<PackageReleaseNotes>Added .NET framework as a separate target.</PackageReleaseNotes>
<PackageReleaseNotes>Resolved an issue regarding the construction of API urls.</PackageReleaseNotes>
<Summary>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.</Summary>
<PackageTags>Bynder API C# SDK</PackageTags>
<Title>Bynder.Sdk</Title>
Expand Down
28 changes: 28 additions & 0 deletions Bynder/Sdk/Extensions/UriBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace Bynder.Sdk.Extensions
{
public static class UriBuilderExtensions
{
/// <summary>
/// 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 "/".
/// </summary>
/// <param name="uriBuilder">extended UriBuilder instance</param>
/// <param name="path">path to be appended to the full url</param>
/// <returns>UriBuilder instance with the appended path</returns>
public static UriBuilder AppendPath(this UriBuilder uriBuilder, String path)
{
uriBuilder.Path = $"{uriBuilder.Path.TrimEnd('/')}/{path.TrimStart('/')}";
return uriBuilder;
}
}
}
4 changes: 2 additions & 2 deletions Bynder/Sdk/Service/OAuth/OAuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,7 +49,6 @@ public string GetAuthorisationUrl(string state, string scopes)

return new UriBuilder(_configuration.BaseUrl)
{
Path = AuthPath,
Query = Utils.Url.ConvertToQuery(
new Dictionary<string, string>
{
Expand All @@ -59,7 +59,7 @@ public string GetAuthorisationUrl(string state, string scopes)
{ "state", state }
}
)
}.ToString();
}.AppendPath(AuthPath).ToString();
}

/// <summary>
Expand Down
20 changes: 20 additions & 0 deletions Bynder/Test/Extensions/UriBuilderExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}

}

0 comments on commit 7810cb0

Please sign in to comment.