Example #1
0
    public static IHttpClientBuilder AddScryfallApiClient(this IServiceCollection services, Action <ScryfallApiClientConfig> configure)
    {
        var clientConfig = ScryfallApiClientConfig.GetDefault();

        configure(clientConfig);
        return(AddScryfallApiClient(services, clientConfig));
    }
Example #2
0
        static async Task Main(string[] args)
        {
            var httpClient = new HttpClient {
                BaseAddress = ScryfallApiClientConfig.GetDefault().ScryfallApiBaseAddress
            };
            var client = new ScryfallApiClient(httpClient);

            do
            {
                try
                {
                    var randomCard = await client.Cards.GetRandom();

                    Console.Clear();
                    Console.WriteLine(DrawCard(randomCard));
                }
                catch (ScryfallApiException ex)
                {
                    Console.Clear();
                    Console.WriteLine($"Error: {ex.Message}");
                    Console.WriteLine($"Status Code: {ex.ResponseStatusCode}");
                    Console.WriteLine($"Remote Call: {ex.RequestMethod} {ex.RequestUri}");
                }
                Console.WriteLine(Environment.NewLine + "Press any key for a new card. Press Esc to quit.");
            } while (Console.ReadKey().Key != ConsoleKey.Escape);
        }
Example #3
0
        public BaseRestService(HttpClient httpClient, ScryfallApiClientConfig clientConfig, IMemoryCache cache)
        {
            _httpClient   = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
            _clientConfig = clientConfig;
            _cache        = cache;

            if (clientConfig.EnableCaching)
            {
                _cacheOptions = new MemoryCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = _clientConfig.UseSlidingCacheExpiration ? null : _clientConfig.CacheDuration,
                    SlidingExpiration = _clientConfig.UseSlidingCacheExpiration ? _clientConfig.CacheDuration : null,
                };
            }
        }
Example #4
0
    /// <summary>
    /// Instantiate a new Scryfall API client.
    /// </summary>
    /// <param name="httpClient"></param>
    /// <param name="clientConfig"></param>
    /// <param name="cache"></param>
    public ScryfallApiClient(HttpClient httpClient, ScryfallApiClientConfig clientConfig = null, IMemoryCache cache = null)
    {
        if (clientConfig is null)
        {
            clientConfig = ScryfallApiClientConfig.GetDefault();
            clientConfig.EnableCaching = cache is not null;
        }

        var restService = new BaseRestService(httpClient, clientConfig, cache);

        _cards     = new Lazy <ICards>(() => new Cards(restService));
        _catalogs  = new Lazy <ICatalogs>(() => new Catalogs(restService));
        _sets      = new Lazy <ISets>(() => new Sets(restService));
        _symbology = new Lazy <ISymbology>(() => new Symbology(restService));
        _bulkData  = new Lazy <IBulkData>(() => new BulkData(restService));
    }
Example #5
0
 public static IHttpClientBuilder AddScryfallApiClient(this IServiceCollection services) =>
 AddScryfallApiClient(services, ScryfallApiClientConfig.GetDefault());
Example #6
0
    public static IHttpClientBuilder AddScryfallApiClient(this IServiceCollection services, ScryfallApiClientConfig clientConfig)
    {
        services.AddScoped(services => clientConfig.Clone());
        var clientBuilder = services.AddHttpClient <ScryfallApiClient>(client =>
        {
            client.BaseAddress = clientConfig.ScryfallApiBaseAddress;
        });

        return(clientBuilder);
    }