Skip to content

Commit

Permalink
Merge pull request #56 from aspriddell/expose-querygenerator
Browse files Browse the repository at this point in the history
add additionalqueries property and tests
  • Loading branch information
aspriddell authored Dec 24, 2020
2 parents 47d0ed4 + 0ec5488 commit d48256a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
19 changes: 17 additions & 2 deletions DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// DragonFruit.Common Copyright 2020 DragonFruit Network
// Licensed under the MIT License. Please refer to the LICENSE file at the root of this project for details

using System.Collections.Generic;
using System.Linq;
using DragonFruit.Common.Data.Parameters;
using DragonFruit.Common.Data.Utils;
Expand All @@ -11,7 +12,7 @@ namespace DragonFruit.Common.Data.Tests
[TestFixture]
public class RequestDataCompilationTests
{
[TestCase]
[Test]
public void TestQueries()
{
var query = new TestRequest().FullUrl.Split('?').Last().Split('&');
Expand All @@ -27,7 +28,7 @@ public void TestQueries()
Assert.IsTrue(query.Contains($"{TestRequest.QueryName}={string.Join(":", TestRequest.TestDataset)}"));
}

[TestCase]
[Test]
public void TestEnumHandling()
{
var request = new TestRequest();
Expand All @@ -37,6 +38,15 @@ public void TestEnumHandling()
Assert.IsTrue(query.Contains($"enum={nameof(EnumValues.Blue).ToLower(CultureUtils.DefaultCulture)}"));
Assert.IsTrue(query.Contains($"enum={(int)EnumValues.Green}"));
}

[Test]
public void TestAdditionalQueryHandling()
{
var request = new TestRequest();
var query = request.FullUrl.Split('?').Last().Split('&');

Assert.IsTrue(query.Contains("a=x"));
}
}

internal class TestRequest : ApiRequest
Expand All @@ -46,6 +56,11 @@ internal class TestRequest : ApiRequest

public override string Path => "http://example.com";

protected override IEnumerable<KeyValuePair<string, string>> AdditionalQueries => new[]
{
new KeyValuePair<string, string>("a", "x")
};

[QueryParameter(QueryName, CollectionConversionMode.Recursive)]
public string[] RecursiveData { get; set; } = TestDataset;

Expand Down
26 changes: 21 additions & 5 deletions DragonFruit.Common.Data/ApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using DragonFruit.Common.Data.Parameters;
Expand Down Expand Up @@ -34,11 +35,8 @@ public abstract class ApiRequest
protected virtual BodyType BodyType { get; }

/// <summary>
/// Whether an auth header is required.
/// Whether an auth header is required. Throws on check failure (before sending request)
/// </summary>
/// <exception cref="ClientValidationException">This was set to true but no auth header was specified.
/// Automatically suppressed if the <see cref="Headers"/> property has been initialised.
/// </exception>
protected internal virtual bool RequireAuth => false;

/// <summary>
Expand Down Expand Up @@ -81,7 +79,25 @@ public abstract class ApiRequest
/// <summary>
/// Query string generated from all filled <see cref="QueryParameter"/>-attributed properties
/// </summary>
internal string QueryString => QueryUtils.QueryStringFrom(ParameterUtils.GetParameter<QueryParameter>(this, RequestCulture));
internal string QueryString
{
get
{
var queries = ParameterUtils.GetParameter<QueryParameter>(this, RequestCulture);

if (AdditionalQueries != null)
{
queries = queries.Concat(AdditionalQueries);
}

return QueryUtils.QueryStringFrom(queries);
}
}

/// <summary>
/// Additional abstract collection of queries provided as an <see cref="IEnumerable{T}"/> of <see cref="KeyValuePair{TKey,TValue}"/>
/// </summary>
protected virtual IEnumerable<KeyValuePair<string, string>> AdditionalQueries { get; }

/// <summary>
/// Create a <see cref="HttpResponseMessage"/> for this <see cref="ApiRequest"/>, which can then be modified manually or overriden by <see cref="ApiClient.SetupRequest"/>
Expand Down

0 comments on commit d48256a

Please sign in to comment.