mirror of
https://github.com/hanzoai/papers.git
synced 2026-10-22 17:20:17 +00:00
89 lines
1.7 KiB
TeX
89 lines
1.7 KiB
TeX
\section{Python SDK}
|
|
|
|
|
|
\subsection{Package Structure}
|
|
|
|
\begin{lstlisting}[caption=Python SDK Structure]
|
|
hanzo-python/
|
|
hanzo/
|
|
__init__.py
|
|
client.py
|
|
resources/
|
|
__init__.py
|
|
products.py
|
|
orders.py
|
|
models/
|
|
__init__.py
|
|
product.py
|
|
errors.py
|
|
utils/
|
|
pagination.py
|
|
webhooks.py
|
|
setup.py
|
|
pyproject.toml
|
|
\end{lstlisting}
|
|
|
|
\subsection{Client Implementation}
|
|
|
|
\begin{lstlisting}[language=Python, caption=Python Client]
|
|
import hanzo
|
|
|
|
client = hanzo.Client('sk_live_...')
|
|
|
|
# List products
|
|
products = client.products.list(limit=10)
|
|
for product in products:
|
|
print(product.name)
|
|
|
|
# Create a product
|
|
product = client.products.create(
|
|
name='Widget',
|
|
price=1999,
|
|
description='A useful widget',
|
|
)
|
|
|
|
# Async support
|
|
import asyncio
|
|
|
|
async def main():
|
|
async_client = hanzo.AsyncClient('sk_live_...')
|
|
products = await async_client.products.list()
|
|
async for product in products:
|
|
print(product.name)
|
|
|
|
asyncio.run(main())
|
|
\end{lstlisting}
|
|
|
|
\subsection{Type Annotations}
|
|
|
|
\begin{lstlisting}[language=Python, caption=Python Type Annotations]
|
|
from dataclasses import dataclass
|
|
from typing import Optional, List
|
|
from datetime import datetime
|
|
|
|
@dataclass
|
|
class Product:
|
|
id: str
|
|
name: str
|
|
price: int
|
|
description: Optional[str]
|
|
variants: List['Variant']
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
@dataclass
|
|
class ProductCreateParams:
|
|
name: str
|
|
price: int
|
|
description: Optional[str] = None
|
|
\end{lstlisting}
|
|
|
|
\subsection{Context Managers}
|
|
|
|
\begin{lstlisting}[language=Python, caption=Python Context Manager]
|
|
with hanzo.Client('sk_live_...') as client:
|
|
product = client.products.retrieve('prod_123')
|
|
# Connection automatically closed
|
|
\end{lstlisting}
|
|
|