diff --git a/src/linkahead/connection/connection.py b/src/linkahead/connection/connection.py index 140cb8cd46ad0f850fb7543634d0e31d4b7139ce..22035aefc124ffdc519f4c078ca71c0d42d5515b 100644 --- a/src/linkahead/connection/connection.py +++ b/src/linkahead/connection/connection.py @@ -63,7 +63,7 @@ if TYPE_CHECKING and sys.version_info > (3, 7): from typing import Optional, List, Any, Iterator, Dict, Union from requests.models import Response from ssl import _SSLMethod - from .authentication.interface import AbstractAuthenticator + from .authentication.interface import AbstractAuthenticator, CredentialsAuthenticator _LOGGER = logging.getLogger(__name__) @@ -105,6 +105,7 @@ class _WrappedHTTPResponse(CaosDBHTTPResponse): if len(self._buffer) >= size: # still enough bytes in the buffer + # FIXME: `chunk`` is used before definition result = chunk[:size] self._buffer = chunk[size:] return result @@ -172,7 +173,7 @@ class _DefaultCaosDBServerConnection(CaosDBServerConnection): method: str, path: str, headers: Optional[Dict[str, str]] = None, body: Union[str, bytes, None] = None, - **kwargs): + **kwargs) -> _WrappedHTTPResponse: """request. Send a HTTP request to the server. @@ -192,7 +193,7 @@ class _DefaultCaosDBServerConnection(CaosDBServerConnection): Returns ------- - response : CaosDBHTTPResponse + response : _WrappedHTTPResponse """ if headers is None: @@ -552,7 +553,7 @@ class _Connection(object): # pylint: disable=useless-object-inheritance def __init__(self): self._delegate_connection: Optional[CaosDBServerConnection] = None - self._authenticator: Optional[AbstractAuthenticator] = None + self._authenticator: Optional[CredentialsAuthenticator] = None self.is_configured = False @classmethod @@ -600,14 +601,15 @@ class _Connection(object): # pylint: disable=useless-object-inheritance def retrieve(self, entity_uri_segments: Optional[List[str]] = None, query_dict: Optional[Dict[str, Optional[str]]] = None, - **kwargs): + **kwargs) -> CaosDBHTTPResponse: path = make_uri_path(entity_uri_segments, query_dict) http_response = self._http_request(method="GET", path=path, **kwargs) return http_response - def delete(self, entity_uri_segments=None, query_dict=None, **kwargs): + def delete(self, entity_uri_segments: Optional[List[str]] = None, + query_dict: Optional[Dict[str, Optional[str]]] = None, **kwargs) -> CaosDBHTTPResponse: path = make_uri_path(entity_uri_segments, query_dict) http_response = self._http_request( @@ -615,15 +617,17 @@ class _Connection(object): # pylint: disable=useless-object-inheritance return http_response - def update(self, entity_uri_segment, query_dict=None, **kwargs): + def update(self, entity_uri_segment: Optional[List[str]], + query_dict: Optional[Dict[str, Optional[str]]] = None, **kwargs) -> CaosDBHTTPResponse: path = make_uri_path(entity_uri_segment, query_dict) http_response = self._http_request(method="PUT", path=path, **kwargs) return http_response - def activate_user(self, link): - self._authenticator.logout() + def activate_user(self, link: str) -> CaosDBHTTPResponse: + if self._authenticator is not None: + self._authenticator.logout() fullurl = urlparse(link) path = fullurl.path query = fullurl.query @@ -632,17 +636,17 @@ class _Connection(object): # pylint: disable=useless-object-inheritance return http_response - def put_form_data(self, entity_uri_segment, params): + def put_form_data(self, entity_uri_segment: str, params) -> CaosDBHTTPResponse: return self._form_data_request( method="PUT", path=entity_uri_segment, params=params) - def post_form_data(self, entity_uri_segment, params): + def post_form_data(self, entity_uri_segment: str, params: Dict[str, Optional[str]]) -> CaosDBHTTPResponse: return self._form_data_request( method="POST", path=entity_uri_segment, params=params) - def _form_data_request(self, method, path, params): + def _form_data_request(self, method: str, path: str, params: Dict[str, Optional[str]]) -> CaosDBHTTPResponse: body = urlencode(params) headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" @@ -654,7 +658,9 @@ class _Connection(object): # pylint: disable=useless-object-inheritance return response - def insert(self, entity_uri_segment, query_dict=None, body=None, **kwargs): + def insert(self, entity_uri_segment: Optional[List[str]], + query_dict: Optional[Dict[str, Optional[str]]] = None, + body: Union[str, bytes, None] = None, **kwargs) -> CaosDBHTTPResponse: path = make_uri_path(entity_uri_segment, query_dict) http_response = self._http_request( @@ -679,7 +685,9 @@ class _Connection(object): # pylint: disable=useless-object-inheritance def _logout(self): self._authenticator.logout() - def _http_request(self, method, path, headers=None, body=None, **kwargs): + def _http_request(self, method: str, path: str, + headers: Optional[Dict["str", Any]] = None, + body: Union[str, bytes, None] = None, **kwargs): try: return self._retry_http_request(method=method, path=path, headers=headers, body=body, @@ -706,9 +714,9 @@ class _Connection(object): # pylint: disable=useless-object-inheritance method: str, path: str, headers: Optional[Dict["str", Any]], - body: Union[str, bytes], **kwargs) -> CaosDBHTTPResponse: + body: Union[str, bytes, None], **kwargs) -> CaosDBHTTPResponse: - if hasattr(body, "encode"): + if hasattr(body, "encode") and body is not None: # python3 body = body.encode("utf-8")