Python SDK for working with TargetAI tokens
Project description
TargetAI Python SDK
Python SDK for working with TargetAI tokens. Provides simple tools for retrieving tokens from TOS backend and deploying token distribution endpoints.
Installation
pip install targetai
Quick Start
TargetAITokenClient - Token Retrieval
from targetai import TargetAITokenClient
# Simplest way - uses default URL https://app.targetai.ai
async def example_simple():
async with TargetAITokenClient() as client:
token_response = await client.get_token()
print(f"Token: {token_response.token}")
# With API key
async def example_with_api_key():
async with TargetAITokenClient(api_key="your-api-key") as client:
token_response = await client.get_token()
print(f"Token: {token_response.token}")
# With custom URL
async def example_custom_url():
async with TargetAITokenClient("https://your-tos.example.com", api_key="your-api-key") as client:
token_response = await client.get_token()
print(f"Token: {token_response.token}")
TargetAITokenServer - Token Distribution Server
from targetai import TargetAITokenServer
# Simplest startup (uses default URL)
def run_server():
server = TargetAITokenServer(port=8001)
server.run() # Blocking startup
# With API key for TOS backend
def run_server_with_key():
server = TargetAITokenServer(api_key="your-tos-api-key", port=8001)
server.run()
# With custom TOS backend URL
def run_server_custom():
server = TargetAITokenServer(
tos_base_url="https://your-tos.example.com",
api_key="your-api-key",
port=8001
)
server.run()
# Or asynchronously
async def run_server_async():
async with TargetAITokenServer(port=8001) as server:
await server.start()
After starting the server, the following endpoints are available:
POST /token- token retrievalGET /health- health checkGET /docs- Swagger documentation
Using the /token endpoint
# Simple token request
curl -X POST http://localhost:8001/token
# Response:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
API Reference
TargetAITokenClient
class TargetAITokenClient:
def __init__(self,
tos_base_url: str = "https://app.targetai.ai",
api_key: Optional[str] = None)
async def get_token(self) -> TokenResponse
async def close(self)
TargetAITokenServer
class TargetAITokenServer:
def __init__(self,
tos_base_url: str = "https://app.targetai.ai",
api_key: Optional[str] = None,
host: str = "0.0.0.0",
port: int = 8001)
async def start(self)
async def stop(self)
def run(self) # synchronous version
Data Schemas
class TokenResponse:
token: str
Error Handling
from targetai import TargetAITokenClient, TargetAITokenClientError
try:
async with TargetAITokenClient() as client: # Uses default URL
token = await client.get_token()
except TargetAITokenClientError as e:
print(f"Error: {e}")
Possible errors:
TargetAITokenClientError- base client errorTargetAITokenServerError- base server error
Requirements
- Python 3.8+
- aiohttp >= 3.8.0
- pydantic >= 2.0.0
- fastapi >= 0.100.0
- uvicorn >= 0.20.0
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
targetai-0.0.2.tar.gz
(6.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file targetai-0.0.2.tar.gz.
File metadata
- Download URL: targetai-0.0.2.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6dee64edd4fb1a48cde27c8456a271379cc57249ce8d1599212a1d3c629931d9
|
|
| MD5 |
1794a1818dc1cd5fb74dfb55c6ff4da3
|
|
| BLAKE2b-256 |
bf4fc76e895a7201c96fcb49588139582826897ff706e5e347e3fa720274ce39
|
File details
Details for the file targetai-0.0.2-py3-none-any.whl.
File metadata
- Download URL: targetai-0.0.2-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
647eafd8de78b0820b10a9909e453a2dda6c25207df09bac162d92e6171b76c3
|
|
| MD5 |
95da6f1ed13b7f298efbca18c8988064
|
|
| BLAKE2b-256 |
0f8f6c03049ca91b6e953621f51999cfb2964260e2bf0c6318a34b8f4d3bc4fd
|