-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbubble_client.py
289 lines (227 loc) · 7.84 KB
/
bubble_client.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import datetime
import json
import httpx
from thingy import NamesMixin, Thingy, classproperty
class Join:
def __init__(self, cursor_or_cls, key):
if isinstance(cursor_or_cls, Cursor):
self.cursor = cursor_or_cls
self.getter = self.get_from_cursor
else:
self.getter = cursor_or_cls._get_by_id
self.key = key
self.cache = {}
async def get_from_cursor(self, id):
self.cursor.cache = True
self.cursor.rewind()
async for other in self.cursor:
if other._id == id:
return other
async def get(self, id):
if isinstance(id, list):
return [await self.get(i) for i in id]
other = self.cache.get(id)
if not other:
other = await self.getter(id)
self.cache[id] = other
return other
async def __call__(self, thing):
other_id = getattr(thing, self.key)
if other_id:
other = await self.get(other_id)
setattr(thing, self.key, other)
return thing
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, (datetime.date, datetime.datetime)):
return o.isoformat()
if isinstance(o, BubbleThing):
return o.view("bubble")
return json.JSONEncoder.default(self, o)
async def raise_for_status(response):
try:
response.raise_for_status()
except httpx.HTTPStatusError:
await response.aread()
raise
class AsyncClient(httpx.AsyncClient):
_json_encoder = JSONEncoder
def __init__(self, *args, **kwargs):
kwargs.setdefault("event_hooks", {"response": [raise_for_status]})
super().__init__(*args, **kwargs)
@classmethod
def _dump_params(cls, params):
for key, value in params.items():
if not isinstance(value, (str, int)):
params[key] = json.dumps(value, cls=cls._json_encoder)
async def request(self, *args, **kwargs):
params = kwargs.get("params")
if params:
self._dump_params(params)
if "json" in kwargs:
content = kwargs.pop("json")
kwargs["content"] = json.dumps(content, cls=self._json_encoder)
headers = kwargs.get("headers")
if headers is None:
headers = {}
kwargs["headers"] = {**headers, **{"Content-Type": "application/json"}}
return await super().request(*args, **kwargs)
class Cursor:
_json_encoder = JSONEncoder
def __init__(self, cls, params, cache=False):
self.cls = cls
self.params = params
self.index = self.params.get("cursor", 0)
self.page = None
self.joins = []
self.cache = cache
self.cached = []
async def _get_page(self):
self.params["cursor"] = self.index
async with self.cls._get_client() as client:
response = await client.get(
f"/api/1.1/obj/{self.cls.typename}",
params=self.params,
)
return response.json()["response"]
@property
def page_index(self):
return self.index - self.page["cursor"]
def __aiter__(self):
return self
async def __anext__(self):
try:
bubble_object = self.cached[self.index]
except IndexError:
pass
else:
self.index += 1
return bubble_object
if not self.page or (self.page_index == self.page["count"]):
limit = self.params.get("limit")
if not limit or self.index < limit:
self.page = await self._get_page()
try:
bubble_object = self.page["results"][self.page_index]
except IndexError:
raise StopAsyncIteration
self.index += 1
bubble_object = self.cls(bubble_object)
for join in self.joins:
await join(bubble_object)
if self.cache:
self.cached.append(bubble_object)
return bubble_object
async def count(self):
if not self.page:
self.page = await self._get_page()
return self.page["cursor"] + self.page["count"] + self.page["remaining"]
def rewind(self):
self.index = 0
def join(self, key, cursor_or_cls):
join = Join(cursor_or_cls, key)
self.joins.append(join)
class BubbleThing(NamesMixin, Thingy):
_base_url = None
_client_cls = AsyncClient
_json_encoder = JSONEncoder
_headers = {}
_typename = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
__dict__ = {}
includes = []
for key, value in self.__dict__.items():
alias = key.replace(" ", "_").lower()
__dict__[alias] = value
if key not in ("Modified Date", "Created Date", "Created By", "_id"):
includes.append((alias, key))
self.__dict__ = __dict__
self.add_view("bubble", include=includes)
def __eq__(self, other):
return self._id == other._id
def __hash__(self):
return hash(self._id)
@classmethod
def configure(cls, base_url, token, headers=None):
cls._base_url = base_url
if headers is None:
headers = {}
cls._headers = headers
if token:
cls._headers.setdefault("Authorization", f"Bearer {token}")
@classproperty
def base_url(cls):
if cls._base_url:
return cls._base_url
raise AttributeError("Undefined base URL. Call configure().")
@classproperty
def typename(cls):
return cls._typename or "".join(cls.names)
@classmethod
def _get_client(cls):
return cls._client_cls(
base_url=cls._base_url,
headers=cls._headers,
)
@classmethod
def get(cls, **params):
return Cursor(cls, params)
@classmethod
async def _get_by_id(cls, id, **params):
async with cls._get_client() as client:
response = await client.get(
f"/api/1.1/obj/{cls.typename}/{id}",
params=params,
)
bubble_object = response.json()["response"]
if bubble_object:
return cls(bubble_object)
@classmethod
async def _get_first(cls, **params):
params["limit"] = 1
try:
return await cls.get(**params).__anext__()
except StopAsyncIteration:
return None
@classmethod
async def get_one(cls, id=None, **params):
if id:
return await cls._get_by_id(id, **params)
return await cls._get_first(**params)
@classmethod
async def count(cls, **params):
params["limit"] = 1
return await cls.get(**params).count()
async def join(self, key, cursor_or_cls):
return await Join(cursor_or_cls, key)(self)
async def put(self, **params):
async with self._get_client() as client:
await client.put(
f"/api/1.1/obj/{self.__class__.typename}/{self._id}",
params=params,
json=self,
)
return self
async def post(self, **params):
async with self._get_client() as client:
response = await client.post(
f"/api/1.1/obj/{self.__class__.typename}",
params=params,
json=self,
)
self._id = response.json()["id"]
return self
async def delete(self, **params):
async with self._get_client() as client:
await client.delete(
f"/api/1.1/obj/{self.__class__.typename}/{self._id}",
params=params,
)
return self
async def save(self, **params):
if self._id:
return await self.put(**params)
return await self.post(**params)
configure = BubbleThing.configure
__all__ = ("BubbleThing", "configure")