/mtg-sdk-dotnet

Magic: The Gathering SDK - C#.NET

Primary LanguageC#MIT LicenseMIT

Magic: The Gathering SDK

Build status codecov NuGet

This is the Magic: The Gathering SDK C# .NET implementation. It is a wrapper around the MTG API of magicthegathering.io.

Installation

MtgApiManager is available on the [NuGet Gallery]

Use the following command in the Package Manager Console.

PM> Install-Package MtgApiManager.Lib

Rate Limit

The MTG API has a rate limit 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.

IMtgServiceProvider serviceProvider = new MtgServiceProvider();

The result of all service calls resturns a generic IOperationResult containing the results of the call. It's also important to note that the active query that was added with the Where method will be cleared after a query is performed with the AllAsync method.

ICardService service = serviceProvider.GetCardService();
IOperationResult<List<ICard>> result = service.AllAsync();
if (result.IsSuccess)
{
  var value = result.Value;
}
else
{
  var exception = result.Exception;
}

Find a card by id

ICardService service = serviceProvider.GetCardService();
var result = await service.FindAsync("f2eb06047a3a8e515bff62b55f29468fcde6332a");

Find a card by multiverse id

ICardService service = serviceProvider.GetCardService();
var result = await service.FindAsync(123);

Filter Cards via query parameters

ICardService 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)

ICardService service = serviceProvider.GetCardService();
var result = await service.AllAsync()

Get all cards with pagination

ICardService service = serviceProvider.GetCardService();
var result = await service.Where(x => x.Page, 5)
                          .Where(x => x.PageSize, 250)
                          .AllAsync()

Get all card types

ICardService service = serviceProvider.GetCardService();
var result = await service.GetCardTypesAsync();

Get all card supertypes

ICardService service = serviceProvider.GetCardService();
var result = await service.GetCardSuperTypesAsync();

Get all card subtypes

ICardService service = serviceProvider.GetCardService();
var result = await service.GetCardSubTypesAsync();

Get all game formats

ICardService service = serviceProvider.GetCardService();
var result = await service.GetFormatsAsync();

Find a set by code

ISetService service = serviceProvider.GetSetService();
var result = await service.FindAsync("ktk");

Filter sets via query parameters

ISetService service = serviceProvider.GetSetService();
var result = await service.Where(x => x.Name, "khans").AllAsync()

Get all Sets

ISetService service = serviceProvider.GetSetService();
var result = await service.AllAsync()

Generate booster

ISetService service = serviceProvider.GetSetService();
var result = await service.GenerateBoosterAsync("ktk")