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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 0 additions & 19 deletions bruno/PricechartingAPI/Marketplace API/offer-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 25 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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`:
Expand Down
17 changes: 0 additions & 17 deletions open_api/pricecharting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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."
Expand Down
26 changes: 21 additions & 5 deletions src/pypricecharting/asyncpricecharting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Expand Down Expand Up @@ -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."""
Expand Down
26 changes: 21 additions & 5 deletions src/pypricecharting/pricecharting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Expand Down Expand Up @@ -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."""
Expand Down
68 changes: 61 additions & 7 deletions tests/test_asyncpricecharting_marketplace.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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():
Expand Down
64 changes: 57 additions & 7 deletions tests/test_pricecharting_marketplace.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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():
Expand Down
Loading