diff --git a/MtgApiManager.Lib/Service/CardService.cs b/MtgApiManager.Lib/Service/CardService.cs index 6382166..529a6f0 100644 --- a/MtgApiManager.Lib/Service/CardService.cs +++ b/MtgApiManager.Lib/Service/CardService.cs @@ -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; @@ -21,11 +22,6 @@ namespace MtgApiManager.Lib.Service public class CardService : ServiceBase, IMtgQueryable { - /// - /// The list of queries to apply. - /// - private readonly NameValueCollection _whereQueries; - /// /// Initializes a new instance of the class. Defaults to version 1.0 of the API. /// @@ -52,7 +48,6 @@ public CardService() public CardService(IMtgApiServiceAdapter serviceAdapter, ApiVersion version, bool rateLimitOn = true) : base(serviceAdapter, version, ApiEndPoint.Cards, rateLimitOn) { - _whereQueries = new NameValueCollection(); } /// @@ -85,7 +80,7 @@ public override Exceptional> All() { try { - var query = BuildUri(_whereQueries); + var query = BuildUri(WhereQueries); var rootCardList = CallWebServiceGet(query).Result; return Exceptional>.Success(MapCardsList(rootCardList), MtgApiController.CreatePagingInfo()); @@ -104,7 +99,7 @@ public async override Task>> AllAsync() { try { - var query = BuildUri(_whereQueries); + var query = BuildUri(WhereQueries); var rootCardList = await CallWebServiceGet(query).ConfigureAwait(false); return Exceptional>.Success(MapCardsList(rootCardList), MtgApiController.CreatePagingInfo()); @@ -295,14 +290,14 @@ public CardService Where(Expression> property, U Type valueType = value.GetType(); if (valueType.IsArray) { - _whereQueries[queryName] = string.Join("|", (IEnumerable)value); + WhereQueries[queryName] = string.Join("|", (IEnumerable)value); } else { - _whereQueries[queryName] = Convert.ToString(value); + WhereQueries[queryName] = Uri.UnescapeDataString(Convert.ToString(value)); } return this; } } -} +} \ No newline at end of file diff --git a/MtgApiManager.Lib/Service/ServiceBase.cs b/MtgApiManager.Lib/Service/ServiceBase.cs index db2d78b..b874011 100644 --- a/MtgApiManager.Lib/Service/ServiceBase.cs +++ b/MtgApiManager.Lib/Service/ServiceBase.cs @@ -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; @@ -41,6 +42,7 @@ public ServiceBase(IMtgApiServiceAdapter serviceAdapter, ApiVersion version, Api Version = version; EndPoint = endpoint; _isRateLimitOn = rateLimitOn; + WhereQueries = new Dictionary(); } /// @@ -58,6 +60,11 @@ public ServiceBase(IMtgApiServiceAdapter serviceAdapter, ApiVersion version, Api /// protected ApiVersion Version { get; } + /// + /// Gets the list of where queries. + /// + protected Dictionary WhereQueries { get; } + /// /// Gets all the defined by the query parameters. /// @@ -75,7 +82,7 @@ public ServiceBase(IMtgApiServiceAdapter serviceAdapter, ApiVersion version, Api /// /// The list of parameters to add to the query. /// The URL to make the call with. - protected Uri BuildUri(NameValueCollection parameters) + protected Uri BuildUri(Dictionary parameters) { if (parameters == null) { @@ -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; } diff --git a/MtgApiManager.Lib/Service/SetService.cs b/MtgApiManager.Lib/Service/SetService.cs index 5a2745a..c1911b4 100644 --- a/MtgApiManager.Lib/Service/SetService.cs +++ b/MtgApiManager.Lib/Service/SetService.cs @@ -23,11 +23,6 @@ namespace MtgApiManager.Lib.Service public class SetService : ServiceBase, IMtgQueryable { - /// - /// The list of queries to apply. - /// - private NameValueCollection _whereQueries = null; - /// /// Initializes a new instance of the class. Defaults to version 1.0 of the API. /// @@ -54,7 +49,6 @@ public SetService() public SetService(IMtgApiServiceAdapter serviceAdapter, ApiVersion version, bool rateLimitOn = true) : base(serviceAdapter, version, ApiEndPoint.Sets, rateLimitOn) { - _whereQueries = new NameValueCollection(); } /// @@ -87,7 +81,7 @@ public override Exceptional> All() { try { - var query = BuildUri(_whereQueries); + var query = BuildUri(WhereQueries); var rootSetList = CallWebServiceGet(query).Result; return Exceptional>.Success(MapSetsList(rootSetList), MtgApiController.CreatePagingInfo()); @@ -106,7 +100,7 @@ public async override Task>> AllAsync() { try { - var query = BuildUri(_whereQueries); + var query = BuildUri(WhereQueries); var rootSetList = await CallWebServiceGet(query).ConfigureAwait(false); return Exceptional>.Success(MapSetsList(rootSetList), MtgApiController.CreatePagingInfo()); @@ -218,7 +212,7 @@ public SetService Where(Expression> property, U va MemberExpression expression = property.Body as MemberExpression; var queryName = QueryUtility.GetQueryPropertyName(expression.Member.Name); - _whereQueries[queryName] = Convert.ToString(value); + WhereQueries[queryName] = Convert.ToString(value); return this; }