Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Previous Link #4680

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
27 changes: 25 additions & 2 deletions src/Microsoft.Health.Fhir.Api/Features/Routing/UrlResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
Request.Host.Value);
}

public Uri ResolveRouteUrl(IReadOnlyCollection<Tuple<string, string>> unsupportedSearchParams = null, IReadOnlyList<(SearchParameterInfo searchParameterInfo, SortOrder sortOrder)> resultSortOrder = null, string continuationToken = null, bool removeTotalParameter = false)
public Uri ResolveRouteUrl(IReadOnlyCollection<Tuple<string, string>> unsupportedSearchParams = null, IReadOnlyList<(SearchParameterInfo searchParameterInfo, SortOrder sortOrder)> resultSortOrder = null, string continuationToken = null, bool removeTotalParameter = false, bool addPreviousLink = true)
{
string routeName = _fhirRequestContextAccessor.RequestContext.RouteName;

Expand Down Expand Up @@ -212,7 +212,15 @@
// Update continuationToken if new value is provided.
if (continuationToken != null)
{
routeValues[KnownQueryParameterNames.ContinuationToken] = continuationToken;
if (Request.Headers.TryGetValue(KnownHeaders.AddPreviousLink, out var addPreviousLinkHeader) && addPreviousLinkHeader == "true" && addPreviousLink)
{
var continuationTokenWithPreviousLink = continuationToken + "||" + routeValues[KnownQueryParameterNames.ContinuationToken];
routeValues[KnownQueryParameterNames.ContinuationToken] = continuationTokenWithPreviousLink;
}
else
{
routeValues[KnownQueryParameterNames.ContinuationToken] = continuationToken;
}
}

return GetRouteUri(
Expand All @@ -223,6 +231,21 @@
Request.Host.Value);
}

public Uri ResolvePreviousRouteUrl()
{
if (Request.Headers.TryGetValue(KnownHeaders.AddPreviousLink, out var addPreviousLinkHeader) && addPreviousLinkHeader == "true")
{
if (Request.Query != null && Request.Query.TryGetValue(KnownQueryParameterNames.ContinuationToken, out var continuationToken))
{
// Removes the most recent continuation token segment and returns the previous continuation token.
var previousContinuationToken = continuationToken[0].Split("||", 2)[1];
return ResolveRouteUrl(null, null, previousContinuationToken, false, false);
}
}
LTA-Thinking marked this conversation as resolved.
Show resolved Hide resolved

return null;
}

public Uri ResolveRouteNameUrl(string routeName, IDictionary<string, object> routeValues)
{
var routeValueDictionary = new RouteValueDictionary(routeValues);
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.Health.Fhir.Core/Features/KnownHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static class KnownHeaders
public const string CustomAuditHeaderPrefix = "X-MS-AZUREFHIR-AUDIT-";
public const string FhirUserHeader = "x-ms-fhiruser";
public const string QueryLatencyOverEfficiency = "x-ms-query-latency-over-efficiency";
public const string AddPreviousLink = "x-ms-add-previous-link";

// #conditionalQueryParallelism - Header used to activate parallel conditional-query processing.
public const string ConditionalQueryProcessingLogic = "x-conditionalquery-processing-logic";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ public interface IUrlResolver
/// <param name="resultSortOrder">The order the results are sorted in</param>
/// <param name="continuationToken">The continuation token.</param>
/// <param name="removeTotalParameter">True if the _total parameter should be removed from the url, false otherwise.</param>
/// <param name="addPreviousLink">Whether to add the previous link</param>
/// <returns>The URL.</returns>
Uri ResolveRouteUrl(IReadOnlyCollection<Tuple<string, string>> unsupportedSearchParams = null, IReadOnlyList<(SearchParameterInfo searchParameterInfo, SortOrder sortOrder)> resultSortOrder = null, string continuationToken = null, bool removeTotalParameter = false);
Uri ResolveRouteUrl(IReadOnlyCollection<Tuple<string, string>> unsupportedSearchParams = null, IReadOnlyList<(SearchParameterInfo searchParameterInfo, SortOrder sortOrder)> resultSortOrder = null, string continuationToken = null, bool removeTotalParameter = false, bool addPreviousLink = true);

Uri ResolvePreviousRouteUrl();

/// <summary>
/// Resolves the URL for the specified routeName.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public async Task<SearchResult> SearchAsync(
{
EverythingOperationContinuationToken token = string.IsNullOrEmpty(continuationToken)
? new EverythingOperationContinuationToken()
: EverythingOperationContinuationToken.FromJson(ContinuationTokenConverter.Decode(continuationToken));
: EverythingOperationContinuationToken.FromJson(ContinuationTokenConverter.Decode(continuationToken.Split("||", 2)[0]));

if (token == null || token.Phase < 0 || token.Phase > 3)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ private void CreateLinks(SearchResult result, Bundle bundle)
result.UnsupportedSearchParameters,
result.SortOrder,
ContinuationTokenConverter.Encode(result.ContinuationToken),
true,
true);
}
catch (UriFormatException)
Expand All @@ -168,6 +169,13 @@ private void CreateLinks(SearchResult result, Bundle bundle)
{
// Add the self link to indicate which search parameters were used.
bundle.SelfLink = _urlResolver.ResolveRouteUrl(result.UnsupportedSearchParameters, result.SortOrder);

// Add flag check
var previous = _urlResolver.ResolvePreviousRouteUrl();
if (previous != null)
{
bundle.PreviousLink = previous;
}
}
catch (UriFormatException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public SearchOptions Create(
string.Format(Core.Resources.MultipleQueryParametersNotAllowed, KnownQueryParameterNames.ContinuationToken));
}

continuationToken = ContinuationTokenConverter.Decode(query.Item2);
var fullContinuationToken = query.Item2;
continuationToken = ContinuationTokenConverter.Decode(fullContinuationToken.Split("||", 2)[0]);
setDefaultBundleTotal = false;
}
else if (string.Equals(query.Item1, KnownQueryParameterNames.FeedRange, StringComparison.OrdinalIgnoreCase))
Expand Down
Loading