diff --git a/deta/_async/client.py b/deta/_async/client.py index f183253..18c21d5 100644 --- a/deta/_async/client.py +++ b/deta/_async/client.py @@ -6,16 +6,24 @@ from urllib.parse import quote from deta.utils import _get_project_key_id -from deta.base import FetchResponse, Util, insert_ttl, BASE_TTL_ATTTRIBUTE +from deta.base import FetchResponse, Util, insert_ttl, BASE_TTL_ATTRIBUTE -def AsyncBase(name: str): +def AsyncBase(name: str, *, session: aiohttp.ClientSession = None): project_key, project_id = _get_project_key_id() - return _AsyncBase(name, project_key, project_id) + return _AsyncBase(name, project_key, project_id, session=session) class _AsyncBase: - def __init__(self, name: str, project_key: str, project_id: str, host: str = None): + def __init__( + self, + name: str, + project_key: str, + project_id: str, + *, + host: str = None, + session: aiohttp.ClientSession = None + ): if not project_key: raise AssertionError("No Base name provided") @@ -23,9 +31,9 @@ def __init__(self, name: str, project_key: str, project_id: str, host: str = Non self._base_url = f"https://{host}/v1/{project_id}/{name}" self.util = Util() - self.__ttl_attribute = BASE_TTL_ATTTRIBUTE + self.__ttl_attribute = BASE_TTL_ATTRIBUTE - self._session = aiohttp.ClientSession( + self._session = session or aiohttp.ClientSession( headers={ "Content-type": "application/json", "X-API-Key": project_key, diff --git a/deta/base.py b/deta/base.py index c15c127..706235c 100644 --- a/deta/base.py +++ b/deta/base.py @@ -1,6 +1,5 @@ import os import datetime -from re import I import typing from urllib.parse import quote @@ -8,7 +7,7 @@ # timeout for Base service in seconds BASE_SERVICE_TIMEOUT = 300 -BASE_TTL_ATTTRIBUTE = "__expires" +BASE_TTL_ATTRIBUTE = "__expires" class FetchResponse: @@ -131,7 +130,7 @@ def insert( if code == 201: return res elif code == 409: - raise Exception("Item with key '{4}' already exists".format(key)) + raise Exception(f"Item with key '{key}' already exists") def put( self, diff --git a/deta/service.py b/deta/service.py index e1f1cdf..260bc16 100644 --- a/deta/service.py +++ b/deta/service.py @@ -81,16 +81,16 @@ def _request( res.read() if not self.keep_alive: self.client.close() - ## return None if not found + # return None if not found if status == 404: return status, None raise urllib.error.HTTPError(url, status, res.reason, res.headers, res.fp) - ## if stream return the response and client without reading and closing the client + # if stream return the response and client without reading and closing the client if stream: return status, res - ## return json if application/json + # return json if application/json payload = ( json.loads(res.read()) if JSON_MIME in res.getheader("content-type") @@ -109,10 +109,10 @@ def _send_request_with_retry( body: typing.Union[str, bytes, dict] = None, retry=2, # try at least twice to regain a new connection ): - reinitializeConnection = False + reinitialize_connection = False while retry > 0: try: - if not self.keep_alive or reinitializeConnection: + if not self.keep_alive or reinitialize_connection: self.client = http.client.HTTPSConnection( host=self.host, timeout=self.timeout ) @@ -126,5 +126,5 @@ def _send_request_with_retry( res = self.client.getresponse() return res except http.client.RemoteDisconnected: - reinitializeConnection = True + reinitialize_connection = True retry -= 1 diff --git a/deta/utils.py b/deta/utils.py index f94c598..5bd127d 100644 --- a/deta/utils.py +++ b/deta/utils.py @@ -13,4 +13,4 @@ def _get_project_key_id(project_key: str = None, project_id: str = None): if project_id == project_key: raise AssertionError("Bad project key provided") - return project_key, project_id \ No newline at end of file + return project_key, project_id