kwork-api/docs/examples.md
2026-03-23 03:26:21 +00:00

213 lines
4.2 KiB
Markdown

# Usage Examples
## Catalog
### Get Catalog List
```python
from kwork_api import KworkClient
async with KworkClient(token="token") as client:
catalog = await client.catalog.get_list(page=1, category_id=5)
for kwork in catalog.kworks:
print(f"{kwork.title}: {kwork.price} RUB")
# Pagination
if catalog.pagination:
print(f"Page {catalog.pagination.current_page} of {catalog.pagination.total_pages}")
```
### Get Kwork Details
```python
details = await client.catalog.get_details(kwork_id=123)
print(f"Title: {details.title}")
print(f"Price: {details.price}")
print(f"Description: {details.full_description}")
print(f"Delivery: {details.delivery_time} days")
```
## Projects
### Get Projects List
```python
projects = await client.projects.get_list(page=1)
for project in projects.projects:
print(f"{project.title} - {project.budget} RUB")
```
### Get Customer Orders
```python
orders = await client.projects.get_payer_orders()
for order in orders:
print(f"Order #{order.id}: {order.status}")
```
### Get Performer Orders
```python
orders = await client.projects.get_worker_orders()
for order in orders:
print(f"Work #{order.id}: {order.status}")
```
## User
### Get User Info
```python
user_info = await client.user.get_info()
print(f"Username: {user_info.get('username')}")
```
### Get Reviews
```python
reviews = await client.user.get_reviews(page=1)
for review in reviews.reviews:
print(f"Rating: {review.rating}/5 - {review.comment}")
```
### Get Favorite Kworks
```python
favorites = await client.user.get_favorite_kworks()
for kwork in favorites:
print(f"Favorite: {kwork.title}")
```
## Reference Data
### Get Cities
```python
cities = await client.reference.get_cities()
for city in cities:
print(f"{city.id}: {city.name}")
```
### Get Countries
```python
countries = await client.reference.get_countries()
for country in countries:
print(f"{country.id}: {country.name}")
```
### Get Timezones
```python
timezones = await client.reference.get_timezones()
for tz in timezones:
print(f"{tz.id}: {tz.name} ({tz.offset})")
```
## Notifications
### Get Notifications
```python
notifications = await client.notifications.get_list()
for notif in notifications.notifications:
print(f"{notif.title}: {notif.message}")
print(f"Unread: {notifications.unread_count}")
```
### Fetch New Notifications
```python
new_notifications = await client.notifications.fetch()
print(f"New: {len(new_notifications.notifications)}")
```
### Get Dialogs
```python
dialogs = await client.notifications.get_dialogs()
for dialog in dialogs:
print(f"Dialog with {dialog.participant.username}: {dialog.last_message}")
```
## Error Handling
```python
from kwork_api import KworkAuthError, KworkApiError, KworkNotFoundError
try:
catalog = await client.catalog.get_list()
except KworkAuthError as e:
print(f"Authentication failed: {e}")
except KworkNotFoundError as e:
print(f"Resource not found: {e}")
except KworkApiError as e:
print(f"API error [{e.status_code}]: {e.message}")
except Exception as e:
print(f"Unexpected error: {e}")
```
## Rate Limiting
```python
import asyncio
async def fetch_all_pages():
all_kworks = []
for page in range(1, 10):
try:
catalog = await client.catalog.get_list(page=page)
all_kworks.extend(catalog.kworks)
if not catalog.pagination or not catalog.pagination.has_next:
break
# Delay to avoid rate limiting
await asyncio.sleep(1)
except KworkRateLimitError:
print("Rate limited, waiting...")
await asyncio.sleep(5)
return all_kworks
```
## Pagination Helper
```python
async def fetch_all_catalog():
"""Fetch all kworks from catalog with pagination."""
all_kworks = []
page = 1
while True:
catalog = await client.catalog.get_list(page=page)
all_kworks.extend(catalog.kworks)
if not catalog.pagination or not catalog.pagination.has_next:
break
page += 1
await asyncio.sleep(0.5) # Rate limiting
return all_kworks
```
---
*More examples in the API Reference.*