Metadata-Version: 2.5
Name: neocrew-connector
Version: 0.2.0
Summary: Python SDK for Crewlink: connect accounts, call provider APIs, and manage secrets with sync and async clients.
Project-URL: Homepage, https://github.com/WowLabz/crewlink
Project-URL: Source, https://github.com/WowLabz/crewlink/tree/main/packages/connector-py
Author: Neocrew AI
License-Expression: MIT
License-File: LICENSE
Keywords: api,connectors,crewlink,integrations,neocrew,oauth,sdk
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Typing :: Typed
Requires-Python: >=3.9
Requires-Dist: anyio>=3.0
Requires-Dist: httpx>=0.21.2
Requires-Dist: pydantic>=2.0
Requires-Dist: typing-extensions>=4.0.0
Description-Content-Type: text/markdown

# neocrew-connector

Python SDK for the Crewlink integration API. Connect user accounts, call provider
APIs, manage secrets, and issue principal tokens from your backend. Includes
typed synchronous and asynchronous clients.

## Install

Requires Python 3.9 or newer.

```sh
pip install neocrew-connector
```

## Quick start

Set `CREWLINK_API_KEY` and `CREWLINK_BASE_URL` in your server environment.
Use your Crewlink API URL and a key with `connections:read`.

```python
import os
from neocrew_connector import Crewlink

client = Crewlink(
    token=os.environ["CREWLINK_API_KEY"],
    base_url=os.environ["CREWLINK_BASE_URL"],
    max_retries=0,
    timeout=30.0,
)
page = client.connections.list(limit=20)
for connection in page.data:
    print(connection.id, connection.status)
```

The constructor also reads these environment variables if the arguments are
omitted. Set the URL for production; its fallback is `http://localhost:4310`.
`max_retries=0` disables automatic SDK retries; see Errors below.

## Connect and call a provider

The integration must already be enabled. Use its unique key (`github` here),
not its display name. A **principal** is your user, service, or agent; obtain its
ID from your authenticated server session.

```python
session = client.connect.create_session(
    principal_id="user_123",
    allowed_integrations=["github"],
)
connect_url = session.data.connect_url
```

Send `connect_url` only to that principal and open it to authorize their account.
Session creation requires `connections:write`. Once authorization completes:

```python
github = client.proxy.for_(integration_key="github", principal_id="user_123")
response = github.get("/user")
response.raise_for_status()
profile = response.json()
```

Use `connection_id` instead of `principal_id` if you have a connection ID, never
both. Proxy methods include `get`, `post`, `put`, `patch`, and `delete` and return
`httpx.Response`. Reads need `connections:read`; writes need `connections:write`.

## Async

```python
import asyncio
import os
from neocrew_connector import AsyncCrewlink

async def main():
    async_client = AsyncCrewlink(
        token=os.environ["CREWLINK_API_KEY"],
        base_url=os.environ["CREWLINK_BASE_URL"],
        max_retries=0,
    )
    page = await async_client.connections.list(limit=20)
    return page.data

connections = asyncio.run(main())
```

In an existing event loop, use `await main()` instead. Async proxy methods use
an `a` prefix, for example `await github.aget("/user")` on a proxy created from
`async_client.proxy.for_(...)`.

## Principal tokens and secrets

- `client.principal_tokens.create(principal_id=..., name=..., scope="read", integrations=["github"], expires_at=...)`
  returns `result.data.token`, which an MCP client sends as `Authorization: Bearer`. Use a short ISO expiry;
  token creation requires `principals:write`.
- `client.secrets.create(principal_id=..., provider=..., label=..., values=...)`
  stores a provider-specific bundle and requires `secrets:write`.
- `client.secrets.read_env(principal_id=..., labels=["prod"])` returns
  `result.data.bundles`, each containing an `env` map, and requires `secrets:read`.

Keep API keys server-side. Give frontend code a narrowly scoped principal token,
not your API key, and register its browser origin in the issuing environment or
app. Never log tokens, credential-bearing MCP URLs, or decrypted secrets.

## Errors and pagination

API HTTP failures raise `neocrew_connector.core.ApiError` subclasses with
`status_code` and `body`; transport failures can raise `httpx` exceptions.
Proxy calls return responses even on HTTP errors: call `raise_for_status()` or
inspect `status_code`.

Generated API calls default to two retries on selected network failures and
HTTP 408, 409, 429, and 5xx responses, including mutations. Disable them with
`max_retries=0` when retries could duplicate work. The handwritten proxy does not
use that retry wrapper. A timeout does not prove that a write was not applied;
`timeout` limits individual HTTP requests, not the whole retry sequence.

List responses expose `data` and `next_cursor`. Pass `cursor=page.next_cursor`
to fetch another page until it is `None`.

## License

MIT. The license text is included in the package.
