Skip to content

Commit

Permalink
write better docs, fix exception handling errors
Browse files Browse the repository at this point in the history
  • Loading branch information
CralixRaev committed Jan 8, 2024
1 parent 0a3cc4f commit 273b11f
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 49 deletions.
70 changes: 67 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,73 @@

Easy way to communicate with russian marketplaces!

## Currently supported apis:
## Features:
* Fully async
* Uses httpx and mashumaro - blazing fast!
* Easy and not verbose to use
* Supports Ozon SellerAPI and Yandex MarketAPI - support for Wildberries API planned

#### OZON Seller api
## Usage example
### Ozon SellerAPI

```python
import asyncio
from datetime import datetime, timedelta
from marketplace_apis.ozon.seller_api import SellerApi
import os


async def main():
async with SellerApi(os.getenv("API_KEY"), os.getenv("CLIENT_ID")) as client:
# print all postings from 14 days before now to now:
now = datetime.now()
postings = await client.posting.list_postings(
filter_since=now - timedelta(14),
filter_to=now)
print(postings)
# get product infos and attributes from first posting products concurrently
async with asyncio.TaskGroup() as tg:
posting = postings[0]
offer_ids = [product.offer_id for product in posting.products]
info = tg.create_task(client.product.list_info(["112026854"]))
attributes = tg.create_task(
client.product.list_attributes(offer_id=["112026854"]))
print(info.result(), attributes.result())

asyncio.run(main())
```
### Yandex MarketAPI

```python
import asyncio
from datetime import datetime, timedelta
from marketplace_apis.yandex.market_api import MarketApi
import os


async def main():
# you don't have to pass campaign_id or business_id,
# if you will not use methods, that require them
async with MarketApi(os.getenv("TOKEN"), os.getenv("CAMPAIGN_ID"),
os.getenv("BUSINESS_ID")) as client:
# print all orders from 14 days before now to now:
now = datetime.now()
orders = await client.order.list_orders(
fromDate=(now - timedelta(14)).date(),
toDate=now.date())
print(orders)
# get offer_mappings of first order items
order = orders[0]
offer_ids = [item.offerId for item in order.items]
offer_mappings = await client.offer_mapping.list_offer_mappings(offerIds=offer_ids)
print(offer_mappings)

asyncio.run(main())
```

## Currently supported api endpoints:

#### OZON SellerAPI

Supports endpoints for:

Expand All @@ -13,7 +77,7 @@ Supports endpoints for:
* posting,
* product.

#### YANDEX MARKET api
#### Yandex MarketAPI

Supports endpoints for:

Expand Down
2 changes: 1 addition & 1 deletion marketplace_apis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

"""Easy way to communicate with russian marketplaces"""

__version__ = "2.0.0"
__version__ = "2.0.1"
40 changes: 21 additions & 19 deletions marketplace_apis/ozon/seller_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SellerApi(AsyncRequester):
async def check_for_errors(func, self, *args, **kwargs):
response_, data = await func(self, *args, **kwargs)
if response_.status_code != HTTPStatus.OK:
raise SellerApiError(response_.json())
raise SellerApiError(response_.status_code, response_.json())
return response_, data

def __init__(
Expand All @@ -66,31 +66,33 @@ def __init__(

if __name__ == "__main__":
from pathlib import Path
import time
import os
import asyncio
from datetime import datetime, timedelta
from marketplace_apis.ozon.seller_api import SellerApi

with Path.open(".env", "r") as f:
for line in f.readlines():
if line:
k, v = line.split("=")
os.environ[k] = v.strip()

async def async_function():
start_time = datetime.now()
start_time_count = time.monotonic()
async with (os.getenv("API_KEY"), os.getenv("CLIENT_ID")) as client:
print( # noqa: T201
await client.posting.list_postings(
filter_since=start_time - timedelta(14),
filter_to=start_time + timedelta(14),
)
async def main():
async with SellerApi(os.getenv("API_KEY"), os.getenv("CLIENT_ID")) as client:
# print all postings from 14 days before now to now:
now = datetime.now()
postings = await client.posting.list_postings(
filter_since=now - timedelta(14), filter_to=now
)
end_time = time.monotonic()
print(timedelta(seconds=end_time - start_time_count)) # noqa: T201

asyncio.run(async_function())
# print(api.posting.label_task_get(45342209))
# [print([attribute.values[0].value for attribute in product.attributes if
# attribute.attribute_id == 4191]) for product in
# api.product.list_attribute()]
print(postings)
# get product infos and attributes from first posting products concurrently
async with asyncio.TaskGroup() as tg:
posting = postings[0]
offer_ids = [product.offer_id for product in posting.products]
info = tg.create_task(client.product.list_info(["112026854"]))
attributes = tg.create_task(
client.product.list_attributes(offer_id=["112026854"])
)
print(info.result(), attributes.result())

asyncio.run(main())
46 changes: 20 additions & 26 deletions marketplace_apis/yandex/market_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

from marketplace_apis.yandex.offer_mapping.methods import OfferMappingMethods
from marketplace_apis.yandex.order.methods import OrderMethods

# from marketplace_apis.yandex.order.methods import OrderMethods
from marketplace_apis.yandex.warehouse.methods import WarehouseMethods


Expand All @@ -48,7 +46,7 @@ class MarketApi(AsyncRequester):
async def check_for_errors(func, self, *args, **kwargs):
response_, data = await func(self, *args, **kwargs)
if response_.status_code != HTTPStatus.OK:
raise MarketApiError(response_.json())
raise MarketApiError(response_.status_code, response_.json())
return response_, data

def build_campaign_url(self, endpoint):
Expand Down Expand Up @@ -86,36 +84,32 @@ def __init__(
if __name__ == "__main__":
from pathlib import Path
import os
import time
from datetime import timedelta
from datetime import timedelta, datetime

with Path.open(".env", "r") as f:
for line in f.readlines():
if line:
k, v = line.split("=")
os.environ[k] = v.strip()

async def async_function():
start_time_count = time.monotonic()
async def main():
# you don't have to pass campaign_id or business_id,
# if you will not use methods, that require them
async with MarketApi(
os.getenv("TOKEN"), os.getenv("CAMPAIGN_ID"), os.getenv("BUSINESS_ID")
) as client:
print(len(await client.offer_mapping.list_offer_mappings())) # noqa: T201
end_time = time.monotonic()
print(timedelta(seconds=end_time - start_time_count)) # noqa: T201

asyncio.run(async_function())
# print(api.warehouse.list_fby_warehouses())
# print(api.warehouse.list_warehouses())
# print(api.order.list_orders(status=OrderStatusType.PROCESSING)[-1])
# print(MarketApi.oauth.get_tokens_by_code(
# os.getenv("CLIENT_ID"),
# os.getenv("CLIENT_SECRET"), 8863298
# ))
# print(
# MarketApi.oauth.get_tokens_by_refresh(
# os.getenv("CLIENT_ID"),
# os.getenv("CLIENT_SECRET"),
# os.getenv("REFRESH_TOKEN"),
# )
# )
# print all orders from 14 days before now to now:
now = datetime.now()
orders = await client.order.list_orders(
fromDate=(now - timedelta(14)).date(), toDate=now.date()
)
print(orders)
# get offer_mappings of first order items
order = orders[0]
offer_ids = [item.offerId for item in order.items]
offer_mappings = await client.offer_mapping.list_offer_mappings(
offerIds=offer_ids
)
print(offer_mappings)

asyncio.run(main())

0 comments on commit 273b11f

Please sign in to comment.