Metadata-Version: 2.4
Name: bardiyamap-sdk
Version: 0.1.0
Summary: Official Bardiya Map SDK for Python: Places, Geocoding, Directions, Matrix, Tracking and Realtime.
Author: Bardiya Map
License-Expression: Apache-2.0
Project-URL: Homepage, https://bardiyamap.com/developers/sdks
Project-URL: Documentation, https://bardiyamap.com/developers/sdks
Project-URL: Changelog, https://bardiyamap.com/developers/sdks
Keywords: bardiya,maps,geocoding,routing,tracking,egypt
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: OS Independent
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: httpx<0.29,>=0.27
Requires-Dist: httpx-sse<0.5,>=0.4
Requires-Dist: pydantic<3,>=2.6
Provides-Extra: test
Requires-Dist: pytest>=8; extra == "test"
Requires-Dist: pytest-asyncio>=0.23; extra == "test"
Dynamic: license-file

# Bardiya Map SDK for Python

Official client for the Bardiya Map API. Python 3.10+.

- Pydantic v2 models generated from the published contract.
- `httpx` transport with bounded retries, `Retry-After` support and a shared connection pool.
- Server-sent events parsed by `httpx-sse`.
- Every error is a `BardiyaError` with `status`, `code`, `details`, `request_id` and `retry_after_ms`.

## Install

```bash
pip install bardiyamap-sdk
```

Requires Python 3.10 or newer. Published on
[PyPI](https://pypi.org/project/bardiyamap-sdk/).

## Quickstart

```python
from bardiya import BardiyaClient

with BardiyaClient(api_key="bdy_live_...") as bardiya:
    results = bardiya.maps.places.search("coffee", latitude=30.0444, longitude=31.2357)
    for place in results.results:
        print(place.name, place.coordinate.longitude, place.coordinate.latitude)
```

`client.maps` sends `X-API-Key`; `client.tracking` sends
`Authorization: Bearer`. Pass `maps_api_key` / `tracking_api_key` when the two
credentials differ.

## Maps

```python
route = bardiya.maps.routing.directions(
    {"longitude": 31.2357, "latitude": 30.0444},
    {"longitude": 31.3209, "latitude": 30.0626},
    profile="driving",
)

matrix = bardiya.maps.matrix.calculate(
    origins=[{"longitude": 31.2357, "latitude": 30.0444}],
    destinations=[{"longitude": 31.3209, "latitude": 30.0626}],
)

reverse = bardiya.maps.geocoding.reverse(30.0451, 31.2362)
```

Maps GETs retry 429/5xx/network failures with bounded backoff. The three
chargeable POST computations (`/v1/directions`, `/v1/distance-matrix`,
`/v1/validate-address`) retry only when `retry_post_computations=True` **and**
the call passes `idempotency_key`; even then an HTTP error response is final and
only an ambiguous transport failure/timeout is replayed. The header itself is
accepted on every metered Maps route, including GET reads; the ledger
deduplicates debit and recomputes rather than caching the body. The server-side
idempotency ledger is flag-gated off in production, so no exactly-once guarantee
is claimed. Set `max_retries=0` to disable retries.

## Tracking

```python
session = bardiya.tracking.sessions.create(
    subject_type="device",
    external_reference="TRUCK-17",
    idempotency_key="truck-17-create",
)

result = bardiya.tracking.sessions.send_location(
    session.id,
    {
        "client_event_id": "22222222-2222-2222-2222-222222222222",
        "client_seq": 1,
        "recorded_at": "2026-11-02T10:00:00Z",
        "event_type": "location",
        "latitude": 30.0444,
        "longitude": 31.2357,
    },
)

state = bardiya.tracking.sessions.state(session.id)
history = bardiya.tracking.sessions.history(session.id, limit=100)
```

`client_seq` must increase per session and `client_event_id` is the idempotency
key: re-sending the same id is reported as `duplicate=True` instead of creating
a second event. Reads of sessions, state, history and webhooks are routed to the
read plane automatically.

### Realtime

```python
for frame in bardiya.tracking.sessions.stream(session.id):
    if frame.event == "location":
        print(frame.id, frame.data)
    elif frame.event == "closed":
        break
```

Resume is not supported. On reconnect, read the snapshot again and use
`history()` for anything older.

### Device ingest

Do not ship an API key inside a mobile app. Issue a session-scoped credential
from your backend, then give the device only that:

```python
credential = bardiya.tracking.client_tokens.issue_ingest(session.id)

device = bardiya.ingest_client(
    session_id=session.id,
    organization_id=ORGANIZATION_ID,   # from the console; not returned by the session API
    ingest_credential=credential.ingest_credential,
)
device.send_location({...})
```

The credential can only post events for its own session.

## Webhooks

```python
from bardiya import verify_webhook_signature

ok = verify_webhook_signature(
    secret=endpoint_secret,
    timestamp=int(request.headers["X-Bardiya-Webhook-Timestamp"]),
    body=raw_body,  # the exact bytes received
    signature=request.headers["X-Bardiya-Webhook-Signature"],
)
```

Pass the full `v1=<lowercase hex>` header and the exact raw body. The helper
authenticates the timestamp but does not check freshness: reject timestamps
outside your replay window separately. Unsupported versions return `False`;
pass `raise_on_unknown_version=True` to receive a `ValueError` instead.

## Errors

```python
from bardiya import BardiyaError

try:
    bardiya.maps.places.search("coffee")
except BardiyaError as error:
    print(error.status, error.code, error.request_id)
    if error.status == 429:
        time.sleep((error.retry_after_ms or 1000) / 1000)
```

`status == 0` means no HTTP response was received (timeout or network failure).

## Free Sandbox quotas

A 429 can mean two different things:

- a renewable rate window: `Retry-After` is present, retrying later works;
- a spent Free Sandbox lifetime balance: `X-Free-Sandbox-Credits-Remaining: 0`
  and **no** `Retry-After`. It does not refill; do not loop.

## Limitations

- Sync client only; an async client is not shipped yet.
- No publishable/browser key type exists, so this package is server-side only.
- The client has not been exercised against a live API in this repository.

## Tests

The suite ships in `tests/` in the source distribution and needs the `test`
extra:

```bash
pip install "bardiyamap-sdk[test]"
```

One check compares the packaged `LICENSE` and `NOTICE` with their `sdk/` root
copies, so it only passes inside a full SDK checkout.
