|
| 1 | +import httpx |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Optional, Any |
| 4 | + |
| 5 | +@dataclass |
| 6 | +class Connection: |
| 7 | + id: str |
| 8 | + provider: str |
| 9 | + name: str |
| 10 | + status: str |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class Sync: |
| 14 | + id: str |
| 15 | + status: str |
| 16 | + records_synced: Optional[int] = None |
| 17 | + |
| 18 | +class _ConnectionsAPI: |
| 19 | + def __init__(self, client: 'ThreadSync'): |
| 20 | + self._client = client |
| 21 | + |
| 22 | + def create(self, provider: str, **kwargs) -> Connection: |
| 23 | + data = self._client._request('POST', '/connections', json={'provider': provider, **kwargs}) |
| 24 | + return Connection(**data) |
| 25 | + |
| 26 | + def get(self, id: str) -> Connection: |
| 27 | + data = self._client._request('GET', f'/connections/{id}') |
| 28 | + return Connection(**data) |
| 29 | + |
| 30 | + def list(self) -> list: |
| 31 | + data = self._client._request('GET', '/connections') |
| 32 | + return [Connection(**c) for c in data] |
| 33 | + |
| 34 | +class _SyncAPI: |
| 35 | + def __init__(self, client: 'ThreadSync'): |
| 36 | + self._client = client |
| 37 | + |
| 38 | + def create(self, source: dict, destination: dict, schedule: str = 'realtime') -> Sync: |
| 39 | + data = self._client._request('POST', '/syncs', json={ |
| 40 | + 'source': source, 'destination': destination, 'schedule': schedule |
| 41 | + }) |
| 42 | + return Sync(**data) |
| 43 | + |
| 44 | + def get(self, id: str) -> Sync: |
| 45 | + data = self._client._request('GET', f'/syncs/{id}') |
| 46 | + return Sync(**data) |
| 47 | + |
| 48 | +class ThreadSync: |
| 49 | + def __init__(self, bearer_token: str, base_url: str = 'https://api.threadsync.io/v1'): |
| 50 | + self._http = httpx.Client( |
| 51 | + base_url=base_url, |
| 52 | + headers={ |
| 53 | + 'Authorization': f'Bearer {bearer_token}', |
| 54 | + 'Content-Type': 'application/json', |
| 55 | + 'User-Agent': 'threadsync-python/0.1.0', |
| 56 | + }, |
| 57 | + ) |
| 58 | + self.connections = _ConnectionsAPI(self) |
| 59 | + self.sync = _SyncAPI(self) |
| 60 | + |
| 61 | + def _request(self, method: str, path: str, **kwargs) -> Any: |
| 62 | + response = self._http.request(method, path, **kwargs) |
| 63 | + response.raise_for_status() |
| 64 | + return response.json() |
0 commit comments