-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
152 lines (123 loc) · 4.23 KB
/
client.py
File metadata and controls
152 lines (123 loc) · 4.23 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""CueAPI client — the main entry point for the SDK."""
from __future__ import annotations
from typing import Any, Dict, Optional
import httpx
from cueapi.exceptions import (
AuthenticationError,
CueAPIError,
CueAPIServerError,
CueLimitExceededError,
CueNotFoundError,
InvalidScheduleError,
RateLimitError,
)
from cueapi.resources.cues import CuesResource
from cueapi.resources.executions import ExecutionsResource
DEFAULT_BASE_URL = "https://api.cueapi.ai"
DEFAULT_TIMEOUT = 30.0
class CueAPI:
"""CueAPI client.
Usage::
from cueapi import CueAPI
client = CueAPI("cue_sk_your_key")
cue = client.cues.create(
name="daily-report",
cron="0 9 * * *",
callback="https://my-app.com/webhook",
)
"""
def __init__(
self,
api_key: str,
*,
base_url: str = DEFAULT_BASE_URL,
timeout: float = DEFAULT_TIMEOUT,
) -> None:
"""Initialize the CueAPI client.
Args:
api_key: Your CueAPI API key (starts with ``cue_sk_``).
base_url: API base URL (default ``https://api.cueapi.ai``).
timeout: Request timeout in seconds (default 30).
"""
if not api_key:
raise ValueError("api_key is required")
self._api_key = api_key
self._base_url = base_url.rstrip("/")
self._http = httpx.Client(
base_url=self._base_url,
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
"User-Agent": "cueapi-python/0.1.0",
},
timeout=timeout,
)
# Resources
self.cues = CuesResource(self)
self.executions = ExecutionsResource(self)
def close(self) -> None:
"""Close the underlying HTTP client."""
self._http.close()
def __enter__(self) -> CueAPI:
return self
def __exit__(self, *args: Any) -> None:
self.close()
# --- HTTP helpers ---
def _request(
self,
method: str,
path: str,
*,
json: Optional[Dict[str, Any]] = None,
params: Optional[Dict[str, Any]] = None,
) -> Any:
"""Make an HTTP request and handle errors."""
response = self._http.request(method, path, json=json, params=params)
return self._handle_response(response)
def _handle_response(self, response: httpx.Response) -> Any:
"""Parse response, raise typed exceptions on errors."""
if response.status_code == 204:
return None
try:
data = response.json()
except Exception:
data = {"error": {"message": response.text, "code": "unknown"}}
if response.is_success:
return data
# Extract error info
error_body = data.get("error", data)
message = error_body.get("message", "Unknown error")
code = error_body.get("code", "unknown")
status = response.status_code
kwargs = dict(
message=message,
status_code=status,
code=code,
body=data,
)
if status == 401:
raise AuthenticationError(**kwargs)
elif status == 403:
raise CueLimitExceededError(**kwargs)
elif status == 404:
raise CueNotFoundError(**kwargs)
elif status == 429:
retry_after = response.headers.get("Retry-After")
raise RateLimitError(
retry_after=int(retry_after) if retry_after else None,
**kwargs,
)
elif status == 400 or status == 422:
raise InvalidScheduleError(**kwargs)
elif status >= 500:
raise CueAPIServerError(**kwargs)
else:
raise CueAPIError(**kwargs)
def _get(self, path: str, **kwargs: Any) -> Any:
return self._request("GET", path, **kwargs)
def _post(self, path: str, **kwargs: Any) -> Any:
return self._request("POST", path, **kwargs)
def _patch(self, path: str, **kwargs: Any) -> Any:
return self._request("PATCH", path, **kwargs)
def _delete(self, path: str, **kwargs: Any) -> Any:
return self._request("DELETE", path, **kwargs)