xlkitlearn-api 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 Daniel Guetta
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xlkitlearn_api
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A python client for XLKitLearn APIs
|
|
5
|
+
Project-URL: Homepage, https://www.xlkitlearn.com
|
|
6
|
+
Author-email: Daniel Guetta <daniel@guetta.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE.md
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Requires-Dist: requests>=2.32.2
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# XLKitLearn API Client
|
|
16
|
+
|
|
17
|
+
This package is a Python client for the APIs available with XLKitLearn. See [xlkitlearn.com](https://www.xlkitlearn.com).
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "xlkitlearn_api"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"requests>=2.32.2",
|
|
10
|
+
]
|
|
11
|
+
authors = [
|
|
12
|
+
{ name="Daniel Guetta", email="daniel@guetta.com" },
|
|
13
|
+
]
|
|
14
|
+
description = "A python client for XLKitLearn APIs"
|
|
15
|
+
readme = "readme.md"
|
|
16
|
+
requires-python = ">=3.10"
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
]
|
|
21
|
+
license = "MIT"
|
|
22
|
+
license-files = ["LICEN[CS]E*"]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://www.xlkitlearn.com"
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import typing
|
|
3
|
+
import urllib.parse
|
|
4
|
+
|
|
5
|
+
import requests
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
__all__ = ["APIGateway"]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class APIGateway:
|
|
12
|
+
"""
|
|
13
|
+
Synchronous Python client for the XLKit Learn API.
|
|
14
|
+
|
|
15
|
+
Parameters
|
|
16
|
+
----------
|
|
17
|
+
api_key:
|
|
18
|
+
API key sent as an Authorization bearer token.
|
|
19
|
+
base_url:
|
|
20
|
+
Base API URL. Defaults to the production XLKit Learn API.
|
|
21
|
+
timeout:
|
|
22
|
+
Request timeout in seconds.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
DEFAULT_BASE_URL = "https://server.xlkitlearn.com/api"
|
|
26
|
+
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
api_key: str,
|
|
30
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
31
|
+
timeout: float = 60.0,
|
|
32
|
+
):
|
|
33
|
+
self.api_key = api_key
|
|
34
|
+
self.base_url = base_url.rstrip("/")
|
|
35
|
+
self.timeout = timeout
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def _auth_headers(self) -> dict[str, str]:
|
|
39
|
+
api_key = self.api_key.strip()
|
|
40
|
+
if api_key.lower().startswith("bearer "):
|
|
41
|
+
return {"Authorization": api_key}
|
|
42
|
+
return {"Authorization": f"Bearer {api_key}"}
|
|
43
|
+
|
|
44
|
+
def _url(self, path: str) -> str:
|
|
45
|
+
return f"{self.base_url}/{path.lstrip('/')}"
|
|
46
|
+
|
|
47
|
+
def _request(self, method: str, path: str, **kwargs: typing.Any) -> typing.Any:
|
|
48
|
+
headers = dict(self._auth_headers)
|
|
49
|
+
headers.update(kwargs.pop("headers", {}) or {})
|
|
50
|
+
|
|
51
|
+
response = requests.request(
|
|
52
|
+
method,
|
|
53
|
+
self._url(path),
|
|
54
|
+
headers=headers,
|
|
55
|
+
timeout=self.timeout,
|
|
56
|
+
**kwargs,
|
|
57
|
+
)
|
|
58
|
+
return self._decode_response(response)
|
|
59
|
+
|
|
60
|
+
def _get(self, path: str) -> typing.Any:
|
|
61
|
+
return self._request("GET", path)
|
|
62
|
+
|
|
63
|
+
def _post(self, path: str, payload: dict[str, typing.Any]) -> typing.Any:
|
|
64
|
+
return self._request("POST", path, json=self._without_none(payload))
|
|
65
|
+
|
|
66
|
+
def _post_sse(self, path: str, payload: dict[str, typing.Any]) -> typing.Any:
|
|
67
|
+
response = requests.post(
|
|
68
|
+
self._url(path),
|
|
69
|
+
headers=self._auth_headers,
|
|
70
|
+
json=self._without_none(payload),
|
|
71
|
+
timeout=self.timeout,
|
|
72
|
+
)
|
|
73
|
+
self._raise_for_status(response)
|
|
74
|
+
|
|
75
|
+
events = self._parse_sse(response.text)
|
|
76
|
+
for event, data in events:
|
|
77
|
+
if event == "error":
|
|
78
|
+
raise RuntimeError(str(data))
|
|
79
|
+
if event == "sent":
|
|
80
|
+
return data
|
|
81
|
+
|
|
82
|
+
return events[-1][1] if events else None
|
|
83
|
+
|
|
84
|
+
def _decode_response(self, response: requests.Response) -> typing.Any:
|
|
85
|
+
self._raise_for_status(response)
|
|
86
|
+
if not response.content:
|
|
87
|
+
return None
|
|
88
|
+
|
|
89
|
+
content_type = response.headers.get("content-type", "")
|
|
90
|
+
if "application/json" in content_type:
|
|
91
|
+
return response.json()
|
|
92
|
+
|
|
93
|
+
try:
|
|
94
|
+
return response.json()
|
|
95
|
+
except ValueError:
|
|
96
|
+
return response.text
|
|
97
|
+
|
|
98
|
+
def _raise_for_status(self, response: requests.Response) -> None:
|
|
99
|
+
if response.status_code < 400:
|
|
100
|
+
return
|
|
101
|
+
|
|
102
|
+
detail: typing.Any
|
|
103
|
+
try:
|
|
104
|
+
payload = response.json()
|
|
105
|
+
detail = payload.get("detail", payload) if isinstance(payload, dict) else payload
|
|
106
|
+
except ValueError:
|
|
107
|
+
detail = response.text
|
|
108
|
+
|
|
109
|
+
message = f"{response.status_code} error for {response.request.method} {response.url}: {detail}"
|
|
110
|
+
raise requests.HTTPError(message, response=response)
|
|
111
|
+
|
|
112
|
+
@staticmethod
|
|
113
|
+
def _without_none(payload: dict[str, typing.Any]) -> dict[str, typing.Any]:
|
|
114
|
+
return {key: value for key, value in payload.items() if value is not None}
|
|
115
|
+
|
|
116
|
+
@staticmethod
|
|
117
|
+
def _quote_path(value: str) -> str:
|
|
118
|
+
return urllib.parse.quote(str(value), safe="")
|
|
119
|
+
|
|
120
|
+
@staticmethod
|
|
121
|
+
def _parse_sse(text: str) -> list[tuple[str | None, typing.Any]]:
|
|
122
|
+
events: list[tuple[str | None, typing.Any]] = []
|
|
123
|
+
event: str | None = None
|
|
124
|
+
data_lines: list[str] = []
|
|
125
|
+
|
|
126
|
+
for line in text.splitlines():
|
|
127
|
+
if line == "":
|
|
128
|
+
if event is not None or data_lines:
|
|
129
|
+
raw_data = "\n".join(data_lines)
|
|
130
|
+
try:
|
|
131
|
+
data: typing.Any = json.loads(raw_data)
|
|
132
|
+
except ValueError:
|
|
133
|
+
data = raw_data
|
|
134
|
+
events.append((event, data))
|
|
135
|
+
event = None
|
|
136
|
+
data_lines = []
|
|
137
|
+
continue
|
|
138
|
+
|
|
139
|
+
if line.startswith("event:"):
|
|
140
|
+
event = line[len("event:") :].strip()
|
|
141
|
+
elif line.startswith("data:"):
|
|
142
|
+
data_lines.append(line[len("data:") :].strip())
|
|
143
|
+
|
|
144
|
+
if event is not None or data_lines:
|
|
145
|
+
raw_data = "\n".join(data_lines)
|
|
146
|
+
try:
|
|
147
|
+
data = json.loads(raw_data)
|
|
148
|
+
except ValueError:
|
|
149
|
+
data = raw_data
|
|
150
|
+
events.append((event, data))
|
|
151
|
+
|
|
152
|
+
return events
|
|
153
|
+
|
|
154
|
+
def quotas(self) -> dict[str, typing.Any]:
|
|
155
|
+
"""GET /quotas"""
|
|
156
|
+
|
|
157
|
+
return self._get("quotas")
|
|
158
|
+
|
|
159
|
+
def send_text(self, phone_number: str, message: str) -> dict[str, typing.Any]:
|
|
160
|
+
"""
|
|
161
|
+
POST /send-text with wait_for_response disabled.
|
|
162
|
+
|
|
163
|
+
Returns the immediate sent event payload, usually {"text_id": "..."}.
|
|
164
|
+
"""
|
|
165
|
+
|
|
166
|
+
return self._post_sse(
|
|
167
|
+
"send-text",
|
|
168
|
+
{
|
|
169
|
+
"phone_number": phone_number,
|
|
170
|
+
"message": message,
|
|
171
|
+
"wait_for_response": False,
|
|
172
|
+
},
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
def get_text_responses(self, text_id: str) -> list[str]:
|
|
176
|
+
"""GET /send-text/responses/{text_id}"""
|
|
177
|
+
|
|
178
|
+
return self._get(f"send-text/responses/{self._quote_path(text_id)}")
|
|
179
|
+
|
|
180
|
+
def send_email(
|
|
181
|
+
self,
|
|
182
|
+
send_to_address: str,
|
|
183
|
+
email_subject: str = "(no subject)",
|
|
184
|
+
email_text: str = "(no message)",
|
|
185
|
+
thread_token: str | None = None,
|
|
186
|
+
) -> dict[str, typing.Any]:
|
|
187
|
+
"""
|
|
188
|
+
POST /send-email with wait_for_response disabled.
|
|
189
|
+
|
|
190
|
+
Returns the immediate sent event payload, usually
|
|
191
|
+
{"message_id": "...", "thread_token": "..."}.
|
|
192
|
+
"""
|
|
193
|
+
|
|
194
|
+
return self._post_sse(
|
|
195
|
+
"send-email",
|
|
196
|
+
{
|
|
197
|
+
"send_to_address": send_to_address,
|
|
198
|
+
"email_subject": email_subject,
|
|
199
|
+
"email_text": email_text,
|
|
200
|
+
"wait_for_response": False,
|
|
201
|
+
"thread_token": thread_token,
|
|
202
|
+
},
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
def get_email_thread(self, thread_token: str) -> list[dict[str, typing.Any]]:
|
|
206
|
+
"""GET /send-email/thread/{thread_token}"""
|
|
207
|
+
|
|
208
|
+
return self._get(f"send-email/thread/{self._quote_path(thread_token)}")
|
|
209
|
+
|
|
210
|
+
def maps_places_search(
|
|
211
|
+
self,
|
|
212
|
+
query: str,
|
|
213
|
+
restriction_place_id: str | None = None,
|
|
214
|
+
restriction_radius_meters: float | None = None,
|
|
215
|
+
included_type: str | None = None,
|
|
216
|
+
open_now: bool | None = None,
|
|
217
|
+
) -> dict[str, typing.Any]:
|
|
218
|
+
"""POST /maps/places/search"""
|
|
219
|
+
|
|
220
|
+
return self._post(
|
|
221
|
+
"maps/places/search",
|
|
222
|
+
{
|
|
223
|
+
"query": query,
|
|
224
|
+
"restriction_place_id": restriction_place_id,
|
|
225
|
+
"restriction_radius_meters": restriction_radius_meters,
|
|
226
|
+
"included_type": included_type,
|
|
227
|
+
"open_now": open_now,
|
|
228
|
+
},
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
def maps_routes_travel_time(
|
|
232
|
+
self,
|
|
233
|
+
origin_place_id: str,
|
|
234
|
+
destination_place_id: str,
|
|
235
|
+
travel_mode: str = "DRIVE",
|
|
236
|
+
consider_traffic: bool = False,
|
|
237
|
+
departure_time: str | None = None,
|
|
238
|
+
arrival_time: str | None = None,
|
|
239
|
+
units: str = "IMPERIAL",
|
|
240
|
+
) -> dict[str, typing.Any]:
|
|
241
|
+
"""POST /maps/routes/travel-time"""
|
|
242
|
+
|
|
243
|
+
return self._post(
|
|
244
|
+
"maps/routes/travel-time",
|
|
245
|
+
{
|
|
246
|
+
"origin_place_id": origin_place_id,
|
|
247
|
+
"destination_place_id": destination_place_id,
|
|
248
|
+
"travel_mode": travel_mode,
|
|
249
|
+
"consider_traffic": consider_traffic,
|
|
250
|
+
"departure_time": departure_time,
|
|
251
|
+
"arrival_time": arrival_time,
|
|
252
|
+
"units": units,
|
|
253
|
+
},
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
def maps_weather(
|
|
257
|
+
self,
|
|
258
|
+
latitude: float,
|
|
259
|
+
longitude: float,
|
|
260
|
+
units_system: str = "IMPERIAL",
|
|
261
|
+
forecast_until: str | None = None,
|
|
262
|
+
) -> dict[str, typing.Any]:
|
|
263
|
+
"""POST /maps/weather"""
|
|
264
|
+
|
|
265
|
+
return self._post(
|
|
266
|
+
"maps/weather",
|
|
267
|
+
{
|
|
268
|
+
"latitude": latitude,
|
|
269
|
+
"longitude": longitude,
|
|
270
|
+
"units_system": units_system,
|
|
271
|
+
"forecast_until": forecast_until,
|
|
272
|
+
},
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
def flight_availability(
|
|
276
|
+
self,
|
|
277
|
+
cabin_class: str,
|
|
278
|
+
allow_connection: bool,
|
|
279
|
+
legs: list[dict[str, typing.Any]],
|
|
280
|
+
) -> dict[str, typing.Any]:
|
|
281
|
+
"""POST /duffel/flight-availability"""
|
|
282
|
+
|
|
283
|
+
return self._post(
|
|
284
|
+
"duffel/flight-availability",
|
|
285
|
+
{
|
|
286
|
+
"cabin_class": cabin_class,
|
|
287
|
+
"allow_connection": allow_connection,
|
|
288
|
+
"legs": legs,
|
|
289
|
+
},
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
def flight_availability_next_leg(
|
|
293
|
+
self,
|
|
294
|
+
search_id: str,
|
|
295
|
+
previous_legs: str,
|
|
296
|
+
) -> dict[str, typing.Any]:
|
|
297
|
+
"""POST /duffel/flight-availability/next-leg"""
|
|
298
|
+
|
|
299
|
+
return self._post(
|
|
300
|
+
"duffel/flight-availability/next-leg",
|
|
301
|
+
{
|
|
302
|
+
"search_id": search_id,
|
|
303
|
+
"previous_legs": previous_legs,
|
|
304
|
+
},
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
def finance_daily_prices(
|
|
308
|
+
self,
|
|
309
|
+
tickers: list[str],
|
|
310
|
+
start: str,
|
|
311
|
+
end: str,
|
|
312
|
+
) -> dict[str, list[dict[str, typing.Any]]]:
|
|
313
|
+
"""POST /finance/daily-prices"""
|
|
314
|
+
|
|
315
|
+
return self._post(
|
|
316
|
+
"finance/daily-prices",
|
|
317
|
+
{
|
|
318
|
+
"tickers": tickers,
|
|
319
|
+
"start": start,
|
|
320
|
+
"end": end,
|
|
321
|
+
},
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
def ip_info(self) -> dict[str, typing.Any]:
|
|
325
|
+
"""GET /ip_info"""
|
|
326
|
+
|
|
327
|
+
return self._get("ip_info")
|