Client API Reference¶
The client module provides functionality for making Spartan protocol requests.
Quick example¶
import asyncio
from teyaotlani import SpartanClient, get
# Simple one-off request
response = asyncio.run(get("spartan://example.com/"))
# Multiple requests with a client
async def main():
async with SpartanClient() as client:
response = await client.get("spartan://example.com/")
print(response.body)
asyncio.run(main())
SpartanClient¶
teyaotlani.SpartanClient ¶
High-level Spartan client with async/await API.
This class provides a simple, high-level interface for making Spartan requests. Handles connection management and timeouts.
Spartan is plaintext by default (no TLS, no TOFU).
Examples:
>>> # Basic usage
>>> async with SpartanClient() as client:
... response = await client.get("spartan://example.com/")
... print(response.body)
>>> # With custom timeout
>>> client = SpartanClient(timeout=60)
>>> response = await client.get("spartan://example.com/")
>>> # Upload content
>>> response = await client.upload(
... "spartan://example.com/file.txt",
... b"Hello, world!"
... )
Initialize the Spartan client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
Request timeout in seconds. |
30.0
|
Source code in src/teyaotlani/client/session.py
Functions¶
__aenter__
async
¶
__aexit__
async
¶
get
async
¶
Get a Spartan resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The Spartan URL to get. |
required |
Returns:
| Type | Description |
|---|---|
SpartanResponse
|
A SpartanResponse with status, meta, and optional body. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the URL is invalid. |
TimeoutError
|
If the request times out. |
ConnectionError
|
If the connection fails. |
Examples:
>>> response = await client.get("spartan://example.com/")
>>> if response.is_success():
... print(response.body)
Source code in src/teyaotlani/client/session.py
upload
async
¶
Upload content to a Spartan server.
Spartan integrates upload into the core protocol (content-length > 0 in the request).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The target URL. |
required |
content
|
bytes | str
|
The content to upload. |
required |
Returns:
| Type | Description |
|---|---|
SpartanResponse
|
A SpartanResponse indicating success or failure. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the URL is invalid. |
TimeoutError
|
If the request times out. |
ConnectionError
|
If the connection fails. |
Examples:
Source code in src/teyaotlani/client/session.py
query
async
¶
Send a query to a Spartan server.
In Spartan, queries are sent as the data block (like uploads).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The target URL. |
required |
query
|
str
|
The query string. |
required |
Returns:
| Type | Description |
|---|---|
SpartanResponse
|
A SpartanResponse. |
Examples:
Source code in src/teyaotlani/client/session.py
Convenience Functions¶
get¶
teyaotlani.get
async
¶
Convenience function to get a Spartan URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The Spartan URL to get. |
required |
timeout
|
float
|
Request timeout in seconds. |
30.0
|
Returns:
| Type | Description |
|---|---|
SpartanResponse
|
A SpartanResponse. |
Examples:
Source code in src/teyaotlani/client/session.py
upload¶
teyaotlani.upload
async
¶
Convenience function to upload content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The target URL. |
required |
content
|
bytes | str
|
The content to upload. |
required |
timeout
|
float
|
Request timeout in seconds. |
30.0
|
Returns:
| Type | Description |
|---|---|
SpartanResponse
|
A SpartanResponse. |