-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.py
More file actions
49 lines (40 loc) · 1.16 KB
/
client.py
File metadata and controls
49 lines (40 loc) · 1.16 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
from enum import Enum
from typing import Any, Generic, TypedDict, TypeVar
from urllib.parse import urljoin
import requests
from requests import Response
T = TypeVar("T")
class GenericTypedDict(TypedDict, Generic[T]):
pass
class HttpMethod(Enum):
GET = "GET"
PUT = "PUT"
POST = "POST"
PATCH = "PATCH"
DELETE = "DELETE"
class HttpClient:
def __init__(self, api_key: str) -> None:
self._api_key = api_key
self._base_url = "https://api.usemotion.com"
self._api_version = "v1"
def call_api(
self,
method: HttpMethod,
path: str,
data: GenericTypedDict[Any] | None = None,
params: dict[str, Any] | None = None,
) -> Response:
"""
Call the Motion API
"""
path = path[1:] if path.startswith("/") else path
url = urljoin(self._base_url, f"/{self._api_version}/{path}")
response = requests.request(
method=method.value,
url=url,
json=data,
params=params, # type: ignore
headers={"X-API-Key": self._api_key},
)
response.raise_for_status()
return response