Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### New in 1.0.3 (Released _)
* Allow relative link uri

### New in 1.0 (Released 05/09/2018)
* Upgrade packages to netstandard

Expand Down
2 changes: 1 addition & 1 deletion Version.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>1.0.2</VersionPrefix>
<VersionPrefix>1.0.3</VersionPrefix>
<VersionSuffix>
</VersionSuffix>
</PropertyGroup>
Expand Down
7 changes: 6 additions & 1 deletion src/HalKit/HalClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,13 @@ private async Task<IApiResponse<T>> SendRequestAsync<T>(
headers.Add("Accept", new[] { HalJsonMediaType });
}


var uri = Configuration.AllowRelativeLinks
? _linkResolver.ResolveLink(Configuration.RootEndpoint, link, parameters)
: _linkResolver.ResolveLink(link, parameters);

return await HttpConnection.SendRequestAsync<T>(
_linkResolver.ResolveLink(link, parameters),
uri,
method,
body,
headers,
Expand Down
2 changes: 2 additions & 0 deletions src/HalKit/HalKitConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public HalKitConfiguration(Uri rootEndpoint)
public Uri RootEndpoint { get; set; }

public bool CaptureSynchronizationContext { get; set; }

public bool AllowRelativeLinks { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/HalKit/IHalKitConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ public interface IHalKitConfiguration
/// </summary>
/// <remarks>See http://blog.stephencleary.com/2012/02/async-and-await.html#avoiding-context.</remarks>
bool CaptureSynchronizationContext { get; set; }

/// <summary>
/// Set whether relative links should be constructed from RootEndpoint
/// </summary>
bool AllowRelativeLinks { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/HalKit/Services/ILinkResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,14 @@ public interface ILinkResolver
/// Otherwise, the parameters are added to the href as query parameters.
/// </summary>
Uri ResolveLink(Link link, IDictionary<string, string> parameters);

/// <summary>
/// Resolves the href of the given <see cref="Link"/> with the given parameters to
/// get a <see cref="Uri"/>. If <paramref name="link"/> is templated then the parameters
/// are applied in accordance with Uri Template Spec RFC6570 (http://tools.ietf.org/html/rfc6570).
/// Otherwise, the parameters are added to the href as query parameters.
/// If the link is a relative Uri, it is then merged with the given rootEndpoint.
/// </summary>
Uri ResolveLink(Uri rootEndpoint, Link link, IDictionary<string, string> parameters);
}
}
19 changes: 16 additions & 3 deletions src/HalKit/Services/LinkResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ namespace HalKit.Services
{
public class LinkResolver : ILinkResolver
{
public Uri ResolveLink(Uri rootEndpoint, Link link, IDictionary<string, string> parameters)
{
Requires.ArgumentNotNull(link, nameof(link));

if (!link.HRef.Contains(rootEndpoint.AbsoluteUri))
{
// Make new href from relative path (using absolute path as base)
link.HRef = rootEndpoint + link.HRef.Replace(rootEndpoint.AbsolutePath, "");
}

return ResolveLink(link, parameters);
}

public Uri ResolveLink(Link link, IDictionary<string, string> parameters)
{
Requires.ArgumentNotNull(link, nameof(link));
Expand All @@ -20,7 +33,7 @@ public Uri ResolveLink(Link link, IDictionary<string, string> parameters)

if (!link.IsTemplated)
{
return AppendParametersAsQueryParams(link, parameters);
return AppendParametersAsQueryParams(link.HRef, parameters);
}

var templateParameters = parameters.ToDictionary(kv => kv.Key, kv => (object) kv.Value);
Expand All @@ -29,10 +42,10 @@ public Uri ResolveLink(Link link, IDictionary<string, string> parameters)
}

private Uri AppendParametersAsQueryParams(
Link link,
string href,
IEnumerable<KeyValuePair<string, string>> parameters)
{
var uriBuilder = new UriBuilder(link.HRef);
var uriBuilder = new UriBuilder(href);
var existingQueryParameters = uriBuilder.Query.Replace("?", "");
if (!string.IsNullOrEmpty(existingQueryParameters) && !existingQueryParameters.EndsWith("&"))
{
Expand Down