Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Clean-Up #48

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions deta/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import json
import typing
from urllib.parse import quote

Expand All @@ -25,11 +24,7 @@ def items(self):
return self._items

def __eq__(self, other):
return (
self.count == other.count
and self.last == other.last
and self.items == other.items
)
return self.count == other.count and self.last == other.last and self.items == other.items


class Util:
Expand Down Expand Up @@ -111,9 +106,7 @@ def insert(self, data: typing.Union[dict, list, str, int, bool], key: str = None
if key:
data["key"] = key

code, res = self._request(
"/items", "POST", {"item": data}, content_type=JSON_MIME
)
code, res = self._request("/items", "POST", {"item": data}, content_type=JSON_MIME)
if code == 201:
return res
elif code == 409:
Expand All @@ -133,9 +126,7 @@ def put(self, data: typing.Union[dict, list, str, int, bool], key: str = None):
if key:
data["key"] = key

code, res = self._request(
"/items", "PUT", {"items": [data]}, content_type=JSON_MIME
)
code, res = self._request("/items", "PUT", {"items": [data]}, content_type=JSON_MIME)
return res["processed"]["items"][0] if res and code == 207 else None

def put_many(self, items: typing.List[typing.Union[dict, list, str, int, bool]]):
Expand All @@ -147,9 +138,7 @@ def put_many(self, items: typing.List[typing.Union[dict, list, str, int, bool]])
else:
_items.append(i)

_, res = self._request(
"/items", "PUT", {"items": _items}, content_type=JSON_MIME
)
_, res = self._request("/items", "PUT", {"items": _items}, content_type=JSON_MIME)
return res

def _fetch(
Expand Down
12 changes: 3 additions & 9 deletions deta/drive.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import os
import typing
import http
from io import BufferedIOBase, TextIOBase, RawIOBase, StringIO, BytesIO
from urllib.parse import quote_plus
import http.client

from .service import JSON_MIME, _Service

Expand All @@ -28,7 +26,7 @@ def iter_chunks(self, chunk_size: int = 1024):
if not chunk:
break
yield chunk

def iter_lines(self, chunk_size: int = 1024):
while True:
chunk = self.__stream.readline(chunk_size)
Expand Down Expand Up @@ -73,9 +71,7 @@ def get(self, name: str):
Returns a DriveStreamingBody.
"""
assert name, "No name provided"
_, res = self._request(
f"/files/download?name={self._quote(name)}", "GET", stream=True
)
_, res = self._request(f"/files/download?name={self._quote(name)}", "GET", stream=True)
if res:
return DriveStreamingBody(res)
return None
Expand All @@ -87,9 +83,7 @@ def delete_many(self, names: typing.List[str]):
"""
assert names, "Names is empty"
assert len(names) <= 1000, "More than 1000 names to delete"
_, res = self._request(
"/files", "DELETE", {"names": names}, content_type=JSON_MIME
)
_, res = self._request("/files", "DELETE", {"names": names}, content_type=JSON_MIME)
return res

def delete(self, name: str):
Expand Down
12 changes: 3 additions & 9 deletions deta/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def __init__(
self.host = host
self.timeout = timeout
self.keep_alive = keep_alive
self.client = (
http.client.HTTPSConnection(host, timeout=timeout) if keep_alive else None
)
self.client = http.client.HTTPSConnection(host, timeout=timeout) if keep_alive else None

def _is_socket_closed(self):
if not self.client.sock:
Expand Down Expand Up @@ -92,9 +90,7 @@ def _request(

## return json if application/json
payload = (
json.loads(res.read())
if JSON_MIME in res.getheader("content-type")
else res.read()
json.loads(res.read()) if JSON_MIME in res.getheader("content-type") else res.read()
)

if not self.keep_alive:
Expand All @@ -113,9 +109,7 @@ def _send_request_with_retry(
while retry > 0:
try:
if not self.keep_alive or reinitializeConnection:
self.client = http.client.HTTPSConnection(
host=self.host, timeout=self.timeout
)
self.client = http.client.HTTPSConnection(host=self.host, timeout=self.timeout)

self.client.request(
method,
Expand Down
8 changes: 2 additions & 6 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,7 @@ def test_fetch(self):
)
self.assertEqual(res3, expectedItem)

res4 = self.db.fetch(
[{"value?gt": 6}, {"value?lt": 50}], limit=2, last="existing2"
)
res4 = self.db.fetch([{"value?gt": 6}, {"value?lt": 50}], limit=2, last="existing2")
expectedItem = FetchResponse(
1,
None,
Expand Down Expand Up @@ -317,9 +315,7 @@ def test_update(self):
self.assertEqual(self.db.get("existing4"), expectedItem)

self.assertIsNone(
self.db.update(
{"value.name": self.db.util.trim(), "value.age": 32}, "existing4"
)
self.db.update({"value.name": self.db.util.trim(), "value.age": 32}, "existing4")
)
expectedItem = {"key": "existing4", "value": {"age": 32}}
self.assertEqual(self.db.get("existing4"), expectedItem)
Expand Down