From fa189f9afe2448b21b1d3dc9d22c1bff8b750be0 Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcodedev@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:35:00 -0800 Subject: [PATCH] Marketplace API - Offers Publish endpoint --- README.md | 5 +- .../Marketplace API/offer-publish.yml | 19 ------ docs/index.md | 27 +++++++- open_api/pricecharting.yaml | 17 ----- src/pypricecharting/asyncpricecharting.py | 26 +++++-- src/pypricecharting/pricecharting.py | 26 +++++-- tests/test_asyncpricecharting_marketplace.py | 68 +++++++++++++++++-- tests/test_pricecharting_marketplace.py | 64 +++++++++++++++-- 8 files changed, 188 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 20d7ef0..3642704 100644 --- a/README.md +++ b/README.md @@ -72,8 +72,9 @@ See [RELEASE.md](RELEASE.md) for how releases and release labels work. ## Usage -Under development: the Prices API methods (`product`, `products`) and the Marketplace `offers` -and `offer_details` methods are implemented; the other Marketplace API methods are not yet. +Under development: the Prices API methods (`product`, `products`) and the Marketplace `offers`, +`offer_details`, and `offer_publish` methods are implemented; the other Marketplace API methods +are not yet. ```python from pypricecharting import AsyncPriceCharting, PriceCharting diff --git a/bruno/PricechartingAPI/Marketplace API/offer-publish.yml b/bruno/PricechartingAPI/Marketplace API/offer-publish.yml index 2a85055..a84b541 100644 --- a/bruno/PricechartingAPI/Marketplace API/offer-publish.yml +++ b/bruno/PricechartingAPI/Marketplace API/offer-publish.yml @@ -80,25 +80,6 @@ http: - name: writing value: "on" disabled: true - - name: photo1 - value: "" - description: JPG or PNG URL (photo1..photo6) - disabled: true - - name: photo2 - value: "" - disabled: true - - name: photo3 - value: "" - disabled: true - - name: photo4 - value: "" - disabled: true - - name: photo5 - value: "" - disabled: true - - name: photo6 - value: "" - disabled: true auth: none settings: diff --git a/docs/index.md b/docs/index.md index 378af3f..c0a55f3 100644 --- a/docs/index.md +++ b/docs/index.md @@ -5,8 +5,9 @@ Python client for the [PriceCharting API](https://www.pricecharting.com/api-docu It provides a synchronous client, `PriceCharting`, and an asynchronous client, `AsyncPriceCharting`, both authenticated with a 40-character API token. -Under development: the Prices API methods (`product`, `products`) and the Marketplace `offers` -and `offer_details` methods are implemented; the other Marketplace API methods are not yet. +Under development: the Prices API methods (`product`, `products`) and the Marketplace `offers`, +`offer_details`, and `offer_publish` methods are implemented; the other Marketplace API methods +are not yet. ## Authentication @@ -100,6 +101,28 @@ with PriceCharting() as client: An empty or whitespace-only offer ID raises `ParameterError`. +## Publishing an offer + +`offer_publish` lists an item for sale. It takes an `OfferPublishRequestParams`, which needs +`price_max` (in pennies), `condition_id`, and one identifier (`product`, `upc`, `asin`, or `epid`): + +```python +from pypricecharting import Condition, OfferPublishRequestParams, PriceCharting, State + +with PriceCharting() as client: + params = OfferPublishRequestParams( + product="9331", + price_max=901, + condition_id=Condition.VideoGames.LOOSE, + scratch=State.ON, + ) + offer = client.offer_publish(params) + print(offer.offer_id, offer.offer_url, offer.price) +``` + +Set `offer_id` instead of a product identifier to edit an existing offer. Set `price_min` to make +the offer a reverse auction that drops from `price_max` over 30 days. + ## Errors All errors subclass `PriceChartingError`: diff --git a/open_api/pricecharting.yaml b/open_api/pricecharting.yaml index 31d2cce..c8ec71c 100644 --- a/open_api/pricecharting.yaml +++ b/open_api/pricecharting.yaml @@ -384,18 +384,6 @@ paths: $ref: "#/components/schemas/On" writing: $ref: "#/components/schemas/On" - photo1: - $ref: "#/components/schemas/PhotoUrl" - photo2: - $ref: "#/components/schemas/PhotoUrl" - photo3: - $ref: "#/components/schemas/PhotoUrl" - photo4: - $ref: "#/components/schemas/PhotoUrl" - photo5: - $ref: "#/components/schemas/PhotoUrl" - photo6: - $ref: "#/components/schemas/PhotoUrl" responses: "200": description: The published offer. @@ -598,11 +586,6 @@ components: type: string enum: ["on"] - PhotoUrl: - type: string - format: uri - description: JPG or PNG photo URL. - Date: type: string description: "`YYYY-MM-DD`. `0001-01-01` means not set." diff --git a/src/pypricecharting/asyncpricecharting.py b/src/pypricecharting/asyncpricecharting.py index 2ac032f..99a18b1 100644 --- a/src/pypricecharting/asyncpricecharting.py +++ b/src/pypricecharting/asyncpricecharting.py @@ -8,9 +8,10 @@ from pypricecharting._auth import _REDACT_FILTER, _TokenAuth from pypricecharting.error import ParameterError -from pypricecharting.requests import OffersRequestParams +from pypricecharting.requests import OfferPublishRequestParams, OffersRequestParams from pypricecharting.responses import ( OfferDetailsResponse, + OfferPublishResponse, OffersResponse, ProductResponse, ProductsResponse, @@ -23,7 +24,7 @@ class AsyncPriceCharting: """Asynchronous PriceCharting API client. Reuse one client for all requests and close it when done (`async with` or `aclose()`). The Marketplace - methods other than `offers` and `offer_details` are not implemented yet and return `None`. + methods other than `offers`, `offer_details`, and `offer_publish` are not implemented yet and return `None`. """ TOKEN_KEY = "PRICECHARTING_TOKEN" @@ -178,9 +179,24 @@ async def offer_details(self, offer_id: str) -> OfferDetailsResponse: ) return _parse(response, OfferDetailsResponse) - async def offer_publish(self): - """Publish a new marketplace offer.""" - pass + async def offer_publish(self, params: OfferPublishRequestParams) -> OfferPublishResponse: + """Publish a new marketplace offer, or edit an existing one. + + Args: + params: The offer to publish. Set `offer_id` to edit an existing offer. + + Returns: + The published offer. + + Raises: + APIError: If the API returns an error. + + """ + response = await self._client.post( + Route.Marketplace.OFFER_PUBLISH, + data=params.model_dump(mode="json", by_alias=True, exclude_none=True), + ) + return _parse(response, OfferPublishResponse) async def offer_feedback(self): """Leave feedback on a marketplace offer.""" diff --git a/src/pypricecharting/pricecharting.py b/src/pypricecharting/pricecharting.py index b414572..b59e358 100644 --- a/src/pypricecharting/pricecharting.py +++ b/src/pypricecharting/pricecharting.py @@ -8,9 +8,10 @@ from pypricecharting._auth import _REDACT_FILTER, _TokenAuth from pypricecharting.error import ParameterError -from pypricecharting.requests import OffersRequestParams +from pypricecharting.requests import OfferPublishRequestParams, OffersRequestParams from pypricecharting.responses import ( OfferDetailsResponse, + OfferPublishResponse, OffersResponse, ProductResponse, ProductsResponse, @@ -23,7 +24,7 @@ class PriceCharting: """Synchronous PriceCharting API client. Reuse one client for all requests and close it when done (`with` or `close()`). The Marketplace - methods other than `offers` and `offer_details` are not implemented yet and return `None`. + methods other than `offers`, `offer_details`, and `offer_publish` are not implemented yet and return `None`. """ TOKEN_KEY = "PRICECHARTING_TOKEN" @@ -175,9 +176,24 @@ def offer_details(self, offer_id: str) -> OfferDetailsResponse: response = self._client.get(Route.Marketplace.OFFER_DETAILS, params={"offer-id": offer_id}) return _parse(response, OfferDetailsResponse) - def offer_publish(self): - """Publish a new marketplace offer.""" - pass + def offer_publish(self, params: OfferPublishRequestParams) -> OfferPublishResponse: + """Publish a new marketplace offer, or edit an existing one. + + Args: + params: The offer to publish. Set `offer_id` to edit an existing offer. + + Returns: + The published offer. + + Raises: + APIError: If the API returns an error. + + """ + response = self._client.post( + Route.Marketplace.OFFER_PUBLISH, + data=params.model_dump(mode="json", by_alias=True, exclude_none=True), + ) + return _parse(response, OfferPublishResponse) def offer_feedback(self): """Leave feedback on a marketplace offer.""" diff --git a/tests/test_asyncpricecharting_marketplace.py b/tests/test_asyncpricecharting_marketplace.py index 690971e..2ed8dc3 100644 --- a/tests/test_asyncpricecharting_marketplace.py +++ b/tests/test_asyncpricecharting_marketplace.py @@ -1,12 +1,20 @@ +from urllib.parse import parse_qsl + import pytest from httpx2 import Request, Response from pypricecharting.asyncpricecharting import AsyncPriceCharting from pypricecharting.error import APIError, ParameterError -from pypricecharting.requests import OffersRequestParams, OffersStatus -from pypricecharting.responses import OfferDetailsResponse, OffersResponse -from pypricecharting.types import Status -from tests.mocks.json import OFFER_DETAILS_JSON, OFFERS_JSON, OFFERS_REQUEST_JSON +from pypricecharting.requests import OfferPublishRequestParams, OffersRequestParams, OffersStatus +from pypricecharting.responses import OfferDetailsResponse, OfferPublishResponse, OffersResponse +from pypricecharting.types import Condition, Status +from tests.mocks.json import ( + OFFER_DETAILS_JSON, + OFFER_PUBLISH_REQUEST_JSON, + OFFER_PUBLISH_RESPONSE_JSON, + OFFERS_JSON, + OFFERS_REQUEST_JSON, +) from tests.mocks.mock_pricecharting import TOKEN, error_body, mocked_async_client # Template tests: methods that are still placeholders only check they can be called. @@ -106,9 +114,55 @@ async def test_offer_details_raises_api_error(): async def test_offer_publish(): - client = AsyncPriceCharting(TOKEN) - assert await client.offer_publish() is None - await client.aclose() + requests: list[Request] = [] + params = OfferPublishRequestParams.model_validate(OFFER_PUBLISH_REQUEST_JSON) + + async with mocked_async_client( + TOKEN, Response(200, json=OFFER_PUBLISH_RESPONSE_JSON), requests + ) as client: + response = await client.offer_publish(params) + + assert isinstance(response, OfferPublishResponse) + assert response.status == Status.SUCCESS + assert response.offer_id == "vtdsww72uyslgooawzyyijsllm" + assert response.price == 2500 + + assert len(requests) == 1 + assert requests[0].method == "POST" + assert requests[0].url.path == "/api/offer-publish" + assert "t" not in requests[0].url.params + assert requests[0].headers["Content-Type"] == "application/x-www-form-urlencoded" + expected = {key: str(value) for key, value in OFFER_PUBLISH_REQUEST_JSON.items()} + assert dict(parse_qsl(requests[0].content.decode())) == {**expected, "t": TOKEN} + + +async def test_offer_publish_sends_only_set_parameters(): + requests: list[Request] = [] + params = OfferPublishRequestParams( + offer_id="testofferid", price_max=901, condition_id=Condition.VideoGames.LOOSE + ) + + async with mocked_async_client( + TOKEN, Response(200, json=OFFER_PUBLISH_RESPONSE_JSON), requests + ) as client: + await client.offer_publish(params) + + assert dict(parse_qsl(requests[0].content.decode())) == { + "offer-id": "testofferid", + "price-max": "901", + "condition-id": "1", + "t": TOKEN, + } + + +async def test_offer_publish_raises_api_error(): + requests: list[Request] = [] + params = OfferPublishRequestParams(product="0", price_max=901, condition_id=1) + async with mocked_async_client( + TOKEN, Response(400, json=error_body("No such product")), requests + ) as client: + with pytest.raises(APIError, match="400: No such product"): + await client.offer_publish(params) async def test_offer_feedback(): diff --git a/tests/test_pricecharting_marketplace.py b/tests/test_pricecharting_marketplace.py index 39a1732..ec3874d 100644 --- a/tests/test_pricecharting_marketplace.py +++ b/tests/test_pricecharting_marketplace.py @@ -1,12 +1,20 @@ +from urllib.parse import parse_qsl + import pytest from httpx2 import Request, Response from pypricecharting.error import APIError, ParameterError from pypricecharting.pricecharting import PriceCharting -from pypricecharting.requests import OffersRequestParams, OffersStatus -from pypricecharting.responses import OfferDetailsResponse, OffersResponse -from pypricecharting.types import Status -from tests.mocks.json import OFFER_DETAILS_JSON, OFFERS_JSON, OFFERS_REQUEST_JSON +from pypricecharting.requests import OfferPublishRequestParams, OffersRequestParams, OffersStatus +from pypricecharting.responses import OfferDetailsResponse, OfferPublishResponse, OffersResponse +from pypricecharting.types import Condition, Status +from tests.mocks.json import ( + OFFER_DETAILS_JSON, + OFFER_PUBLISH_REQUEST_JSON, + OFFER_PUBLISH_RESPONSE_JSON, + OFFERS_JSON, + OFFERS_REQUEST_JSON, +) from tests.mocks.mock_pricecharting import TOKEN, error_body, mocked_client # Template tests: methods that are still placeholders only check they can be called. @@ -102,9 +110,51 @@ def test_offer_details_raises_api_error(): def test_offer_publish(): - client = PriceCharting(TOKEN) - assert client.offer_publish() is None - client.close() + requests: list[Request] = [] + params = OfferPublishRequestParams.model_validate(OFFER_PUBLISH_REQUEST_JSON) + + with mocked_client(TOKEN, Response(200, json=OFFER_PUBLISH_RESPONSE_JSON), requests) as client: + response = client.offer_publish(params) + + assert isinstance(response, OfferPublishResponse) + assert response.status == Status.SUCCESS + assert response.offer_id == "vtdsww72uyslgooawzyyijsllm" + assert response.price == 2500 + + assert len(requests) == 1 + assert requests[0].method == "POST" + assert requests[0].url.path == "/api/offer-publish" + assert "t" not in requests[0].url.params + assert requests[0].headers["Content-Type"] == "application/x-www-form-urlencoded" + expected = {key: str(value) for key, value in OFFER_PUBLISH_REQUEST_JSON.items()} + assert dict(parse_qsl(requests[0].content.decode())) == {**expected, "t": TOKEN} + + +def test_offer_publish_sends_only_set_parameters(): + requests: list[Request] = [] + params = OfferPublishRequestParams( + offer_id="testofferid", price_max=901, condition_id=Condition.VideoGames.LOOSE + ) + + with mocked_client(TOKEN, Response(200, json=OFFER_PUBLISH_RESPONSE_JSON), requests) as client: + client.offer_publish(params) + + assert dict(parse_qsl(requests[0].content.decode())) == { + "offer-id": "testofferid", + "price-max": "901", + "condition-id": "1", + "t": TOKEN, + } + + +def test_offer_publish_raises_api_error(): + requests: list[Request] = [] + params = OfferPublishRequestParams(product="0", price_max=901, condition_id=1) + with ( + mocked_client(TOKEN, Response(400, json=error_body("No such product")), requests) as client, + pytest.raises(APIError, match="400: No such product"), + ): + client.offer_publish(params) def test_offer_feedback():