diff --git a/README.md b/README.md index 38b944f..5fa6ea6 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,14 @@ PM> Install-Package MtgApiManager.Lib ## Rate Limit The MTG API has a [rate limit](https://docs.magicthegathering.io/#documentationrate_limits) in order to maintain a reponsive experience for the user. This SDK has a rate limit manager which manages the rates for you. Based on the total requests per hour the rate limit manager attempts to spead out the calls over the hour in 10 second chuncks. For example if the number of requests is limited to 5000 per hour, the number of requests per 10 seconds is 13 (5000 / (3600 / 10)) after rounding down, therefore if you make 13 requests out to the web service in 8 seconds when you attempt to make the 14th request the SDK will wait 2 seconds before trying to call the API. ## Usage +Creating a new service provider used to fetch the different services available. +```cs +IMtgServiceProvider serviceProvider = new MtgServiceProvider(); +``` The result of all service calls resturns a generic **Exception Monad** containing the results of the call. ```cs -CardService service = new CardService(); -Exceptional> result = service.All(); +CardService service = serviceProvider.GetCardService(); +Exceptional> result = service.AllAsync(); if (result.IsSuccess) { var value = result.Value; @@ -30,84 +34,67 @@ else var exception = result.Exception; } ``` -Each service call also has an equivalent asynchronous call. #### Find a card by id ```cs -CardService service = new CardService(); -var result = service.Find("f2eb06047a3a8e515bff62b55f29468fcde6332a"); -var asyncResult = await service.FindAsync("f2eb06047a3a8e515bff62b55f29468fcde6332a"); +CardService service = serviceProvider.GetCardService(); +var result = await service.FindAsync("f2eb06047a3a8e515bff62b55f29468fcde6332a"); ``` #### Find a card by multiverse id ```cs -CardService service = new CardService(); -var result = service.Find(123); -var asyncResult = await service.FindAsync(123); +CardService service = serviceProvider.GetCardService(); +var result = await service.FindAsync(123); ``` #### Filter Cards via query parameters ```cs -CardService service = new CardService(); -var result = service.Where(x => x.Set, "ktk") - .Where(x => x.SubTypes, "warrior,human") - .All() -var asyncResult = await service.Where(x => x.Set, "ktk") - .Where(x => x.SubTypes, "warrior,human") - .AllAsync() +CardService service = serviceProvider.GetCardService(); +var result = await service.Where(x => x.Set, "ktk") + .Where(x => x.SubTypes, "warrior,human") + .AllAsync() ``` #### Find all cards (limited by default page size) ```cs -CardService service = new CardService(); -var result = service.All() -var asyncResult = await service.AllAsync() +CardService service = serviceProvider.GetCardService(); +var result = await service.AllAsync() ``` #### Get all cards with pagination ```cs -CardService service = new CardService(); -var result = service.Where(x => x.Page, 5) - .Where(x => x.PageSize, 250) - .All() -var asyncResult = await service.Where(x => x.Page, 5) - .Where(x => x.PageSize, 250) - .AllAsync() +CardService service = serviceProvider.GetCardService(); +var result = await service.Where(x => x.Page, 5) + .Where(x => x.PageSize, 250) + .AllAsync() ``` #### Get all card types ```cs -CardService service = new CardService(); -var result = service.GetCardTypes(); -var asyncResult = await service.GetCardTypesAsync(); +CardService service = serviceProvider.GetCardService(); +var result = await service.GetCardTypesAsync(); ``` #### Get all card supertypes ```cs -CardService service = new CardService(); -var result = service.GetCardSuperTypes(); -var asyncResult = await service.GetCardSuperTypesAsync(); +CardService service = serviceProvider.GetCardService(); +var result = await service.GetCardSuperTypesAsync(); ``` #### Get all card subtypes ```cs -CardService service = new CardService(); -var result = service.GetCardSubTypes(); -var asyncResult = await service.GetCardSubTypesAsync(); +CardService service = serviceProvider.GetCardService(); +var result = await service.GetCardSubTypesAsync(); ``` #### Find a set by code ```cs -SetService service = new SetService(); -var result = service.Find("ktk"); -var asyncResult = await service.FindAsync("ktk"); +SetService service = serviceProvider.GetSetService(); +var result = await service.FindAsync("ktk"); ``` #### Filter sets via query parameters ```cs -SetService service = new SetService(); -var result = service.Where(x => x.Name, "khans").All() -var asyncResult = await service.Where(x => x.Name, "khans").AllAsync() +SetService service = serviceProvider.GetSetService(); +var result = await service.Where(x => x.Name, "khans").AllAsync() ``` #### Get all Sets ```cs -SetService service = new SetService(); -var result = service.All() -var asyncResult = await service.AllAsync() +SetService service = serviceProvider.GetSetService(); +var result = await service.AllAsync() ``` #### Generate booster ```cs -SetService service = new SetService(); -var result = service.GenerateBooster("ktk") -var asyncResult = await service.GenerateBoosterAsync("ktk") +SetService service = serviceProvider.GetSetService(); +var result = await service.GenerateBoosterAsync("ktk") ``` diff --git a/src/.editorconfig b/src/.editorconfig index 4628051..4c64564 100644 --- a/src/.editorconfig +++ b/src/.editorconfig @@ -1,7 +1,4 @@ [*.cs] -# CS1591: Missing XML comment for publicly visible type or member -dotnet_diagnostic.CS1591.severity = none - # S1075: URIs should not be hardcoded dotnet_diagnostic.S1075.severity = none diff --git a/src/MtgApiManager.Lib.Test/Service/CardServiceTest.cs b/src/MtgApiManager.Lib.Test/Service/CardServiceTest.cs index f705fcd..0a3b701 100644 --- a/src/MtgApiManager.Lib.Test/Service/CardServiceTest.cs +++ b/src/MtgApiManager.Lib.Test/Service/CardServiceTest.cs @@ -126,7 +126,8 @@ public async Task AllAsyncTest() .Throws() .ReturnsAsync(new RootCardListDto() { Cards = cards }); - var service = new CardService(moqAdapter.Object, new ModelMapper(), ApiVersion.V1_0, false); + var serviceProvider = new MtgServiceProvider(moqAdapter.Object, new ModelMapper(), false); + var service = serviceProvider.GetCardService(); service = service.Where(x => x.Name, "name1"); var result = await service.AllAsync(); @@ -307,149 +308,6 @@ public async Task FindAsyncTest() Assert.NotNull(result.Value); } - /// - /// Tests the method. - /// - [Fact] - public void FindTest() - { - var cardDto = new CardDto() - { - Artist = "artist1", - Border = "border1", - Cmc = 111, - Colors = new string[] { "blue", "pink" }, - Flavor = "flavor1", - ForeignNames = new ForeignNameDto[] { new ForeignNameDto() { Language = "english", MultiverseId = 222, Name = "name2" } }, - Hand = 222, - Id = "12345", - ImageUrl = "http://fake/url", - Layout = "layout1", - Legalities = new LegalityDto[] { new LegalityDto() { Format = "format2", Legality = "legality name 2" } }, - Life = 333, - Loyalty = "loyalty", - ManaCost = "500", - MultiverseId = 444, - Name = "name1", - Names = new string[] { "name2", "name3" }, - Number = "600", - OriginalText = "original text", - OriginalType = "original type", - Power = "9000", - Printings = new string[] { "printing1", "printing2" }, - Rarity = "rare", - ReleaseDate = "2010, 1, 1", - Reserved = true, - Rulings = new RulingDto[] { new RulingDto() { Date = "2010, 2, 2", Text = "text2" } }, - Set = "set1", - SetName = "set name 1", - Source = "source", - Starter = true, - SubTypes = new string[] { "subtype1", "subtype2" }, - SuperTypes = new string[] { "supertype1", "supertype2" }, - Text = "text3", - Timeshifted = false, - Toughness = "tough", - Type = "type2", - Types = new string[] { "type1", "type2" }, - Variations = new string[] { Guid.Empty.ToString() }, - Watermark = "watermark" - }; - - // Test the Find by multi verse identifier method. - var moqAdapter = new Mock(); - moqAdapter - .SetupSequence(x => x.WebGetAsync(It.IsAny())) - .Throws() - .Throws(new MtgApiException("bad request")) - .Throws(new MtgApiException("forbidden")) - .Throws(new MtgApiException("server error")) - .Throws(new MtgApiException("not found")) - .Throws(new MtgApiException("unavailable")) - .Throws() - .ReturnsAsync(new RootCardDto() { Card = cardDto }); - - var service = new CardService(moqAdapter.Object, new ModelMapper(), ApiVersion.V1_0, false); - - var result = service.Find(1); - Assert.False(result.IsSuccess); - Assert.Equal("Value cannot be null.", result.Exception.Message); - - result = service.Find(1); - Assert.False(result.IsSuccess); - Assert.Equal("bad request", result.Exception.Message); - - result = service.Find(1); - Assert.False(result.IsSuccess); - Assert.Equal("forbidden", result.Exception.Message); - - result = service.Find(1); - Assert.False(result.IsSuccess); - Assert.Equal("server error", result.Exception.Message); - - result = service.Find(1); - Assert.False(result.IsSuccess); - Assert.Equal("not found", result.Exception.Message); - - result = service.Find(1); - Assert.False(result.IsSuccess); - Assert.Equal("unavailable", result.Exception.Message); - - result = service.Find(1); - Assert.False(result.IsSuccess); - Assert.IsType(result.Exception); - - result = service.Find(1); - Assert.True(result.IsSuccess); - Assert.Null(result.Exception); - Assert.NotNull(result.Value); - - // Test the Find by identifier method. - moqAdapter - .SetupSequence(x => x.WebGetAsync(It.IsAny())) - .Throws() - .Throws(new MtgApiException("bad request")) - .Throws(new MtgApiException("forbidden")) - .Throws(new MtgApiException("server error")) - .Throws(new MtgApiException("not found")) - .Throws(new MtgApiException("unavailable")) - .Throws() - .ReturnsAsync(new RootCardDto() { Card = cardDto }); - - result = service.Find("123h4hfh4h6jgjk45jhbj"); - Assert.False(result.IsSuccess); - Assert.Equal("Value cannot be null.", result.Exception.Message); - - result = service.Find("123h4hfh4h6jgjk45jhbj"); - Assert.False(result.IsSuccess); - Assert.Equal("bad request", result.Exception.Message); - - result = service.Find("123h4hfh4h6jgjk45jhbj"); - Assert.False(result.IsSuccess); - Assert.Equal("forbidden", result.Exception.Message); - - result = service.Find("123h4hfh4h6jgjk45jhbj"); - Assert.False(result.IsSuccess); - Assert.Equal("server error", result.Exception.Message); - - result = service.Find("123h4hfh4h6jgjk45jhbj"); - Assert.False(result.IsSuccess); - Assert.Equal("not found", result.Exception.Message); - - result = service.Find("123h4hfh4h6jgjk45jhbj"); - Assert.False(result.IsSuccess); - Assert.Equal("unavailable", result.Exception.Message); - - result = service.Find("123h4hfh4h6jgjk45jhbj"); - Assert.False(result.IsSuccess); - Assert.IsType(result.Exception); - - result = service.Find("123h4hfh4h6jgjk45jhbj"); - Assert.True(result.IsSuccess); - Assert.Null(result.Exception); - Assert.NotNull(result.Value); - } - /// /// Tests the method. /// @@ -479,7 +337,8 @@ public async Task GetCardSubTypesAsyncTest() .Throws() .ReturnsAsync(new RootCardSubTypeDto() { SubTypes = cardSubTypes }); - var service = new CardService(moqAdapter.Object, new ModelMapper(), ApiVersion.V1_0, false); + var serviceProvider = new MtgServiceProvider(moqAdapter.Object, new ModelMapper(), false); + var service = serviceProvider.GetCardService(); var result = await service.GetCardSubTypesAsync(); Assert.False(result.IsSuccess); @@ -515,70 +374,6 @@ public async Task GetCardSubTypesAsyncTest() Assert.NotNull(result.Value); } - /// - /// Tests the method. - /// - [Fact] - public void GetCardSubTypesTest() - { - var cardSubTypes = new List - { - "type1", - "type2", - "type3", - "type4", - "type5", - }; - - // Test the All method. - var moqAdapter = new Mock(); - moqAdapter - .SetupSequence(x => x.WebGetAsync(new Uri("https://api.magicthegathering.io/v1/subtypes"))) - .Throws() - .Throws(new MtgApiException("bad request")) - .Throws(new MtgApiException("forbidden")) - .Throws(new MtgApiException("server error")) - .Throws(new MtgApiException("not found")) - .Throws(new MtgApiException("unavailable")) - .Throws() - .ReturnsAsync(new RootCardSubTypeDto() { SubTypes = cardSubTypes }); - - var service = new CardService(moqAdapter.Object, new ModelMapper(), ApiVersion.V1_0, false); - - var result = service.GetCardSubTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("Value cannot be null.", result.Exception.Message); - - result = service.GetCardSubTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("bad request", result.Exception.Message); - - result = service.GetCardSubTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("forbidden", result.Exception.Message); - - result = service.GetCardSubTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("server error", result.Exception.Message); - - result = service.GetCardSubTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("not found", result.Exception.Message); - - result = service.GetCardSubTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("unavailable", result.Exception.Message); - - result = service.GetCardSubTypes(); - Assert.False(result.IsSuccess); - Assert.IsType(result.Exception); - - result = service.GetCardSubTypes(); - Assert.True(result.IsSuccess); - Assert.Null(result.Exception); - Assert.NotNull(result.Value); - } - /// /// Tests the method. /// @@ -608,7 +403,8 @@ public async Task GetCardSuperTypesAsyncTest() .Throws() .ReturnsAsync(new RootCardSuperTypeDto() { SuperTypes = cardSuperTypes }); - var service = new CardService(moqAdapter.Object, new ModelMapper(), ApiVersion.V1_0, false); + var serviceProvider = new MtgServiceProvider(moqAdapter.Object, new ModelMapper(), false); + var service = serviceProvider.GetCardService(); var result = await service.GetCardSuperTypesAsync(); Assert.False(result.IsSuccess); @@ -644,70 +440,6 @@ public async Task GetCardSuperTypesAsyncTest() Assert.NotNull(result.Value); } - /// - /// Tests the method. - /// - [Fact] - public void GetCardSuperTypesTest() - { - var cardSuperTypes = new List - { - "type1", - "type2", - "type3", - "type4", - "type5", - }; - - // Test the All method. - var moqAdapter = new Mock(); - moqAdapter - .SetupSequence(x => x.WebGetAsync(new Uri("https://api.magicthegathering.io/v1/supertypes"))) - .Throws() - .Throws(new MtgApiException("bad request")) - .Throws(new MtgApiException("forbidden")) - .Throws(new MtgApiException("server error")) - .Throws(new MtgApiException("not found")) - .Throws(new MtgApiException("unavailable")) - .Throws() - .ReturnsAsync(new RootCardSuperTypeDto() { SuperTypes = cardSuperTypes }); - - var service = new CardService(moqAdapter.Object, new ModelMapper(), ApiVersion.V1_0, false); - - var result = service.GetCardSuperTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("Value cannot be null.", result.Exception.Message); - - result = service.GetCardSuperTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("bad request", result.Exception.Message); - - result = service.GetCardSuperTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("forbidden", result.Exception.Message); - - result = service.GetCardSuperTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("server error", result.Exception.Message); - - result = service.GetCardSuperTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("not found", result.Exception.Message); - - result = service.GetCardSuperTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("unavailable", result.Exception.Message); - - result = service.GetCardSuperTypes(); - Assert.False(result.IsSuccess); - Assert.IsType(result.Exception); - - result = service.GetCardSuperTypes(); - Assert.True(result.IsSuccess); - Assert.Null(result.Exception); - Assert.NotNull(result.Value); - } - /// /// Tests the method. /// @@ -737,7 +469,8 @@ public async Task GetCardTypesAsyncTest() .Throws() .ReturnsAsync(new RootCardTypeDto() { Types = cardTypes }); - var service = new CardService(moqAdapter.Object, new ModelMapper(), ApiVersion.V1_0, false); + var serviceProvider = new MtgServiceProvider(moqAdapter.Object, new ModelMapper(), false); + var service = serviceProvider.GetCardService(); var result = await service.GetCardTypesAsync(); Assert.False(result.IsSuccess); @@ -773,77 +506,14 @@ public async Task GetCardTypesAsyncTest() Assert.NotNull(result.Value); } - /// - /// Tests the method. - /// - [Fact] - public void GetCardTypesTest() - { - var cardTypes = new List - { - "type1", - "type2", - "type3", - "type4", - "type5", - }; - - // Test the All method. - var moqAdapter = new Mock(); - moqAdapter - .SetupSequence(x => x.WebGetAsync(new Uri("https://api.magicthegathering.io/v1/types"))) - .Throws() - .Throws(new MtgApiException("bad request")) - .Throws(new MtgApiException("forbidden")) - .Throws(new MtgApiException("server error")) - .Throws(new MtgApiException("not found")) - .Throws(new MtgApiException("unavailable")) - .Throws() - .ReturnsAsync(new RootCardTypeDto() { Types = cardTypes }); - - var service = new CardService(moqAdapter.Object, new ModelMapper(), ApiVersion.V1_0, false); - - var result = service.GetCardTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("Value cannot be null.", result.Exception.Message); - - result = service.GetCardTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("bad request", result.Exception.Message); - - result = service.GetCardTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("forbidden", result.Exception.Message); - - result = service.GetCardTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("server error", result.Exception.Message); - - result = service.GetCardTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("not found", result.Exception.Message); - - result = service.GetCardTypes(); - Assert.False(result.IsSuccess); - Assert.Equal("unavailable", result.Exception.Message); - - result = service.GetCardTypes(); - Assert.False(result.IsSuccess); - Assert.IsType(result.Exception); - - result = service.GetCardTypes(); - Assert.True(result.IsSuccess); - Assert.Null(result.Exception); - Assert.NotNull(result.Value); - } - /// /// Tests the method. /// [Fact] public void WhereTest() { - CardService service = new CardService(); + var serviceProvider = new MtgServiceProvider(); + var service = serviceProvider.GetCardService(); try { diff --git a/src/MtgApiManager.Lib.Test/Service/ServiceBaseObjectService.cs b/src/MtgApiManager.Lib.Test/Service/ServiceBaseObjectService.cs index f2b8200..6e96a64 100644 --- a/src/MtgApiManager.Lib.Test/Service/ServiceBaseObjectService.cs +++ b/src/MtgApiManager.Lib.Test/Service/ServiceBaseObjectService.cs @@ -11,7 +11,7 @@ /// /// Object used to test the abstract class. /// - public class ServiceBaseObjectService : ServiceBase + internal class ServiceBaseObjectService : ServiceBase { /// /// Initializes a new instance of the class. Defaults to version 1.0 of the API. diff --git a/src/MtgApiManager.Lib.Test/Service/SetServiceTest.cs b/src/MtgApiManager.Lib.Test/Service/SetServiceTest.cs index 0cae380..db97eee 100644 --- a/src/MtgApiManager.Lib.Test/Service/SetServiceTest.cs +++ b/src/MtgApiManager.Lib.Test/Service/SetServiceTest.cs @@ -67,7 +67,8 @@ public async Task AllAsyncTest() .Throws() .ReturnsAsync(new RootSetListDto() { Sets = sets }); - var service = new SetService(moqAdapter.Object, new ModelMapper(), ApiVersion.V1_0, false); + var serviceProvider = new MtgServiceProvider(moqAdapter.Object, new ModelMapper(), false); + var service = serviceProvider.GetSetService(); service = service.Where(x => x.Name, "name1"); var result = await service.AllAsync(); @@ -138,7 +139,8 @@ public async Task FindAsyncTest() .Throws() .ReturnsAsync(new RootSetDto() { Set = setDto }); - var service = new SetService(moqAdapter.Object, new ModelMapper(), ApiVersion.V1_0, false); + var serviceProvider = new MtgServiceProvider(moqAdapter.Object, new ModelMapper(), false); + var service = serviceProvider.GetSetService(); var result = await service.FindAsync("code1"); Assert.False(result.IsSuccess); @@ -282,7 +284,8 @@ public async Task GenerateBoosterAsyncTest() .Throws() .ReturnsAsync(new RootCardListDto() { Cards = cards }); - var service = new SetService(moqAdapter.Object, new ModelMapper(), ApiVersion.V1_0, false); + var serviceProvider = new MtgServiceProvider(moqAdapter.Object, new ModelMapper(), false); + var service = serviceProvider.GetSetService(); var result = await service.GenerateBoosterAsync("ktk"); Assert.False(result.IsSuccess); @@ -324,7 +327,8 @@ public async Task GenerateBoosterAsyncTest() [Fact] public void WhereTest() { - SetService service = new SetService(); + var serviceProvider = new MtgServiceProvider(); + var service = serviceProvider.GetSetService(); try { diff --git a/src/MtgApiManager.Lib.TestApp/MainViewModel.cs b/src/MtgApiManager.Lib.TestApp/MainViewModel.cs index eba7a67..1973621 100644 --- a/src/MtgApiManager.Lib.TestApp/MainViewModel.cs +++ b/src/MtgApiManager.Lib.TestApp/MainViewModel.cs @@ -8,6 +8,7 @@ namespace MtgApiManager.Lib.TestApp { public class MainViewModel : ViewModelBase { + private readonly IMtgServiceProvider _serviceProvider; private ObservableCollection _cardsCollection = null; private RelayCommand _cardSearchCommand; private string _cardSearchString = null; @@ -33,6 +34,7 @@ public MainViewModel() { _cardsCollection = new ObservableCollection(); _setsCollection = new ObservableCollection(); + _serviceProvider = new MtgServiceProvider(); } public ObservableCollection CardsCollection @@ -48,7 +50,7 @@ public RelayCommand CardSearchCommand return _cardSearchCommand ??= new RelayCommand( async () => { - CardService cardService = new(); + ICardService cardService = _serviceProvider.GetCardService(); if (!string.IsNullOrWhiteSpace(_cardSearchString)) { @@ -90,7 +92,7 @@ public RelayCommand FindSelectedCardCommand { IsLoading = true; - CardService cardService = new(); + ICardService cardService = _serviceProvider.GetCardService(); var result = await cardService.FindAsync(_selectedCardId); if (result.IsSuccess) @@ -113,7 +115,7 @@ public RelayCommand FindSelectedSetCommand { IsLoading = true; - SetService setService = new(); + ISetService setService = _serviceProvider.GetSetService(); var result = await setService.FindAsync(_selectedSetCode); if (result.IsSuccess) @@ -136,7 +138,7 @@ public RelayCommand GenerateBoosterCommand { IsLoading = true; - SetService setService = new(); + ISetService setService = _serviceProvider.GetSetService(); var result = await setService.GenerateBoosterAsync(_selectedSetCode); if (result.IsSuccess) @@ -165,7 +167,7 @@ public RelayCommand GetCardSubTypesCommand { IsLoading = true; - CardService cardService = new(); + ICardService cardService = _serviceProvider.GetCardService(); var result = await cardService.GetCardSubTypesAsync(); if (result.IsSuccess) @@ -187,7 +189,7 @@ public RelayCommand GetCardSuperTypesCommand { IsLoading = true; - CardService cardService = new(); + ICardService cardService = _serviceProvider.GetCardService(); var result = await cardService.GetCardSuperTypesAsync(); if (result.IsSuccess) @@ -209,7 +211,7 @@ public RelayCommand GetCardTypesCommand { IsLoading = true; - CardService cardService = new(); + ICardService cardService = _serviceProvider.GetCardService(); var result = await cardService.GetCardTypesAsync(); if (result.IsSuccess) @@ -265,7 +267,7 @@ public RelayCommand SetSearchCommand return _setSearchCommand ??= new RelayCommand( async () => { - SetService setService = new(); + ISetService setService = _serviceProvider.GetSetService(); if (!string.IsNullOrWhiteSpace(_setSearchString)) { diff --git a/src/MtgApiManager.Lib/Core/Exceptions/MtgExceptionBase.cs b/src/MtgApiManager.Lib/Core/Exceptions/MtgExceptionBase.cs index b1776f8..b325528 100644 --- a/src/MtgApiManager.Lib/Core/Exceptions/MtgExceptionBase.cs +++ b/src/MtgApiManager.Lib/Core/Exceptions/MtgExceptionBase.cs @@ -1,8 +1,4 @@ -// -// Copyright (c) 2014. All rights reserved. -// -// Jason Regnier -namespace MtgApiManager.Lib.Core.Exceptions +namespace MtgApiManager.Lib.Core.Exceptions { using System; using System.Globalization; diff --git a/src/MtgApiManager.Lib/Core/IMtgApiServiceAdapter.cs b/src/MtgApiManager.Lib/Core/IMtgApiServiceAdapter.cs index 0d5ee09..472f76b 100644 --- a/src/MtgApiManager.Lib/Core/IMtgApiServiceAdapter.cs +++ b/src/MtgApiManager.Lib/Core/IMtgApiServiceAdapter.cs @@ -7,7 +7,7 @@ /// /// Used to make web service calls. /// - public interface IMtgApiServiceAdapter + internal interface IMtgApiServiceAdapter { /// /// Do a Web Get for the given request Uri . @@ -15,6 +15,6 @@ public interface IMtgApiServiceAdapter /// The type to serialize into. /// The URL to call. /// The serialized response. - Task WebGetAsync(Uri requestUri) where T : MtgResponseBase; + Task WebGetAsync(Uri requestUri) where T : IMtgResponse; } } \ No newline at end of file diff --git a/src/MtgApiManager.Lib/Core/MtgApiServiceAdapter.cs b/src/MtgApiManager.Lib/Core/MtgApiServiceAdapter.cs index 4b79dcb..34c3c0b 100644 --- a/src/MtgApiManager.Lib/Core/MtgApiServiceAdapter.cs +++ b/src/MtgApiManager.Lib/Core/MtgApiServiceAdapter.cs @@ -13,7 +13,7 @@ /// internal class MtgApiServiceAdapter : IMtgApiServiceAdapter { - public static async Task WebGetAsyncInternal(Uri requestUri) where T : MtgResponseBase + public static async Task WebGetAsyncInternal(Uri requestUri) where T : IMtgResponse { using var client = new HttpClient(); using var response = await client.GetAsync(requestUri).ConfigureAwait(false); @@ -39,7 +39,7 @@ public static async Task WebGetAsyncInternal(Uri requestUri) where T : Mtg throw new MtgApiException(MtgApiError.ServiceUnavailable.GetDescription()); default: response.EnsureSuccessStatusCode(); - return null; + return default; } } } @@ -50,7 +50,7 @@ public static async Task WebGetAsyncInternal(Uri requestUri) where T : Mtg /// The type to serialize into. /// The URL to call. /// The serialized response. - public Task WebGetAsync(Uri requestUri) where T : MtgResponseBase + public Task WebGetAsync(Uri requestUri) where T : IMtgResponse { if (requestUri == null) { diff --git a/src/MtgApiManager.Lib/Core/PagingInfo.cs b/src/MtgApiManager.Lib/Core/PagingInfo.cs index d78ad5f..b97b77f 100644 --- a/src/MtgApiManager.Lib/Core/PagingInfo.cs +++ b/src/MtgApiManager.Lib/Core/PagingInfo.cs @@ -1,9 +1,4 @@ -// -// Copyright (c) 2014. All rights reserved. -// -// Jason Regnier - -namespace MtgApiManager.Lib.Core +namespace MtgApiManager.Lib.Core { /// /// Represents paging information diff --git a/src/MtgApiManager.Lib/Dto/Cards/CardDto.cs b/src/MtgApiManager.Lib/Dto/Cards/CardDto.cs index d152fbd..829f402 100644 --- a/src/MtgApiManager.Lib/Dto/Cards/CardDto.cs +++ b/src/MtgApiManager.Lib/Dto/Cards/CardDto.cs @@ -2,7 +2,7 @@ namespace MtgApiManager.Lib.Dto { - public class CardDto + internal class CardDto { [JsonPropertyName("artist")] public string Artist { get; set; } diff --git a/src/MtgApiManager.Lib/Dto/Cards/ForeignNameDto.cs b/src/MtgApiManager.Lib/Dto/Cards/ForeignNameDto.cs index 014d65f..8ee6775 100644 --- a/src/MtgApiManager.Lib/Dto/Cards/ForeignNameDto.cs +++ b/src/MtgApiManager.Lib/Dto/Cards/ForeignNameDto.cs @@ -2,7 +2,7 @@ namespace MtgApiManager.Lib.Dto { - public class ForeignNameDto + internal class ForeignNameDto { [JsonPropertyName("language")] public string Language { get; set; } diff --git a/src/MtgApiManager.Lib/Dto/Cards/LegalityDto.cs b/src/MtgApiManager.Lib/Dto/Cards/LegalityDto.cs index 9166c65..89c33d5 100644 --- a/src/MtgApiManager.Lib/Dto/Cards/LegalityDto.cs +++ b/src/MtgApiManager.Lib/Dto/Cards/LegalityDto.cs @@ -2,7 +2,7 @@ namespace MtgApiManager.Lib.Dto { - public class LegalityDto + internal class LegalityDto { [JsonPropertyName("format")] public string Format { get; set; } diff --git a/src/MtgApiManager.Lib/Dto/Cards/RootCardDto.cs b/src/MtgApiManager.Lib/Dto/Cards/RootCardDto.cs index a8f7e91..49cd90e 100644 --- a/src/MtgApiManager.Lib/Dto/Cards/RootCardDto.cs +++ b/src/MtgApiManager.Lib/Dto/Cards/RootCardDto.cs @@ -2,10 +2,7 @@ namespace MtgApiManager.Lib.Dto { - /// - /// Object representing a the root wrapper to read a single card. - /// - public class RootCardDto : MtgResponseBase + internal class RootCardDto : IMtgResponse { [JsonPropertyName("card")] public CardDto Card { get; set; } diff --git a/src/MtgApiManager.Lib/Dto/Cards/RootCardListDto.cs b/src/MtgApiManager.Lib/Dto/Cards/RootCardListDto.cs index 1be13a3..5250cf6 100644 --- a/src/MtgApiManager.Lib/Dto/Cards/RootCardListDto.cs +++ b/src/MtgApiManager.Lib/Dto/Cards/RootCardListDto.cs @@ -1,12 +1,9 @@ -namespace MtgApiManager.Lib.Dto -{ - using System.Collections.Generic; - using System.Text.Json.Serialization; +using System.Collections.Generic; +using System.Text.Json.Serialization; - /// - /// Object representing a the root wrapper to read a list of cards. - /// - public class RootCardListDto : MtgResponseBase +namespace MtgApiManager.Lib.Dto +{ + internal class RootCardListDto : IMtgResponse { [JsonPropertyName("cards")] public List Cards { get; set; } diff --git a/src/MtgApiManager.Lib/Dto/Cards/RulingDto.cs b/src/MtgApiManager.Lib/Dto/Cards/RulingDto.cs index 5a5f4af..f8d6d2b 100644 --- a/src/MtgApiManager.Lib/Dto/Cards/RulingDto.cs +++ b/src/MtgApiManager.Lib/Dto/Cards/RulingDto.cs @@ -2,7 +2,7 @@ namespace MtgApiManager.Lib.Dto { - public class RulingDto + internal class RulingDto { [JsonPropertyName("date")] public string Date { get; set; } diff --git a/src/MtgApiManager.Lib/Dto/MtgResponseBase.cs b/src/MtgApiManager.Lib/Dto/MtgResponseBase.cs index 731a50e..31d53bc 100644 --- a/src/MtgApiManager.Lib/Dto/MtgResponseBase.cs +++ b/src/MtgApiManager.Lib/Dto/MtgResponseBase.cs @@ -1,9 +1,6 @@ namespace MtgApiManager.Lib.Dto { - /// - /// Base class for an MTG response. - /// - public abstract class MtgResponseBase + internal interface IMtgResponse { } } \ No newline at end of file diff --git a/src/MtgApiManager.Lib/Dto/RootCardSubTypeDto.cs b/src/MtgApiManager.Lib/Dto/RootCardSubTypeDto.cs index 216f5e7..e520b03 100644 --- a/src/MtgApiManager.Lib/Dto/RootCardSubTypeDto.cs +++ b/src/MtgApiManager.Lib/Dto/RootCardSubTypeDto.cs @@ -3,10 +3,7 @@ namespace MtgApiManager.Lib.Dto { - /// - /// Object representing a root wrapper to read in card sub types. - /// - public class RootCardSubTypeDto : MtgResponseBase + internal class RootCardSubTypeDto : IMtgResponse { [JsonPropertyName("subtypes")] public List SubTypes { get; set; } diff --git a/src/MtgApiManager.Lib/Dto/RootCardSuperTypeDto.cs b/src/MtgApiManager.Lib/Dto/RootCardSuperTypeDto.cs index 5cc5c24..69600d6 100644 --- a/src/MtgApiManager.Lib/Dto/RootCardSuperTypeDto.cs +++ b/src/MtgApiManager.Lib/Dto/RootCardSuperTypeDto.cs @@ -3,10 +3,7 @@ namespace MtgApiManager.Lib.Dto { - /// - /// Object representing a root wrapper to read in card super types. - /// - public class RootCardSuperTypeDto : MtgResponseBase + internal class RootCardSuperTypeDto : IMtgResponse { [JsonPropertyName("supertypes")] public List SuperTypes { get; set; } diff --git a/src/MtgApiManager.Lib/Dto/RootCardTypeDto.cs b/src/MtgApiManager.Lib/Dto/RootCardTypeDto.cs index a684926..ed6503c 100644 --- a/src/MtgApiManager.Lib/Dto/RootCardTypeDto.cs +++ b/src/MtgApiManager.Lib/Dto/RootCardTypeDto.cs @@ -3,10 +3,7 @@ namespace MtgApiManager.Lib.Dto { - /// - /// Object representing a root wrapper to read in card types. - /// - public class RootCardTypeDto : MtgResponseBase + internal class RootCardTypeDto : IMtgResponse { [JsonPropertyName("types")] public List Types { get; set; } diff --git a/src/MtgApiManager.Lib/Dto/Set/RootSetDto.cs b/src/MtgApiManager.Lib/Dto/Set/RootSetDto.cs index 1730b86..1d12ef0 100644 --- a/src/MtgApiManager.Lib/Dto/Set/RootSetDto.cs +++ b/src/MtgApiManager.Lib/Dto/Set/RootSetDto.cs @@ -2,10 +2,7 @@ namespace MtgApiManager.Lib.Dto.Set { - /// - /// Object representing a root wrapper to read a single set. - /// - public class RootSetDto : MtgResponseBase + internal class RootSetDto : IMtgResponse { [JsonPropertyName("set")] public SetDto Set { get; set; } diff --git a/src/MtgApiManager.Lib/Dto/Set/RootSetListDto.cs b/src/MtgApiManager.Lib/Dto/Set/RootSetListDto.cs index 5c827c0..f1adcca 100644 --- a/src/MtgApiManager.Lib/Dto/Set/RootSetListDto.cs +++ b/src/MtgApiManager.Lib/Dto/Set/RootSetListDto.cs @@ -3,10 +3,7 @@ namespace MtgApiManager.Lib.Dto.Set { - /// - /// Object representing a the root wrapper to read a list of sets. - /// - public class RootSetListDto : MtgResponseBase + internal class RootSetListDto : IMtgResponse { [JsonPropertyName("sets")] public List Sets { get; set; } diff --git a/src/MtgApiManager.Lib/Dto/Set/SetDto.cs b/src/MtgApiManager.Lib/Dto/Set/SetDto.cs index ca6bf02..b800dd9 100644 --- a/src/MtgApiManager.Lib/Dto/Set/SetDto.cs +++ b/src/MtgApiManager.Lib/Dto/Set/SetDto.cs @@ -3,7 +3,7 @@ namespace MtgApiManager.Lib.Dto { - public class SetDto + internal class SetDto { [JsonPropertyName("block")] public string Block { get; set; } diff --git a/src/MtgApiManager.Lib/Model/IModelMapper.cs b/src/MtgApiManager.Lib/Model/IModelMapper.cs index 368e375..65b89d5 100644 --- a/src/MtgApiManager.Lib/Model/IModelMapper.cs +++ b/src/MtgApiManager.Lib/Model/IModelMapper.cs @@ -2,7 +2,7 @@ namespace MtgApiManager.Lib.Model { - public interface IModelMapper + internal interface IModelMapper { Card MapCard(CardDto cardDto); diff --git a/src/MtgApiManager.Lib/MtgApiManager.Lib.csproj b/src/MtgApiManager.Lib/MtgApiManager.Lib.csproj index d3cfee0..86c803c 100644 --- a/src/MtgApiManager.Lib/MtgApiManager.Lib.csproj +++ b/src/MtgApiManager.Lib/MtgApiManager.Lib.csproj @@ -51,4 +51,13 @@ + + + <_Parameter1>MtgApiManager.Lib.Test + + + <_Parameter1>DynamicProxyGenAssembly2 + + + diff --git a/src/MtgApiManager.Lib/MtgApiManager.Lib.xml b/src/MtgApiManager.Lib/MtgApiManager.Lib.xml index 6c86cb7..3d7aa63 100644 --- a/src/MtgApiManager.Lib/MtgApiManager.Lib.xml +++ b/src/MtgApiManager.Lib/MtgApiManager.Lib.xml @@ -387,46 +387,6 @@ The number of calls permitted per hour. the delay in milliseconds. - - - Object representing a the root wrapper to read a single card. - - - - - Object representing a the root wrapper to read a list of cards. - - - - - Base class for an MTG response. - - - - - Object representing a root wrapper to read in card sub types. - - - - - Object representing a root wrapper to read in card super types. - - - - - Object representing a root wrapper to read in card types. - - - - - Object representing a root wrapper to read a single set. - - - - - Object representing a the root wrapper to read a list of sets. - - Object representing a MTG card. @@ -908,104 +868,70 @@ Gets or sets the types of the card. These appear to the left of the dash in a card type. Example values: Instant, Sorcery, Artifact, Creature, Enchantment, Land, Planeswalker - - - Object representing a MTG card. - + + - - - Initializes a new instance of the class. Defaults to version 1.0 of the API. - + + - - - Initializes a new instance of the class. - - The service adapter used to interact with the MTG API. - Used to map entity objects to models. - The version of the API - Turn the rate limit on or off. + + - - - Gets all the defined by the query parameters. - - A representing the result containing all the items. + + + + + + + + + + + - + - Find a specific card by its multi verse identifier. - - The multi verse identifier to query for. - A representing the result containing a or an exception. + Object representing a MTG card. + - + - Find a specific card by its multi verse identifier. + Gets all the defined by the query parameters. - The identifier to query for. - A representing the result containing a or an exception. + A representing the result containing all the items. - + Find a specific card by its multi verse identifier. The multi verse identifier to query for. A representing the result containing a or an exception. - + Find a specific card by its multi verse identifier. The identifier to query for. A representing the result containing a or an exception. - + Gets a list of all the card sub types. A list of all the card super types. - - - Gets a list of all the card sub types. - - A list of all the card super types. - - - - Gets a list of all the card super types. - - A list of all the card super types. - - + Gets a list of all the card super types. A list of all the card super types. - + Gets a list of all the card types. A list of all the card types. - - - Gets a list of all the card types. - - A list of all the card types. - - - - Adds a query parameter. - - The type of property to add the query for. - The property to add the query for. - The value of the query. - The instance of its self with the new query parameter. - Defines an object to be query able against the MTG API. @@ -1022,59 +948,66 @@ The value of the query. The instance of its self with the new query parameter. - + + + Defines a query parameter + + + - Base class for query parameters. + Used to get an instance of a service class. - + - Base class for a service. + Gets an instance of the card service. - The type of model used by the service. + An instance of the card service. - + - Initializes a new instance of the class. + Gets an instance of the set service. - The service adapter used to interact with the MTG API. - Used to map entity objects to models. - The version of the API (currently only 1 version.) - The end point of the service. - Turn the rate limit on or off. + An instance of the set service. - + - Gets the list of where queries. + Object representing a MTG set. - + - Gets all the objects defined by the query parameters. + Gets all the defined by the query parameters. A representing the result containing all the items. - + - Builds the URL to call the web service. + Find a specific card by its set code. - The list of parameters to add to the query. - The URL to make the call with. + The set code to query for. + A representing the result containing a or an exception. - + - Builds the URL to call the web service. + Generates a booster pack for a specific set asynchronously. - The parameter value to add. - The URL to make the call with. + The set code to generate a booster for. + A representing the result containing a or an exception. + + + - + - Makes a GET call to the web service. + Initializes a new instance of the service provider. - The type of object to serialize the response into. - The URL to call. - The response of the GET serialized into the specified object. + + + + + + @@ -1091,53 +1024,17 @@ Gets or sets the name of the set. - - - Object representing a MTG set. - - - - - Initializes a new instance of the class. Defaults to version 1.0 of the API. - - - - - Initializes a new instance of the class. - - The service adapter used to interact with the MTG API. - Used to map entity objects to models. - The version of the API - Turn the rate limit on or off. - - - Gets all the defined by the query parameters. - - A representing the result containing all the items. + - - Find a specific card by its set code. - - The set code to query for. - A representing the result containing a or an exception. + - - Generates a booster pack for a specific set asynchronously. - - The set code to generate a booster for. - A representing the result containing a or an exception. + - - Adds a query parameter. - - The type of property to add the query for. - The property to add the query for. - The value of the query. - The instance of its self with the new query parameter. + diff --git a/src/MtgApiManager.Lib/Service/CardQueryParameter.cs b/src/MtgApiManager.Lib/Service/CardQueryParameter.cs index 1c72d75..7195c87 100644 --- a/src/MtgApiManager.Lib/Service/CardQueryParameter.cs +++ b/src/MtgApiManager.Lib/Service/CardQueryParameter.cs @@ -5,7 +5,7 @@ namespace MtgApiManager.Lib.Service /// /// Query parameters for the object. /// - public class CardQueryParameter : QueryParameterBase + public class CardQueryParameter : IQueryParameter { /// /// Gets or sets the artist of the card. diff --git a/src/MtgApiManager.Lib/Service/CardService.cs b/src/MtgApiManager.Lib/Service/CardService.cs index 248c7e3..0d9fab1 100644 --- a/src/MtgApiManager.Lib/Service/CardService.cs +++ b/src/MtgApiManager.Lib/Service/CardService.cs @@ -10,27 +10,8 @@ namespace MtgApiManager.Lib.Service { - /// - /// Object representing a MTG card. - /// - public class CardService - : ServiceBase, IMtgQueryable + internal class CardService : ServiceBase, ICardService { - /// - /// Initializes a new instance of the class. Defaults to version 1.0 of the API. - /// - public CardService() - : this(new MtgApiServiceAdapter(), new ModelMapper(), ApiVersion.V1_0) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The service adapter used to interact with the MTG API. - /// Used to map entity objects to models. - /// The version of the API - /// Turn the rate limit on or off. public CardService( IMtgApiServiceAdapter serviceAdapter, IModelMapper modelMapper, @@ -40,10 +21,7 @@ public CardService( { } - /// - /// Gets all the defined by the query parameters. - /// - /// A representing the result containing all the items. + /// public async override Task>> AllAsync() { try @@ -59,32 +37,10 @@ public async override Task>> AllAsync() } } - /// - /// Find a specific card by its multi verse identifier. - /// - /// The multi verse identifier to query for. - /// A representing the result containing a or an exception. - public Exceptional Find(int multiverseId) => FindAsync(multiverseId.ToString()).Result; - - /// - /// Find a specific card by its multi verse identifier. - /// - /// The identifier to query for. - /// A representing the result containing a or an exception. - public Exceptional Find(string id) => FindAsync(id).Result; - - /// - /// Find a specific card by its multi verse identifier. - /// - /// The multi verse identifier to query for. - /// A representing the result containing a or an exception. + /// public Task> FindAsync(int multiverseId) => FindAsync(multiverseId.ToString()); - /// - /// Find a specific card by its multi verse identifier. - /// - /// The identifier to query for. - /// A representing the result containing a or an exception. + /// public async Task> FindAsync(string id) { try @@ -100,29 +56,7 @@ public async Task> FindAsync(string id) } } - /// - /// Gets a list of all the card sub types. - /// - /// A list of all the card super types. - public Exceptional> GetCardSubTypes() - { - try - { - var url = new Uri(new Uri(BaseMtgUrl), string.Concat(Version.GetDescription(), "/", ApiEndPoint.CardSubTypes.GetDescription())); - var rootTypeList = CallWebServiceGet(url).Result; - - return Exceptional>.Success(rootTypeList.SubTypes, MtgApiController.CreatePagingInfo()); - } - catch (AggregateException ex) - { - return Exceptional>.Failure(ex.Flatten().InnerException); - } - } - - /// - /// Gets a list of all the card sub types. - /// - /// A list of all the card super types. + /// public async Task>> GetCardSubTypesAsync() { try @@ -138,29 +72,7 @@ public async Task>> GetCardSubTypesAsync() } } - /// - /// Gets a list of all the card super types. - /// - /// A list of all the card super types. - public Exceptional> GetCardSuperTypes() - { - try - { - var url = new Uri(new Uri(BaseMtgUrl), string.Concat(Version.GetDescription(), "/", ApiEndPoint.CardSuperTypes.GetDescription())); - var rootTypeList = CallWebServiceGet(url).Result; - - return Exceptional>.Success(rootTypeList.SuperTypes, MtgApiController.CreatePagingInfo()); - } - catch (AggregateException ex) - { - return Exceptional>.Failure(ex.Flatten().InnerException); - } - } - - /// - /// Gets a list of all the card super types. - /// - /// A list of all the card super types. + /// public async Task>> GetCardSuperTypesAsync() { try @@ -176,29 +88,7 @@ public async Task>> GetCardSuperTypesAsync() } } - /// - /// Gets a list of all the card types. - /// - /// A list of all the card types. - public Exceptional> GetCardTypes() - { - try - { - var url = new Uri(new Uri(BaseMtgUrl), string.Concat(Version.GetDescription(), "/", ApiEndPoint.CardTypes.GetDescription())); - var rootTypeList = CallWebServiceGet(url).Result; - - return Exceptional>.Success(rootTypeList.Types, MtgApiController.CreatePagingInfo()); - } - catch (AggregateException ex) - { - return Exceptional>.Failure(ex.Flatten().InnerException); - } - } - - /// - /// Gets a list of all the card types. - /// - /// A list of all the card types. + /// public async Task>> GetCardTypesAsync() { try @@ -214,14 +104,9 @@ public async Task>> GetCardTypesAsync() } } - /// - /// Adds a query parameter. - /// - /// The type of property to add the query for. - /// The property to add the query for. - /// The value of the query. - /// The instance of its self with the new query parameter. - public CardService Where(Expression> property, U value) + + /// + public ICardService Where(Expression> property, U value) { if (property == null) { diff --git a/src/MtgApiManager.Lib/Service/ICardService.cs b/src/MtgApiManager.Lib/Service/ICardService.cs new file mode 100644 index 0000000..85426f0 --- /dev/null +++ b/src/MtgApiManager.Lib/Service/ICardService.cs @@ -0,0 +1,51 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using MtgApiManager.Lib.Core; +using MtgApiManager.Lib.Model; + +namespace MtgApiManager.Lib.Service +{ + /// + /// Object representing a MTG card. + /// + public interface ICardService : IMtgQueryable + { + /// + /// Gets all the defined by the query parameters. + /// + /// A representing the result containing all the items. + Task>> AllAsync(); + + /// + /// Find a specific card by its multi verse identifier. + /// + /// The multi verse identifier to query for. + /// A representing the result containing a or an exception. + Task> FindAsync(int multiverseId); + + /// + /// Find a specific card by its multi verse identifier. + /// + /// The identifier to query for. + /// A representing the result containing a or an exception. + Task> FindAsync(string id); + + /// + /// Gets a list of all the card sub types. + /// + /// A list of all the card super types. + Task>> GetCardSubTypesAsync(); + + /// + /// Gets a list of all the card super types. + /// + /// A list of all the card super types. + Task>> GetCardSuperTypesAsync(); + + /// + /// Gets a list of all the card types. + /// + /// A list of all the card types. + Task>> GetCardTypesAsync(); + } +} \ No newline at end of file diff --git a/src/MtgApiManager.Lib/Service/IMtgQueryable.cs b/src/MtgApiManager.Lib/Service/IMtgQueryable.cs index 7727b8e..e28c614 100644 --- a/src/MtgApiManager.Lib/Service/IMtgQueryable.cs +++ b/src/MtgApiManager.Lib/Service/IMtgQueryable.cs @@ -9,7 +9,7 @@ /// The type of service. /// The type of query. public interface IMtgQueryable - where TQuery : QueryParameterBase + where TQuery : IQueryParameter { /// /// Adds a query parameter. diff --git a/src/MtgApiManager.Lib/Service/IQueryParameter.cs b/src/MtgApiManager.Lib/Service/IQueryParameter.cs new file mode 100644 index 0000000..637ac35 --- /dev/null +++ b/src/MtgApiManager.Lib/Service/IQueryParameter.cs @@ -0,0 +1,9 @@ +namespace MtgApiManager.Lib.Service +{ + /// + /// Defines a query parameter + /// + public interface IQueryParameter + { + } +} \ No newline at end of file diff --git a/src/MtgApiManager.Lib/Service/IServiceProvider.cs b/src/MtgApiManager.Lib/Service/IServiceProvider.cs new file mode 100644 index 0000000..1574f81 --- /dev/null +++ b/src/MtgApiManager.Lib/Service/IServiceProvider.cs @@ -0,0 +1,20 @@ +namespace MtgApiManager.Lib.Service +{ + /// + /// Used to get an instance of a service class. + /// + public interface IMtgServiceProvider + { + /// + /// Gets an instance of the card service. + /// + /// An instance of the card service. + ICardService GetCardService(); + + /// + /// Gets an instance of the set service. + /// + /// An instance of the set service. + ISetService GetSetService(); + } +} \ No newline at end of file diff --git a/src/MtgApiManager.Lib/Service/ISetService.cs b/src/MtgApiManager.Lib/Service/ISetService.cs new file mode 100644 index 0000000..4f1dc6a --- /dev/null +++ b/src/MtgApiManager.Lib/Service/ISetService.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using MtgApiManager.Lib.Core; +using MtgApiManager.Lib.Model; + +namespace MtgApiManager.Lib.Service +{ + /// + /// Object representing a MTG set. + /// + public interface ISetService : IMtgQueryable + { + /// + /// Gets all the defined by the query parameters. + /// + /// A representing the result containing all the items. + Task>> AllAsync(); + + /// + /// Find a specific card by its set code. + /// + /// The set code to query for. + /// A representing the result containing a or an exception. + Task> FindAsync(string code); + + /// + /// Generates a booster pack for a specific set asynchronously. + /// + /// The set code to generate a booster for. + /// A representing the result containing a or an exception. + Task>> GenerateBoosterAsync(string code); + } +} \ No newline at end of file diff --git a/src/MtgApiManager.Lib/Service/QueryParameterBase.cs b/src/MtgApiManager.Lib/Service/QueryParameterBase.cs deleted file mode 100644 index 98f2ef7..0000000 --- a/src/MtgApiManager.Lib/Service/QueryParameterBase.cs +++ /dev/null @@ -1,13 +0,0 @@ -// -// Copyright (c) 2014. All rights reserved. -// -// Jason Regnier -namespace MtgApiManager.Lib.Service -{ - /// - /// Base class for query parameters. - /// - public class QueryParameterBase - { - } -} \ No newline at end of file diff --git a/src/MtgApiManager.Lib/Service/ServiceBase.cs b/src/MtgApiManager.Lib/Service/ServiceBase.cs index 4ef859f..1555f86 100644 --- a/src/MtgApiManager.Lib/Service/ServiceBase.cs +++ b/src/MtgApiManager.Lib/Service/ServiceBase.cs @@ -3,30 +3,20 @@ using System.Linq; using System.Threading.Tasks; using MtgApiManager.Lib.Core; +using MtgApiManager.Lib.Dto; using MtgApiManager.Lib.Model; using MtgApiManager.Lib.Utility; namespace MtgApiManager.Lib.Service { - /// - /// Base class for a service. - /// - /// The type of model used by the service. - public abstract class ServiceBase + internal abstract class ServiceBase where TModel : class { private const string BASE_URL = "https://api.magicthegathering.io"; + private readonly ApiEndPoint _endPoint; private readonly bool _isRateLimitOn; - /// - /// Initializes a new instance of the class. - /// - /// The service adapter used to interact with the MTG API. - /// Used to map entity objects to models. - /// The version of the API (currently only 1 version.) - /// The end point of the service. - /// Turn the rate limit on or off. protected ServiceBase( IMtgApiServiceAdapter serviceAdapter, IModelMapper modelMapper, @@ -37,35 +27,23 @@ protected ServiceBase( Adapter = serviceAdapter; ModelMapper = modelMapper; Version = version; - EndPoint = endpoint; + _endPoint = endpoint; _isRateLimitOn = rateLimitOn; WhereQueries = new Dictionary(); } protected IMtgApiServiceAdapter Adapter { get; } - protected string BaseMtgUrl => BASE_URL; - protected ApiEndPoint EndPoint { get; } + + protected static string BaseMtgUrl => BASE_URL; protected IModelMapper ModelMapper { get; } protected ApiVersion Version { get; } - /// - /// Gets the list of where queries. - /// protected Dictionary WhereQueries { get; } - /// - /// Gets all the objects defined by the query parameters. - /// - /// A representing the result containing all the items. public abstract Task>> AllAsync(); - /// - /// Builds the URL to call the web service. - /// - /// The list of parameters to add to the query. - /// The URL to make the call with. protected Uri BuildUri(Dictionary parameters) { if (parameters == null) @@ -76,7 +54,7 @@ protected Uri BuildUri(Dictionary parameters) var urlBuilder = new UriBuilder( new Uri( new Uri(BaseMtgUrl), - string.Concat(Version.GetDescription(), "/", EndPoint.GetDescription()))); + string.Concat(Version.GetDescription(), "/", _endPoint.GetDescription()))); var paramList = parameters .Select(p => $"{p.Key}={p.Value}") @@ -87,11 +65,6 @@ protected Uri BuildUri(Dictionary parameters) return urlBuilder.Uri; } - /// - /// Builds the URL to call the web service. - /// - /// The parameter value to add. - /// The URL to make the call with. protected Uri BuildUri(string parameterValue) { if (string.IsNullOrWhiteSpace(parameterValue)) @@ -101,16 +74,10 @@ protected Uri BuildUri(string parameterValue) return new Uri( new Uri(BaseMtgUrl), - string.Concat(Version.GetDescription(), "/", EndPoint.GetDescription(), "/", parameterValue)); + string.Concat(Version.GetDescription(), "/", _endPoint.GetDescription(), "/", parameterValue)); } - /// - /// Makes a GET call to the web service. - /// - /// The type of object to serialize the response into. - /// The URL to call. - /// The response of the GET serialized into the specified object. - protected Task CallWebServiceGet(Uri requestUri) where T : Dto.MtgResponseBase + protected Task CallWebServiceGet(Uri requestUri) where T : IMtgResponse { if (requestUri == null) { @@ -120,11 +87,13 @@ protected Task CallWebServiceGet(Uri requestUri) where T : Dto.MtgResponse return CallWebServiceGetInternal(requestUri); } - protected async Task CallWebServiceGetInternal(Uri requestUri) where T : Dto.MtgResponseBase + protected async Task CallWebServiceGetInternal(Uri requestUri) where T : IMtgResponse { // Makes sure that th rate limit is not reached. if (_isRateLimitOn) + { await MtgApiController.HandleRateLimit().ConfigureAwait(false); + } return await Adapter.WebGetAsync(requestUri).ConfigureAwait(false); } diff --git a/src/MtgApiManager.Lib/Service/ServiceProvider.cs b/src/MtgApiManager.Lib/Service/ServiceProvider.cs new file mode 100644 index 0000000..7fb9b5a --- /dev/null +++ b/src/MtgApiManager.Lib/Service/ServiceProvider.cs @@ -0,0 +1,48 @@ +using MtgApiManager.Lib.Core; +using MtgApiManager.Lib.Model; + +namespace MtgApiManager.Lib.Service +{ + /// + public class MtgServiceProvider : IMtgServiceProvider + { + private readonly ApiVersion _apiVersion; + private readonly IMtgApiServiceAdapter _serviceAdapter; + private readonly IModelMapper _modelMapper; + private readonly bool _rateLimitOn; + private ICardService _cardService; + private ISetService _setService; + + /// + /// Initializes a new instance of the service provider. + /// + public MtgServiceProvider() + : this(new MtgApiServiceAdapter(), new ModelMapper()) + { + } + + internal MtgServiceProvider( + IMtgApiServiceAdapter serviceAdapter, + IModelMapper modelMapper, + bool rateLimitOn = true) + { + _serviceAdapter = serviceAdapter; + _modelMapper = modelMapper; + _rateLimitOn = rateLimitOn; + _apiVersion = ApiVersion.V1_0; + } + + /// + public ICardService GetCardService() + { + return _cardService ??= new CardService(_serviceAdapter, _modelMapper, _apiVersion, _rateLimitOn); + } + + /// + public ISetService GetSetService() + { + return _setService ??= new SetService(_serviceAdapter, _modelMapper, _apiVersion, _rateLimitOn); + } + } + +} \ No newline at end of file diff --git a/src/MtgApiManager.Lib/Service/SetQueryParameter.cs b/src/MtgApiManager.Lib/Service/SetQueryParameter.cs index e1c57cd..8e4594e 100644 --- a/src/MtgApiManager.Lib/Service/SetQueryParameter.cs +++ b/src/MtgApiManager.Lib/Service/SetQueryParameter.cs @@ -5,7 +5,7 @@ namespace MtgApiManager.Lib.Service /// /// Query parameters for the object. /// - public class SetQueryParameter : QueryParameterBase + public class SetQueryParameter : IQueryParameter { /// /// Gets or sets the block the set is in. diff --git a/src/MtgApiManager.Lib/Service/SetService.cs b/src/MtgApiManager.Lib/Service/SetService.cs index dce3474..798d1a3 100644 --- a/src/MtgApiManager.Lib/Service/SetService.cs +++ b/src/MtgApiManager.Lib/Service/SetService.cs @@ -12,27 +12,8 @@ namespace MtgApiManager.Lib.Service { - /// - /// Object representing a MTG set. - /// - public class SetService - : ServiceBase, IMtgQueryable + internal class SetService : ServiceBase, ISetService { - /// - /// Initializes a new instance of the class. Defaults to version 1.0 of the API. - /// - public SetService() - : this(new MtgApiServiceAdapter(), new ModelMapper(), ApiVersion.V1_0) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The service adapter used to interact with the MTG API. - /// Used to map entity objects to models. - /// The version of the API - /// Turn the rate limit on or off. public SetService( IMtgApiServiceAdapter serviceAdapter, IModelMapper modelMapper, @@ -42,10 +23,7 @@ public SetService( { } - /// - /// Gets all the defined by the query parameters. - /// - /// A representing the result containing all the items. + /// public async override Task>> AllAsync() { try @@ -60,11 +38,7 @@ public async override Task>> AllAsync() } } - /// - /// Find a specific card by its set code. - /// - /// The set code to query for. - /// A representing the result containing a or an exception. + /// public async Task> FindAsync(string code) { try @@ -80,11 +54,7 @@ public async Task> FindAsync(string code) } } - /// - /// Generates a booster pack for a specific set asynchronously. - /// - /// The set code to generate a booster for. - /// A representing the result containing a or an exception. + /// public async Task>> GenerateBoosterAsync(string code) { try @@ -104,14 +74,8 @@ public async Task>> GenerateBoosterAsync(string code) } } - /// - /// Adds a query parameter. - /// - /// The type of property to add the query for. - /// The property to add the query for. - /// The value of the query. - /// The instance of its self with the new query parameter. - public SetService Where(Expression> property, U value) + /// + public ISetService Where(Expression> property, U value) { if (property == null) { diff --git a/src/MtgApiManager.Lib/Utility/Extensions.cs b/src/MtgApiManager.Lib/Utility/Extensions.cs index a1fcf13..67efe22 100644 --- a/src/MtgApiManager.Lib/Utility/Extensions.cs +++ b/src/MtgApiManager.Lib/Utility/Extensions.cs @@ -7,7 +7,7 @@ /// /// Contains extension methods. /// - public static class Extensions + internal static class Extensions { /// /// Extracts the description attribute from an type. diff --git a/src/MtgApiManager.Lib/Utility/QueryUtility.cs b/src/MtgApiManager.Lib/Utility/QueryUtility.cs index 902dba2..46f0fcd 100644 --- a/src/MtgApiManager.Lib/Utility/QueryUtility.cs +++ b/src/MtgApiManager.Lib/Utility/QueryUtility.cs @@ -7,7 +7,7 @@ namespace MtgApiManager.Lib.Utility /// /// Utility methods dealing with queries. /// - public static class QueryUtility + internal static class QueryUtility { /// /// Gets the property name from a member expression.