Skip to content

Commit f9e5fd3

Browse files
HiteshGoranahitesh-goranaCaedenPH
authored
Refactor : API calling methods (#49)
Co-authored-by: Hitesh <hitesh.g@smarter.codes> Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>
1 parent a5e5263 commit f9e5fd3

File tree

1 file changed

+33
-53
lines changed

1 file changed

+33
-53
lines changed

surrealdb/http.py

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ async def select(self, thing: str) -> List[Dict[str, Any]]:
203203
Select a specific record from a table (or other entity)
204204
person = await db.select('person:h5wxrf2ewk8xjxosxtyc')
205205
"""
206-
if ":" in thing:
207-
table, id = thing.split(":")
208-
response = await self._request(method="GET", uri=f"/key/{table}/{id}")
209-
if not response:
210-
raise SurrealException(f"Key {id} not found in table {table}")
211-
else:
212-
response = await self._request(method="GET", uri=f"/key/{thing}")
206+
table, record_id = thing.split(":") if ":" in thing else (thing, None)
207+
response = await self._request(
208+
method="GET",
209+
uri=f"/key/{table}/{record_id}" if record_id else f"/key/{table}",
210+
)
211+
if not response and record_id is not None:
212+
raise SurrealException(f"Key {record_id} not found in table {table}")
213213
return response[0]['result']
214214

215215
async def create(self, thing: str, data: Optional[Dict[str, Any]] = None) -> str:
@@ -235,20 +235,16 @@ async def create(self, thing: str, data: Optional[Dict[str, Any]] = None) -> str
235235
},
236236
})
237237
"""
238-
if ":" in thing:
239-
table, id = thing.split(":")
240-
response = await self._request(
241-
method="POST",
242-
uri=f"/key/{table}/{id}",
243-
data=json.dumps(data),
244-
)
245-
else:
246-
response = await self._request(
247-
method="POST",
248-
uri=f"/key/{thing}",
249-
data=json.dumps(data),
250-
)
238+
table, record_id = thing.split(":") if ":" in thing else (thing, None)
239+
response = await self._request(
240+
method="POST",
241+
uri=f"/key/{table}/{record_id}" if record_id else f"/key/{table}",
242+
data=json.dumps(data),
243+
)
244+
if not response and record_id is not None:
245+
raise SurrealException(f"Key {record_id} not found in table {table}")
251246
return response[0]['result']
247+
252248

253249
async def update(self, thing: str, data: Any) -> Dict[str, Any]:
254250
"""Updates all records in a table, or a specific record, in the database.
@@ -276,20 +272,12 @@ async def update(self, thing: str, data: Any) -> Dict[str, Any]:
276272
},
277273
})
278274
"""
279-
if ":" in thing:
280-
table, id = thing.split(":")
281-
response = await self._request(
282-
method="PUT",
283-
uri=f"/key/{table}/{id}",
284-
data=json.dumps(data),
285-
)
286-
else:
287-
table, id = thing.split(":")
288-
response = await self._request(
289-
method="PUT",
290-
uri=f"/key/{thing}", # need to test if this works
291-
data=json.dumps(data),
292-
)
275+
table, record_id = thing.split(":") if ":" in thing else (thing, None)
276+
response = await self._request(
277+
method="PUT",
278+
uri=f"/key/{table}/{record_id}" if record_id else f"/key/{table}",
279+
data=json.dumps(data),
280+
)
293281
return response[0]['result']
294282

295283
async def patch(self, thing: str, data: Any) -> Dict[str, Any]:
@@ -317,20 +305,12 @@ async def patch(self, thing: str, data: Any) -> Dict[str, Any]:
317305
{ 'op': "remove", "path": "/temp" },
318306
])
319307
"""
320-
if ":" in thing:
321-
table, id = thing.split(":")
322-
response = await self._request(
323-
method="PATCH",
324-
uri=f"/key/{table}/{id}",
325-
data=json.dumps(data),
326-
)
327-
else:
328-
table, id = thing.split(":")
329-
response = await self._request(
330-
method="PATCH",
331-
uri=f"/key/{thing}", # need to test if this works
332-
data=json.dumps(data),
333-
)
308+
table, record_id = thing.split(":") if ":" in thing else (thing, None)
309+
response = await self._request(
310+
method="PATCH",
311+
uri=f"/key/{table}/{record_id}" if record_id else f"/key/{table}",
312+
data=json.dumps(data),
313+
)
334314
return response[0]['result']
335315

336316
async def delete(self, thing: str) -> None:
@@ -348,9 +328,9 @@ async def delete(self, thing: str) -> None:
348328
Delete a specific record from a table
349329
await db.delete('person:h5wxrf2ewk8xjxosxtyc')
350330
"""
351-
if ":" in thing:
352-
table, id = thing.split(":")
353-
response = await self._request(method="DELETE", uri=f"/key/{table}/{id}")
354-
else:
355-
response = await self._request(method="DELETE", uri=f"/key/{thing}")
331+
table, record_id = thing.split(":") if ":" in thing else (thing, None)
332+
response = await self._request(
333+
method="DELETE",
334+
uri=f"/key/{table}/{record_id}" if record_id else f"/key/{table}",
335+
)
356336
return response

0 commit comments

Comments
 (0)