Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dev = [
"codespell>=2.4.3",
"prek>=0.5.10",
"pytest",
"pytest-asyncio>=1.4.1.post1",
"pytest-cov",
"ruff>=0.16.0",
"ty>=0.0.96",
Expand All @@ -29,6 +30,8 @@ ignore-words-list = "gameboy"

[tool.pytest.ini_options]
addopts = "--cov=src --cov-report=term-missing --cov-fail-under 98"
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"

[tool.ruff]
line-length = 100
Expand Down
66 changes: 66 additions & 0 deletions src/pypricecharting/asyncpricecharting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from types import TracebackType
from typing import Self

from httpx2 import AsyncClient


class AsyncPyPriceCharting:
"""Asynchronous PriceCharting API client."""

def __init__(self, credential: str):
"""Create a client authenticated with a PriceCharting API token."""
self.credential = credential
self.client = AsyncClient()

async def aclose(self):
"""Close the underlying HTTP client."""
await self.client.aclose()

async def __aenter__(self) -> Self:
"""Enter the async context and return the client."""
return self

async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> None:
"""Close the underlying HTTP client on exit."""
await self.aclose()

async def product(self):
"""Get a single product."""
pass

async def products(self):
"""Search for products."""
pass

async def offers(self):
"""List marketplace offers."""
pass

async def offer_details(self):
"""Get the details of a marketplace offer."""
pass

async def offer_publish(self):
"""Publish a new marketplace offer."""
pass

async def offer_feedback(self):
"""Leave feedback on a marketplace offer."""
pass

async def offer_ship(self):
"""Mark a marketplace offer as shipped."""
pass

async def offer_end(self):
"""End a marketplace offer."""
pass

async def offer_refund(self):
"""Refund a marketplace offer."""
pass
66 changes: 66 additions & 0 deletions src/pypricecharting/pypricecharting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from types import TracebackType
from typing import Self

from httpx2 import Client


class PyPriceCharting:
"""Synchronous PriceCharting API client."""

def __init__(self, credential: str):
"""Create a client authenticated with a PriceCharting API token."""
self.credential = credential
self.client = Client()

def close(self):
"""Close the underlying HTTP client."""
self.client.close()

def __enter__(self) -> Self:
"""Enter the context and return the client."""
return self

def __exit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> None:
"""Close the underlying HTTP client on exit."""
self.close()

def product(self):
"""Get a single product."""
pass

def products(self):
"""Search for products."""
pass

def offers(self):
"""List marketplace offers."""
pass

def offer_details(self):
"""Get the details of a marketplace offer."""
pass

def offer_publish(self):
"""Publish a new marketplace offer."""
pass

def offer_feedback(self):
"""Leave feedback on a marketplace offer."""
pass

def offer_ship(self):
"""Mark a marketplace offer as shipped."""
pass

def offer_end(self):
"""End a marketplace offer."""
pass

def offer_refund(self):
"""Refund a marketplace offer."""
pass
35 changes: 35 additions & 0 deletions src/pypricecharting/route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from enum import Enum, unique

# Source: https://www.pricecharting.com/api-documentation


BASE_URL = "https://www.pricecharting.com"


@unique
class Prices(Enum):
"""PriceCharting Prices API Routes"""

PRODUCT = "/api/product"
PRODUCTS = "/api/products"


@unique
class MarketPlace(Enum):
"""PriceCharting MarketPlace API Routes"""

OFFERS = "/api/offers"
OFFER_PUBLISH = "/api/offer-publish"
OFFER_DETAILS = "/api/offer-details"
OFFER_FEEDBACK = "/api/offer-feedback"
OFFER_SHIP = "/api/offer-ship"
OFFER_END = "/api/offer-end"
OFFER_REFUND = "/api/offer-refund"


@unique
class Route(Enum):
"""PriceCharting API Routes"""

PRICES = Prices
MARKETPLACE = MarketPlace
103 changes: 103 additions & 0 deletions tests/test_asyncpricecharting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import pytest
from httpx2 import AsyncClient

from pypricecharting.asyncpricecharting import AsyncPyPriceCharting

# Template tests: the API methods are placeholders, so these only check they can be called.
# Replace the `is None` assertions once each method is implemented.

TOKEN = "test-token"


async def test_init_stores_credential():
client = AsyncPyPriceCharting(TOKEN)
assert client.credential == TOKEN
await client.aclose()


async def test_init_creates_http_client():
client = AsyncPyPriceCharting(TOKEN)
assert isinstance(client.client, AsyncClient)
assert not client.client.is_closed
await client.aclose()


async def test_aclose_closes_http_client():
client = AsyncPyPriceCharting(TOKEN)
await client.aclose()
assert client.client.is_closed


async def test_async_with_returns_client():
client = AsyncPyPriceCharting(TOKEN)
async with client as entered:
assert entered is client
assert not client.client.is_closed


async def test_async_with_closes_http_client():
async with AsyncPyPriceCharting(TOKEN) as client:
pass
assert client.client.is_closed


async def test_async_with_closes_http_client_on_error():
client = AsyncPyPriceCharting(TOKEN)
with pytest.raises(ValueError, match="boom"):
async with client:
raise ValueError("boom")
assert client.client.is_closed


async def test_product():
client = AsyncPyPriceCharting(TOKEN)
assert await client.product() is None
await client.aclose()


async def test_products():
client = AsyncPyPriceCharting(TOKEN)
assert await client.products() is None
await client.aclose()


async def test_offers():
client = AsyncPyPriceCharting(TOKEN)
assert await client.offers() is None
await client.aclose()


async def test_offer_details():
client = AsyncPyPriceCharting(TOKEN)
assert await client.offer_details() is None
await client.aclose()


async def test_offer_publish():
client = AsyncPyPriceCharting(TOKEN)
assert await client.offer_publish() is None
await client.aclose()


async def test_offer_feedback():
client = AsyncPyPriceCharting(TOKEN)
assert await client.offer_feedback() is None
await client.aclose()


async def test_offer_ship():
client = AsyncPyPriceCharting(TOKEN)
assert await client.offer_ship() is None
await client.aclose()


async def test_offer_end():
client = AsyncPyPriceCharting(TOKEN)
assert await client.offer_end() is None
await client.aclose()


async def test_offer_refund():
client = AsyncPyPriceCharting(TOKEN)
assert await client.offer_refund() is None
await client.aclose()
102 changes: 102 additions & 0 deletions tests/test_pricecharting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import pytest
from httpx2 import Client

from pypricecharting.pypricecharting import PyPriceCharting

# Template tests: the API methods are placeholders, so these only check they can be called.
# Replace the `is None` assertions once each method is implemented.

TOKEN = "test-token"


def test_init_stores_credential():
client = PyPriceCharting(TOKEN)
assert client.credential == TOKEN
client.close()


def test_init_creates_http_client():
client = PyPriceCharting(TOKEN)
assert isinstance(client.client, Client)
assert not client.client.is_closed
client.close()


def test_close_closes_http_client():
client = PyPriceCharting(TOKEN)
client.close()
assert client.client.is_closed


def test_with_returns_client():
client = PyPriceCharting(TOKEN)
with client as entered:
assert entered is client
assert not client.client.is_closed


def test_with_closes_http_client():
with PyPriceCharting(TOKEN) as client:
pass
assert client.client.is_closed


def test_with_closes_http_client_on_error():
client = PyPriceCharting(TOKEN)
with pytest.raises(ValueError, match="boom"), client:
raise ValueError("boom")
assert client.client.is_closed


def test_product():
client = PyPriceCharting(TOKEN)
assert client.product() is None
client.close()


def test_products():
client = PyPriceCharting(TOKEN)
assert client.products() is None
client.close()


def test_offers():
client = PyPriceCharting(TOKEN)
assert client.offers() is None
client.close()


def test_offer_details():
client = PyPriceCharting(TOKEN)
assert client.offer_details() is None
client.close()


def test_offer_publish():
client = PyPriceCharting(TOKEN)
assert client.offer_publish() is None
client.close()


def test_offer_feedback():
client = PyPriceCharting(TOKEN)
assert client.offer_feedback() is None
client.close()


def test_offer_ship():
client = PyPriceCharting(TOKEN)
assert client.offer_ship() is None
client.close()


def test_offer_end():
client = PyPriceCharting(TOKEN)
assert client.offer_end() is None
client.close()


def test_offer_refund():
client = PyPriceCharting(TOKEN)
assert client.offer_refund() is None
client.close()
Loading
Loading