Skip to content

Commit

Permalink
feat: mtgjson api is tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Guibod committed Feb 17, 2023
1 parent eefc43a commit c4a6dff
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 11 deletions.
29 changes: 29 additions & 0 deletions docs/source/_autosummary/mightstone.services.mtgjson.models.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
mightstone.services.mtgjson.models
==================================

.. automodule:: mightstone.services.mtgjson.models











.. rubric:: Classes

.. autosummary::

RetailPrices









31 changes: 31 additions & 0 deletions docs/source/_autosummary/mightstone.services.mtgjson.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
mightstone.services.mtgjson
===========================

.. automodule:: mightstone.services.mtgjson











.. rubric:: Classes

.. autosummary::

MtgJson
MtgJsonCompression
MtgJsonMode









6 changes: 6 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"sphinx.ext.napoleon",
"sphinx_rtd_theme",
"sphinxcontrib.autodoc_pydantic",
"sphinx.ext.autosummary",
"myst_parser",
]

# Add any paths that contain templates here, relative to this directory.
Expand All @@ -45,7 +47,11 @@
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
<<<<<<< HEAD
exclude_patterns = []
=======
exclude_patterns = ["_build", "_templates"]
>>>>>>> 4144f04 (chore: linting alerts)


# -- Options for HTML output -------------------------------------------------
Expand Down
5 changes: 2 additions & 3 deletions src/mightstone/ass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ def _to_task(future, as_task, loop):
return loop.create_task(future)


def stream_as_list(ait):
def stream_as_list(ait, limit=100):
async def run(my_it):
return await stream.list(my_it)
return await (stream.iterate(my_it) | pipe.take(limit) | pipe.list())

return asyncio_run(run(ait))


def stream_print(ait, limit=100):
async def run(my_it):
# return await stream.print(my_it)
await (stream.iterate(my_it) | pipe.take(limit) | pipe.print())

return asyncio_run(run(ait))
2 changes: 1 addition & 1 deletion src/mightstone/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ijson as ijson_module
from aiohttp import ClientSession
from aiohttp_client_cache import CachedSession, SQLiteBackend
from aiohttp_client_cache import CacheBackend, CachedSession, SQLiteBackend
from appdirs import user_cache_dir

from mightstone.ass import asyncio_run
Expand Down
22 changes: 18 additions & 4 deletions src/mightstone/services/mtgjson/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
from enum import Enum
from io import BytesIO
from typing import Any, AsyncGenerator, List, Optional, Tuple, Type, TypeVar, Union

from aiohttp import ClientResponseError
Expand Down Expand Up @@ -42,6 +41,12 @@ class MtgJsonMode(Enum):


class MtgJsonCompression(Enum):
"""
Available compression mode enumerator
MTGJSON provide 5 compression formats, Mightstone support 4 of them.
"""

NONE = None
XZ = "xz"
ZIP = "zip"
Expand All @@ -64,6 +69,12 @@ def to_stream_compression(self):


class MtgJson(MightstoneHttpClient):
"""
MTGJSON client
Supports compression and will get gzip versions by default.
"""

base_url = "https://mtgjson.com"

def __init__(
Expand Down Expand Up @@ -120,6 +131,7 @@ async def all_identifiers(self) -> AsyncGenerator[Card, None]:
async def all_prices(self) -> AsyncGenerator[Any, None]:
"""
all prices of cards in various formats.
:return: An async iterator of CardPrices
"""
async for k, item in self._iterate_model(kind="AllPrices"):
Expand All @@ -128,15 +140,17 @@ async def all_prices(self) -> AsyncGenerator[Any, None]:
async def atomic_cards(self) -> AsyncGenerator[CardAtomic, None]:
"""
every Card (Atomic) card.
:return: An async iterator of CardAtomicGroup
:return: An async iterator of ``CardAtomicGroup``
"""
async for item in self._atomic(kind="AtomicCards"):
yield item

async def card_types(self) -> CardTypes:
"""
every card type of any type of card.
:return: A CardTypes object
:return: A ``CardTypes`` object
"""
return await self._get_item("CardTypes", model=CardTypes)

Expand Down Expand Up @@ -264,7 +278,7 @@ async def set_list(self) -> AsyncGenerator[SetList, None]:
:return: An async iterator of SetList
"""
async for item in self._iterate_model(
async for k, item in self._iterate_model(
kind="SetList", model=SetList, mode=MtgJsonMode.LIST_OF_MODEL
):
yield item
Expand Down
6 changes: 5 additions & 1 deletion src/mightstone/services/mtgjson/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ class MtgJsonObj(TypedDict):
@click.group()
@click.pass_obj
@click.option("--cache", type=int, default=0)
@click.option("--compression", type=click.Choice([t.value for t in MtgJsonCompression]), default=MtgJsonCompression.GZIP)
@click.option(
"--compression",
type=click.Choice([t.value for t in MtgJsonCompression]),
default=MtgJsonCompression.GZIP,
)
def mtgjson(obj, **kwargs):
obj["client"] = MtgJson(**kwargs)

Expand Down
13 changes: 13 additions & 0 deletions tests/mightstone/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os

import pytest


@pytest.fixture(autouse=True)
def skip_remote_api():
if "CI" in os.environ:
pytest.skip("Remote API tests are disable in CI")
elif os.environ.get("REMOTE_API_TEST", "1") != "1":
pytest.skip("Remote API tests are disable by explicit REMOTE_API_TEST != 1")
else:
yield

0 comments on commit c4a6dff

Please sign in to comment.