From 634afd4183ff281c2ace0697c0f5cac6c9d39609 Mon Sep 17 00:00:00 2001 From: Daniel Vaz Gaspar Date: Thu, 30 Sep 2021 10:18:26 +0100 Subject: [PATCH] fix: pin elasticsearch-py bellow 7.14 (#71) * fix: pin elasticsearch-py bellow 7.14 * bump requirements.txt * fix mypy with elasticsearch > 7.10 --- es/baseapi.py | 10 ++++++---- es/elastic/api.py | 12 ++++++------ es/exceptions.py | 12 +++++++++++- es/opendistro/api.py | 8 +++++--- requirements.txt | 2 +- setup.py | 2 +- 6 files changed, 30 insertions(+), 16 deletions(-) diff --git a/es/baseapi.py b/es/baseapi.py index a092c5a..320d843 100644 --- a/es/baseapi.py +++ b/es/baseapi.py @@ -130,7 +130,7 @@ def __init__( self.cursors: List[BaseCursor] = [] self.kwargs = kwargs # Subclass needs to initialize Elasticsearch - self.es = None + self.es: Optional[Elasticsearch] = None @check_closed def close(self): @@ -320,9 +320,11 @@ def elastic_query(self, query: str) -> Dict[str, Any]: except es_exceptions.ConnectionError: raise exceptions.OperationalError("Error connecting to Elasticsearch") except es_exceptions.RequestError as ex: - raise exceptions.ProgrammingError( - f"Error ({ex.error}): {ex.info['error']['reason']}" - ) + raise exceptions.ProgrammingError(f"Error ({ex.error}): {ex.info}") + # When method is HEAD and code is 404 perform request returns True + # So response is Union[bool, Any] + if isinstance(response, bool): + raise exceptions.UnexpectedRequestResponse() # Opendistro errors are http status 200 if "error" in response: raise exceptions.ProgrammingError( diff --git a/es/elastic/api.py b/es/elastic/api.py index 13e41f8..ae37d00 100644 --- a/es/elastic/api.py +++ b/es/elastic/api.py @@ -69,9 +69,11 @@ def __init__( @check_closed def cursor(self) -> BaseCursor: """Return a new Cursor Object using the connection.""" - cursor = Cursor(self.url, self.es, **self.kwargs) - self.cursors.append(cursor) - return cursor + if self.es: + cursor = Cursor(self.url, self.es, **self.kwargs) + self.cursors.append(cursor) + return cursor + raise exceptions.UnexpectedESInitError() class Cursor(BaseCursor): @@ -165,9 +167,7 @@ def get_array_type_columns(self, table_name: str) -> "Cursor": f"Error connecting to {self.url}: {e.info}" ) except es_exceptions.NotFoundError as e: - raise exceptions.ProgrammingError( - f"Error ({e.error}): {e.info['error']['reason']}" - ) + raise exceptions.ProgrammingError(f"Error ({e.error}): {e.info}") try: if response["hits"]["total"]["value"] == 0: source = {} diff --git a/es/exceptions.py b/es/exceptions.py index 70116d1..a2a3044 100644 --- a/es/exceptions.py +++ b/es/exceptions.py @@ -1,5 +1,5 @@ class Error(Exception): - pass + """Base exception""" class Warning(Exception): @@ -36,3 +36,13 @@ class DataError(DatabaseError): class NotSupportedError(DatabaseError): pass + + +class UnexpectedESInitError(Error): + """ Should never happen, when a cursor is requested + without an ElasticSearch object being initialized""" + + +class UnexpectedRequestResponse(Error): + """ When perform request returns False, only when HTTP method HEAD + and status code 404 """ diff --git a/es/opendistro/api.py b/es/opendistro/api.py index a27c14b..9f7200b 100644 --- a/es/opendistro/api.py +++ b/es/opendistro/api.py @@ -115,9 +115,11 @@ def _aws_auth(aws_access_key: str, aws_secret_key: str, region: str) -> Any: @check_closed def cursor(self) -> "Cursor": """Return a new Cursor Object using the connection.""" - cursor = Cursor(self.url, self.es, **self.kwargs) - self.cursors.append(cursor) - return cursor + if self.es: + cursor = Cursor(self.url, self.es, **self.kwargs) + self.cursors.append(cursor) + return cursor + raise exceptions.UnexpectedESInitError() class Cursor(BaseCursor): diff --git a/requirements.txt b/requirements.txt index 82bef27..ed00f85 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ # # pip-compile # -elasticsearch==7.0.5 +elasticsearch==7.13.4 # via elasticsearch-dbapi (setup.py) greenlet==1.0.0 # via sqlalchemy diff --git a/setup.py b/setup.py index 265f3d6..b24a862 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,7 @@ "odelasticsearch.https = es.opendistro.sqlalchemy:ESHTTPSDialect", ] }, - install_requires=["elasticsearch>7", "packaging==21.0", "sqlalchemy"], + install_requires=["elasticsearch>7, <7.14", "packaging==21.0", "sqlalchemy"], extras_require={"opendistro": ["requests_aws4auth", "boto3"]}, author="Preset Inc.", author_email="daniel@preset.io",