Metadata-Version: 2.4
Name: wr-common-lib
Version: 0.1.7
Summary: Shared enums, Resend provider, and optional PostgreSQL helpers for email workflows
License: MIT
Requires-Python: >=3.12
Provides-Extra: db
Requires-Dist: async-db-tools>=0.1.0; extra == 'db'
Provides-Extra: resend
Requires-Dist: httpx>=0.27.0; extra == 'resend'
Description-Content-Type: text/markdown

# wr-common-lib

Shared enums, Resend provider, attachment encoding helpers, and optional PostgreSQL write helpers for the `email` table.

| PyPI | `wr-common-lib` |
|------|-----------------|
| import | `wr_common_lib` |

Requires Python 3.12+.

## Install

```bash
pip install wr-common-lib
pip install "wr-common-lib[resend]"   # ResendProvider (httpx)
pip install "wr-common-lib[db]"       # EmailDbOper (async-db-tools)
pip install "wr-common-lib[db,resend]"
```

## Package layout

```
src/wr_common_lib/
├── __init__.py          # __version__
└── email/
    ├── __init__.py      # public exports
    ├── constants.py     # MailFlow, MailStatus
    ├── encoding.py      # file_to_base64, file_to_attachment
    ├── provider.py      # ResendProvider, SendResult, ReceivedEmail
    └── db_oper.py       # EmailDbOper, get_email_content_hash
```

## Enums

`MailFlow` and `MailStatus` match PostgreSQL `mail_flow` / `mail_status`.

```python
from wr_common_lib.email import MailFlow, MailStatus

MailFlow.INBOUND
MailStatus.PENDING.value
```

### Status flows

```
Outbound:  PENDING → SENT | FAILED；webhook → DELIVERED | BOUNCED
Inbound:   RECEIVED → PARSED | PARSE_FAILED
```

## Content hash

```python
from wr_common_lib.email import get_email_content_hash

content_hash = get_email_content_hash(
    to="a@example.com,b@example.com",
    subject="...",
    content="...",
    cc="",
    attachments=[{"filename": "report.pdf"}],
)
```

Normalizes `to` / `cc` (comma-separated), strips subject/content, and hashes attachment **filenames** only (not file bytes).

## Attachment encoding

Converts local files to Base64 for Resend `attachments[].content`.

```python
from wr_common_lib.email import file_to_base64, file_to_attachment

b64 = file_to_base64("/path/to/report.pdf")

att = file_to_attachment("/path/to/report.pdf")
# {"filename": "report.pdf", "content": "<base64>", "content_type": "application/pdf"}
```

## ResendProvider

Requires `httpx` (`[resend]` extra). Uses the [Resend API](https://resend.com/docs).

```python
from wr_common_lib.email import ResendProvider, file_to_attachment

provider = ResendProvider(
    api_key="re_...",
    default_from="noreply@example.com",
    default_from_name="My App",
)

# Send (attachments from file_to_attachment)
result = await provider.send(
    to="user@example.com",
    cc="",
    subject="Hello",
    content="Plain text body",
    attachments=[file_to_attachment("/path/to/file.pdf")],
    mail_from=None,           # optional override
    email_id=email_id,        # optional, sets X-Email-ID header
)
# result.ok, result.message_id

# Inbound (Receiving API)
mail = await provider.received(message_id)
# mail.ok, mail.subject, mail.text, mail.html, mail.from_addr, mail.to, mail.cc, ...
body = mail.content   # text, else html
```

## EmailDbOper

Requires the `[db]` extra. Pass an [`async-db-tools`](https://pypi.org/project/async-db-tools/) `PostgresPool` (or compatible pool with `fetchval` / `execute`).

```python
from wr_common_lib.email import EmailDbOper, MailFlow, MailStatus, get_email_content_hash

db_oper = EmailDbOper(pool)
```

Write operations only; **reads should use `db_oper._db` directly** (e.g. `fetchrow`, `fetchval`).

### `insert_email`

```python
email_id, created = await db_oper.insert_email(
    task,
    MailFlow.OUTBOUND,
    MailStatus.PENDING,
)
# created is True when a new row was inserted, False when content_hash already existed
```

**Task fields** (inbound and outbound use the same shape):

| Field | Required | Notes |
|-------|----------|-------|
| `imo` | yes | |
| `voyage_id` | yes | UUID string |
| `mail_from` | yes | Sender |
| `to` | yes | Recipients (comma-separated allowed) |
| `cc` | no | Default `""` |
| `subject` | no | Default `""` |
| `content` | no | Default `""` |
| `attachments` | no | List of dicts with `filename` |
| `content_hash` | no | Computed via `get_email_content_hash` when omitted |
| `created_user_id` | no | Outbound only |

On duplicate `content_hash`, the insert is skipped (`ON CONFLICT DO NOTHING`); the existing row id is returned and `created` is `False`.

### `update_status` / `update_parsed_data`

```python
await db_oper.update_status(email_id, MailStatus.SENT)
await db_oper.update_parsed_data(email_id, {"parsed": "..."})
```

## Dependencies

| install | brings in |
|---------|-----------|
| `wr-common-lib` | enums, encoding, `get_email_content_hash`, `ResendProvider` (needs `httpx` at runtime — use `[resend]`) |
| `wr-common-lib[resend]` | `httpx` |
| `wr-common-lib[db]` | `async-db-tools` |

Your application owns API keys, database URL, and pool lifecycle.

## Publish

```bash
pip install -e ".[db,resend]"

# 1. Update CHANGELOG.md for the new version
# 2. Bump version and publish
uv version 0.1.8
export UV_PUBLISH_TOKEN=pypi-...
uv build && uv publish

git tag v0.1.8 && git push --tags
```

PyPI does not allow re-uploading the same version.

## License

MIT — see [LICENSE](LICENSE).
