Skip to content

Commit

Permalink
Pass-through query string without decoding/encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
markm77 committed Jan 10, 2023
1 parent b6f3e76 commit f10d0b4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Specialized;
using System.Web;
using System.Text;
using FinnovationLabs.OpenBanking.Library.Connector.Instrumentation;
using FinnovationLabs.OpenBanking.Library.Connector.Mapping;
using FinnovationLabs.OpenBanking.Library.Connector.Models.Persistent.AccountAndTransaction;
Expand Down Expand Up @@ -59,7 +59,27 @@ protected override Uri GetApiRequestUrl(

private string ConstructedQuery(string? queryString, TransactionsReadParams readParams)
{
NameValueCollection query = HttpUtility.ParseQueryString(queryString ?? new UriBuilder().Query);
var query = new NameValueCollection();

if (!string.IsNullOrEmpty(queryString))
{
IEnumerable<(string, string)> initialQueryStringParams =
queryString
.Substring(1) // remove '?'
.Split('&') // split on '&'
.Select(
s =>
{
string[] z = s.Split('=', 2);
return (z[0], z[1]);
});

foreach ((string key, string value) in initialQueryStringParams)
{
query[key] = value;
}
}

if (!string.IsNullOrEmpty(readParams.FromBookingDateTime))
{
query["fromBookingDateTime"] = readParams.FromBookingDateTime;
Expand All @@ -70,7 +90,35 @@ private string ConstructedQuery(string? queryString, TransactionsReadParams read
query["toBookingDateTime"] = readParams.ToBookingDateTime;
}

return query.ToString()!;
return ConvertToQueryString(query);
}

private string ConvertToQueryString(NameValueCollection nameValueCollection)
{
StringBuilder stringBuilder =
new StringBuilder()
.Append('?');
var nonEmptyQuery = false;
foreach (string key in nameValueCollection)
{
if (string.IsNullOrEmpty(key))
{
continue;
}

string value = nameValueCollection[key] ?? string.Empty;

stringBuilder
.Append(key)
.Append('=')
.Append(value)
.Append('&');
nonEmptyQuery = true;
}

return nonEmptyQuery
? stringBuilder.ToString(0, stringBuilder.Length - 1) // remove trailing '&'
: string.Empty;
}

protected override TransactionsResponse PublicGetResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public async Task<IActionResult> GetAsync(
throw new InvalidOperationException("Can't generate calling URL.");

// Support pass-through of all query parameters
string queryString = HttpContext.Request.QueryString.Value ?? string.Empty;
string? queryString = null;
if (!string.IsNullOrEmpty(HttpContext.Request.QueryString.Value))
{
queryString = HttpContext.Request.QueryString.Value;
}

// Operation
TransactionsResponse fluentResponse = await _requestBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ await requestBuilderNew
{
const int maxPages = 30;
var page = 0;
var queryString = "";
string? queryString = null;
do
{
TransactionsResponse transactionsResp =
Expand Down

0 comments on commit f10d0b4

Please sign in to comment.