Skip to content

Commit

Permalink
Updated the way the query string was encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
jregnier committed Oct 1, 2019
1 parent 357a855 commit eb2800f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
17 changes: 6 additions & 11 deletions MtgApiManager.Lib/Service/CardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace MtgApiManager.Lib.Service
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Web;
using Dto;
using Model;
using MtgApiManager.Lib.Core;
Expand All @@ -21,11 +22,6 @@ namespace MtgApiManager.Lib.Service
public class CardService
: ServiceBase<CardService, Card>, IMtgQueryable<CardService, CardQueryParameter>
{
/// <summary>
/// The list of queries to apply.
/// </summary>
private readonly NameValueCollection _whereQueries;

/// <summary>
/// Initializes a new instance of the <see cref="CardService"/> class. Defaults to version 1.0 of the API.
/// </summary>
Expand All @@ -52,7 +48,6 @@ public CardService()
public CardService(IMtgApiServiceAdapter serviceAdapter, ApiVersion version, bool rateLimitOn = true)
: base(serviceAdapter, version, ApiEndPoint.Cards, rateLimitOn)
{
_whereQueries = new NameValueCollection();
}

/// <summary>
Expand Down Expand Up @@ -85,7 +80,7 @@ public override Exceptional<List<Card>> All()
{
try
{
var query = BuildUri(_whereQueries);
var query = BuildUri(WhereQueries);
var rootCardList = CallWebServiceGet<RootCardListDto>(query).Result;

return Exceptional<List<Card>>.Success(MapCardsList(rootCardList), MtgApiController.CreatePagingInfo());
Expand All @@ -104,7 +99,7 @@ public async override Task<Exceptional<List<Card>>> AllAsync()
{
try
{
var query = BuildUri(_whereQueries);
var query = BuildUri(WhereQueries);
var rootCardList = await CallWebServiceGet<RootCardListDto>(query).ConfigureAwait(false);

return Exceptional<List<Card>>.Success(MapCardsList(rootCardList), MtgApiController.CreatePagingInfo());
Expand Down Expand Up @@ -295,14 +290,14 @@ public CardService Where<U>(Expression<Func<CardQueryParameter, U>> property, U
Type valueType = value.GetType();
if (valueType.IsArray)
{
_whereQueries[queryName] = string.Join("|", (IEnumerable<object>)value);
WhereQueries[queryName] = string.Join("|", (IEnumerable<object>)value);
}
else
{
_whereQueries[queryName] = Convert.ToString(value);
WhereQueries[queryName] = Uri.UnescapeDataString(Convert.ToString(value));
}

return this;
}
}
}
}
17 changes: 13 additions & 4 deletions MtgApiManager.Lib/Service/ServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace MtgApiManager.Lib.Service
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Core;
Expand Down Expand Up @@ -41,6 +42,7 @@ public ServiceBase(IMtgApiServiceAdapter serviceAdapter, ApiVersion version, Api
Version = version;
EndPoint = endpoint;
_isRateLimitOn = rateLimitOn;
WhereQueries = new Dictionary<string, string>();
}

/// <summary>
Expand All @@ -58,6 +60,11 @@ public ServiceBase(IMtgApiServiceAdapter serviceAdapter, ApiVersion version, Api
/// </summary>
protected ApiVersion Version { get; }

/// <summary>
/// Gets the list of where queries.
/// </summary>
protected Dictionary<string, string> WhereQueries { get; }

/// <summary>
/// Gets all the <see cref="TModel"/> defined by the query parameters.
/// </summary>
Expand All @@ -75,7 +82,7 @@ public ServiceBase(IMtgApiServiceAdapter serviceAdapter, ApiVersion version, Api
/// </summary>
/// <param name="parameters">The list of parameters to add to the query.</param>
/// <returns>The URL to make the call with.</returns>
protected Uri BuildUri(NameValueCollection parameters)
protected Uri BuildUri(Dictionary<string, string> parameters)
{
if (parameters == null)
{
Expand All @@ -87,9 +94,11 @@ protected Uri BuildUri(NameValueCollection parameters)
new Uri(BaseMtgUrl),
string.Concat(Version.GetDescription(), "/", EndPoint.GetDescription())));

var query = HttpUtility.ParseQueryString(urlBuilder.Query);
query.Add(parameters);
urlBuilder.Query = query.ToString();
var paramList = parameters
.Select(p => $"{p.Key}={p.Value}")
.ToList();

urlBuilder.Query = Uri.EscapeUriString(string.Join("&", paramList));

return urlBuilder.Uri;
}
Expand Down
12 changes: 3 additions & 9 deletions MtgApiManager.Lib/Service/SetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ namespace MtgApiManager.Lib.Service
public class SetService
: ServiceBase<SetService, Set>, IMtgQueryable<SetService, SetQueryParameter>
{
/// <summary>
/// The list of queries to apply.
/// </summary>
private NameValueCollection _whereQueries = null;

/// <summary>
/// Initializes a new instance of the <see cref="SetService"/> class. Defaults to version 1.0 of the API.
/// </summary>
Expand All @@ -54,7 +49,6 @@ public SetService()
public SetService(IMtgApiServiceAdapter serviceAdapter, ApiVersion version, bool rateLimitOn = true)
: base(serviceAdapter, version, ApiEndPoint.Sets, rateLimitOn)
{
_whereQueries = new NameValueCollection();
}

/// <summary>
Expand Down Expand Up @@ -87,7 +81,7 @@ public override Exceptional<List<Set>> All()
{
try
{
var query = BuildUri(_whereQueries);
var query = BuildUri(WhereQueries);
var rootSetList = CallWebServiceGet<RootSetListDto>(query).Result;

return Exceptional<List<Set>>.Success(MapSetsList(rootSetList), MtgApiController.CreatePagingInfo());
Expand All @@ -106,7 +100,7 @@ public async override Task<Exceptional<List<Set>>> AllAsync()
{
try
{
var query = BuildUri(_whereQueries);
var query = BuildUri(WhereQueries);
var rootSetList = await CallWebServiceGet<RootSetListDto>(query).ConfigureAwait(false);

return Exceptional<List<Set>>.Success(MapSetsList(rootSetList), MtgApiController.CreatePagingInfo());
Expand Down Expand Up @@ -218,7 +212,7 @@ public SetService Where<U>(Expression<Func<SetQueryParameter, U>> property, U va

MemberExpression expression = property.Body as MemberExpression;
var queryName = QueryUtility.GetQueryPropertyName<SetQueryParameter>(expression.Member.Name);
_whereQueries[queryName] = Convert.ToString(value);
WhereQueries[queryName] = Convert.ToString(value);

return this;
}
Expand Down

0 comments on commit eb2800f

Please sign in to comment.