# Python

Quillsql.

```sh
pip install quillsql
```

```python
import os
from fastapi import Depends, FastAPI, Request
from quillsql import Quill

app = FastAPI()
quill = Quill(
    private_key=os.getenv("QUILL_PRIVATE_KEY"),
    database_connection_string=os.getenv("POSTGRES_READ"),
    database_type="postgresql",
)

# authenticate is your own auth dependency
@app.post("/quill")
async def quill_post(request: Request, user: dict = Depends(authenticate)):
    body = await request.json()
    return await quill.query(
        tenants=[{"tenantField": "organization_id", "tenantIds": [user["org_id"]]}],
        metadata=body["metadata"],
    )
```

## Quill

```python
Quill(private_key, database_type, database_connection_string=None, database_config=None, metadataServerURL=None, cache=None)
```

| Param                        | Default                |                                                                                                                                                   |
| ---------------------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `private_key`                | required               | `str`: the private key of your Quill environment                                                                                                  |
| `database_type`              | required               | `"postgresql"`, `"redshift"`, `"snowflake"`, `"bigquery"`, `"mysql"`, `"clickhouse"`, `"mssql"` or `"databricks"`                                 |
| `database_connection_string` |                        | `str`; used over `database_config`                                                                                                                |
| `database_config`            |                        | `dict`: the connection, by `database_type`                                                                                                        |
| `metadataServerURL`          | `https://api.quill.co` | `str`                                                                                                                                             |
| `cache`                      |                        | `{"cache_type": "redis" \| "rediss", "host", "port", "username", "password", "ttl"}`: caches results in Redis for `ttl` seconds, 86400 by default |

## query

```python
await quill.query(tenants, metadata, flags=None, filters=None, admin_enabled=False)
```

| Param           | Default  |                                                                                            |
| --------------- | -------- | ------------------------------------------------------------------------------------------ |
| `tenants`       | required | `[{"tenantField": str, "tenantIds": list}]`, or a list of tenant ids                       |
| `metadata`      | required | `body["metadata"]`                                                                         |
| `flags`         | `None`   | `list[str]`, or `[{"tenantField": str, "flags": list[str]}]`                               |
| `filters`       | `None`   | `list[Filter]`                                                                             |
| `admin_enabled` | `False`  | `bool`: allows admin tasks, such as editing dashboards; set it from your own authorization |

| Returns     |                                 |
| ----------- | ------------------------------- |
| `"status"`  | `"success"` or `"error"`        |
| `"data"`    | `dict`                          |
| `"queries"` | `{"queryResults": list}`        |
| `"error"`   | `str`, with `"status": "error"` |

`quill.query_sync(...)` takes the same parameters, synchronously; not for Postgres and Redshift.

## generate_client_token

```python
quill.generate_client_token(public_key, tenants, expiration, flags=None) -> str
```

| Param        |                                                              |
| ------------ | ------------------------------------------------------------ |
| `public_key` | `str`                                                        |
| `tenants`    | `[{"tenantField": str, "tenantIds": list}]`                  |
| `expiration` | `int`: milliseconds since the epoch, in the future           |
| `flags`      | `list[str]`, or `[{"tenantField": str, "flags": list[str]}]` |

## stream

```python
async for event in quill.stream(tenants, metadata, flags=None, filters=None, admin_enabled=False):
    ...
```

## close

```python
quill.close()
await quill.aclose()
```
