Official Mesa Python SDK
Project description
mesa-sdk
Official Mesa Python SDK.
This is the primary Python SDK for Mesa. It wraps the generated mesa-rest client with ergonomic async resource namespaces, automatic org inference, and git operations powered by dulwich.
Python 3.10+ is required.
Install
pip install mesa-sdk
Quick Start
import asyncio
from mesa_sdk import Mesa
async def main():
async with Mesa(api_key="mk_...") as mesa:
repos = await mesa.repos.list()
print(repos)
asyncio.run(main())
Usage
Org Resolution
The SDK resolves your default organization automatically via /whoami on first use. You can bypass this by passing org to the constructor or overriding per-call:
# Default: org inferred from /whoami (lazy, cached)
mesa = Mesa(api_key="mk_...")
repos = await mesa.repos.list()
# Constructor org bypasses /whoami entirely
mesa = Mesa(api_key="mk_...", org="acme")
repos = await mesa.repos.list()
# Per-call override
repos = await mesa.repos.list(org="other-org")
Repositories
# List
repos = await mesa.repos.list()
# Create
repo = await mesa.repos.create(name="my-repo")
# Get
repo = await mesa.repos.get(repo="my-repo")
# Update
repo = await mesa.repos.update(repo="my-repo", name="renamed")
# Delete
await mesa.repos.delete(repo="my-repo")
Bookmarks
bookmarks = await mesa.bookmarks.list(repo="my-repo")
await mesa.bookmarks.create(repo="my-repo", name="feature-x", change_id="abc123")
await mesa.bookmarks.move(repo="my-repo", bookmark="feature-x", change_id="def456")
await mesa.bookmarks.merge(repo="my-repo", source="feature-x", target="main")
await mesa.bookmarks.delete(repo="my-repo", bookmark="feature-x")
Changes
from mesa_sdk import Author, FileUpsert
changes = await mesa.changes.list(repo="my-repo")
change = await mesa.changes.create(
repo="my-repo",
base_change_id="abc123",
message="Add feature",
author=Author(name="Alice", email="alice@example.com"),
files=[FileUpsert(path="hello.txt", content="Hello, world!")],
)
change = await mesa.changes.get(repo="my-repo", change_id="def456")
Content & Diffs
content = await mesa.content.get(repo="my-repo", change_id="abc123")
diff = await mesa.diffs.get(
repo="my-repo",
base_change_id="abc123",
head_change_id="def456",
)
API Keys
keys = await mesa.api_keys.list()
key = await mesa.api_keys.create(name="ci-key", scopes=["read", "write"])
await mesa.api_keys.revoke(key_id=key.id)
Webhooks
webhooks = await mesa.webhooks.list()
webhook = await mesa.webhooks.create(url="https://example.com/hook", events=["change.created"])
await mesa.webhooks.delete(webhook_id=webhook.id)
Git Operations
The SDK includes a git client powered by dulwich (pure Python git):
await mesa.git.clone("https://mesa.dev/acme/my-repo.git", "/tmp/my-repo", branch="main")
await mesa.git.add("/tmp/my-repo", ["."])
result = await mesa.git.commit(
"/tmp/my-repo",
message="Initial commit",
author="Alice",
email="alice@example.com",
)
print(result.sha)
status = await mesa.git.status("/tmp/my-repo")
print(status.current_branch, status.file_status)
branches = await mesa.git.branches("/tmp/my-repo")
await mesa.git.create_branch("/tmp/my-repo", "feature-x")
await mesa.git.checkout_branch("/tmp/my-repo", "feature-x")
await mesa.git.delete_branch("/tmp/my-repo", "feature-x")
await mesa.git.push("/tmp/my-repo", username="token", password="mk_...")
await mesa.git.pull("/tmp/my-repo")
Raw Client Access
For operations not covered by the resource namespaces, you can use the underlying mesa-rest AuthenticatedClient directly:
from mesa_rest.api.repo import list_repos
response = await list_repos.asyncio_detailed("acme", client=mesa.raw)
Configuration
Mesa accepts the following keyword arguments:
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key |
str | None |
MESA_API_KEY env var |
API key for authentication |
api_url |
str |
https://api.mesa.dev/v1 |
Base URL for the Mesa API |
org |
str | None |
Resolved from /whoami |
Default organization slug |
user_agent |
str | None |
None |
Custom user agent suffix |
Error Handling
The SDK raises typed exceptions for API errors:
from mesa_sdk import Mesa, NotFoundError, AuthenticationError
async with Mesa() as mesa:
try:
repo = await mesa.repos.get(repo="nonexistent")
except NotFoundError:
print("Repo not found")
except AuthenticationError:
print("Invalid API key")
| Exception | HTTP Status | Description |
|---|---|---|
ValidationError |
400, 406 | Invalid request parameters |
AuthenticationError |
401 | Invalid or missing API key |
AuthorizationError |
403 | Insufficient permissions |
NotFoundError |
404 | Resource not found |
ConflictError |
409 | Resource conflict |
RateLimitError |
429 | Rate limit exceeded |
ServerError |
5xx | Server-side error |
All API exceptions inherit from ApiError, which inherits from MesaError.
Package Relationship
mesa-sdkis the ergonomic, main SDK.mesa-restis the generated REST client used under the hood.
Use mesa.raw when you need direct access to the generated AuthenticatedClient.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
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 mesa_sdk-0.22.0-py3-none-any.whl.
File metadata
- Download URL: mesa_sdk-0.22.0-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b0647cad3bdcd5038641a80645e22b41875bb12d126fbd560cb2cfce5f781da8
|
|
| MD5 |
72e2f4d23b3655cac10bff243d0a4bdb
|
|
| BLAKE2b-256 |
2db93bb5febf60431d23805aebb211ccf56a4f81771d0497d7c3cbeff17009b5
|