-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathclient.py
More file actions
103 lines (76 loc) · 2.98 KB
/
client.py
File metadata and controls
103 lines (76 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import http
from contextlib import asynccontextmanager
from typing import Any, Mapping, Optional
import aiohttp
from clerk.errors import ClerkAPIException
__all__ = ["Client", "Service"]
class Client:
"""An API client for the clerk.dev API"""
def __init__(
self, token: str, base_url: str = "https://api.clerk.dev/v1/", timeout_seconds: float = 30.0
) -> None:
self._session = aiohttp.ClientSession(
headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json"},
timeout=aiohttp.ClientTimeout(total=timeout_seconds),
)
self._base_url = base_url
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self._session.close()
@property
def verification(self):
from clerk.verification import VerificationService
return VerificationService(self)
@property
def session(self):
from clerk.sessions import SessionsService
return SessionsService(self)
@property
def clients(self):
from clerk.clients import ClientsService
return ClientsService(self)
@property
def users(self):
from clerk.users import UsersService
return UsersService(self)
@property
def organizations(self):
from clerk.organizations import OrganizationsService
return OrganizationsService(self)
@asynccontextmanager
async def get(
self, endpoint: str, params: Optional[Mapping[str, str]] = None
) -> aiohttp.ClientResponse:
async with self._session.get(self._make_url(endpoint), params=params) as r:
await self._check_response_err(r)
yield r
@asynccontextmanager
async def post(
self, endpoint: str, data: Any = None, json: Any = None
) -> aiohttp.ClientResponse:
async with self._session.post(self._make_url(endpoint), data=data, json=json) as r:
await self._check_response_err(r)
yield r
@asynccontextmanager
async def delete(self, endpoint: str) -> aiohttp.ClientResponse:
async with self._session.delete(self._make_url(endpoint)) as r:
await self._check_response_err(r)
yield r
@asynccontextmanager
async def patch(
self, endpoint: str, data: Any = None, json: Any = None
) -> aiohttp.ClientResponse:
async with self._session.patch(self._make_url(endpoint), data=data, json=json) as r:
await self._check_response_err(r)
yield r
async def _check_response_err(self, r: aiohttp.ClientResponse):
if http.HTTPStatus.OK <= r.status < http.HTTPStatus.BAD_REQUEST:
return # no error
raise await ClerkAPIException.from_response(r)
def _make_url(self, endpoint: str) -> str:
return f"{self._base_url.rstrip('/')}/{endpoint.strip('/')}/"
class Service:
"""Base Clerk service"""
def __init__(self, client: Client) -> None:
self._client = client