From c4fd6ab054743c7d42084f57be8405e6f6ace9ea Mon Sep 17 00:00:00 2001 From: "alexis.flipo" Date: Tue, 5 Dec 2023 10:35:27 +0100 Subject: [PATCH] Revert changes and add optional http_adapter parameter --- Makefile | 1 - dbtmetabase/metabase.py | 27 ++++++--------------------- dbtmetabase/models/exceptions.py | 8 -------- dbtmetabase/models/interface.py | 15 +++++---------- requirements-extra.txt | 1 - setup.py | 1 - 6 files changed, 11 insertions(+), 42 deletions(-) delete mode 100644 requirements-extra.txt diff --git a/Makefile b/Makefile index 3c19af2..ead0822 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,6 @@ clean: requirements: pip3 install -r requirements.txt pip3 install -r requirements-test.txt - pip3 install -r requirements-extra.txt .PHONY: requirements diff --git a/dbtmetabase/metabase.py b/dbtmetabase/metabase.py index 8e8dbcc..1e7c0f8 100644 --- a/dbtmetabase/metabase.py +++ b/dbtmetabase/metabase.py @@ -16,7 +16,7 @@ import requests import yaml -from requests.adapters import HTTPAdapter, Retry +from requests.adapters import BaseAdapter, HTTPAdapter, Retry from .logger.logging import logger from .models import exceptions @@ -122,7 +122,6 @@ def __init__( password: Optional[str], verify: Optional[Union[str, bool]] = None, cert: Optional[Union[str, Tuple[str, str]]] = None, - pkcs12_data: Optional[Union[str, Tuple[str, str]]] = None, session_id: Optional[str] = None, use_http: bool = False, sync: Optional[bool] = True, @@ -130,6 +129,7 @@ def __init__( exclude_sources: bool = False, http_extra_headers: Optional[dict] = None, http_timeout: int = 15, + http_adapter: Optional[BaseAdapter] = None, ): """Constructor. @@ -142,12 +142,12 @@ def __init__( use_http {bool} -- Use HTTP instead of HTTPS. (default: {False}) verify {Union[str, bool]} -- Path to certificate or disable verification. (default: {None}) cert {Union[str, Tuple[str, str]]} -- Path to a custom certificate to be used by the Metabase client. (default: {None}) - pkcs12_data (Optional[Tuple[str, str]], optional): PKCS#12 Certificate content with its password. If the certificate is not physically present on the running host. (default: {None}) session_id {str} -- Metabase session ID. (default: {None}) sync (bool, optional): Attempt to synchronize Metabase schema with local models. Defaults to True. sync_timeout (Optional[int], optional): Synchronization timeout (in secs). Defaults to None. http_extra_headers {dict} -- HTTP headers to be used by the Metabase client. (default: {None}) exclude_sources {bool} -- Exclude exporting sources. (default: {False}) + http_adapter: (Optional[object], optional) Provides a general-case interface for Requests sessions to contact HTTP and HTTPS urls by implementing the Transport Adapter interface. Defaults to None. """ self.base_url = f"{'http' if use_http else 'https'}://{host}" @@ -159,25 +159,10 @@ def __init__( if http_extra_headers is not None: self.session.headers.update(http_extra_headers) - adaptor = HTTPAdapter(max_retries=Retry(total=3, backoff_factor=0.5)) + if not http_adapter: + http_adapter = HTTPAdapter(max_retries=Retry(total=3, backoff_factor=0.5)) - if pkcs12_data is not None: - try: - from requests_pkcs12 import ( - Pkcs12Adapter, - ) # pylint: disable=E0401 - - adaptor = Pkcs12Adapter( - pkcs12_data=pkcs12_data[0], - pkcs12_password=pkcs12_data[1], - ) - except ImportError as exc: - raise exceptions.ExtraLibraryInstallationError( - "The requests-pkcs12 extra library is not installed." - "Please install it by running `pip install dbt-metabase['pkcs12']`" - ) from exc - - self.session.mount(self.base_url, adaptor) + self.session.mount(self.base_url, http_adapter) session_header = session_id or self.get_session_id(user, password) self.session.headers["X-Metabase-Session"] = session_header diff --git a/dbtmetabase/models/exceptions.py b/dbtmetabase/models/exceptions.py index 4f36a64..730a78d 100644 --- a/dbtmetabase/models/exceptions.py +++ b/dbtmetabase/models/exceptions.py @@ -16,11 +16,3 @@ class MetabaseUnableToSync(Exception): class MetabaseRuntimeError(Exception): """Thrown when Metabase execution failed.""" - - -class MetabaseCertificateImplementationError(Exception): - """Thrown when cert argument and pkcs12_data argument are both defined""" - - -class ExtraLibraryInstallationError(Exception): - """Thrown when an extra library is not installed but imported""" diff --git a/dbtmetabase/models/interface.py b/dbtmetabase/models/interface.py index 037b365..fa2d171 100644 --- a/dbtmetabase/models/interface.py +++ b/dbtmetabase/models/interface.py @@ -7,12 +7,12 @@ from ..parsers.dbt_folder import DbtFolderReader from ..parsers.dbt_manifest import DbtManifestReader from .exceptions import ( - MetabaseCertificateImplementationError, NoDbtPathSupplied, NoDbtSchemaSupplied, NoMetabaseCredentialsSupplied, ) from .metabase import MetabaseModel +from requests.adapters import BaseAdapter class MetabaseInterface: @@ -30,12 +30,12 @@ def __init__( use_http: bool = False, verify: Optional[Union[str, bool]] = True, cert: Optional[Union[str, Tuple[str, str]]] = None, - pkcs12_data: Optional[Union[str, Tuple[str, str]]] = None, sync: bool = True, sync_timeout: Optional[int] = None, exclude_sources: bool = False, http_extra_headers: Optional[dict] = None, http_timeout: int = 15, + http_adapter: Optional[BaseAdapter] = None, ): """Constructor. @@ -48,11 +48,11 @@ def __init__( use_http (bool, optional): Use HTTP to connect to Metabase.. Defaults to False. verify (Optional[Union[str, bool]], optional): Path to custom certificate bundle to be used by Metabase client. Defaults to True. cert (Optional[Union[str, Tuple[str, str]]], optional): Path to a custom certificate to be used by the Metabase client, or a tuple containing the path to the certificate and key. Defaults to None. - pkcs12_data (Optional[Tuple[str, str]], optional): PKCS#12 Certificate content with its password. If the certificate is not physically present on the running host. (default: {None}) sync (bool, optional): Attempt to synchronize Metabase schema with local models. Defaults to True. sync_timeout (Optional[int], optional): Synchronization timeout (in secs). Defaults to None. exclude_sources (bool, optional): Exclude exporting sources. Defaults to False. http_extra_headers (Optional[dict], optional): HTTP headers to be used by the Metabase client. Defaults to None. + http_adapter: (Optional[object], optional) Provides a general-case interface for Requests sessions to contact HTTP and HTTPS urls by implementing the Transport Adapter interface. Defaults to None. """ # Metabase Client @@ -65,9 +65,9 @@ def __init__( self.use_http = use_http self.verify = verify self.cert = cert - self.pkcs12_data = pkcs12_data self.http_extra_headers = dict(http_extra_headers) if http_extra_headers else {} self.http_timeout = http_timeout + self.http_adapter = http_adapter # Metabase Sync self.sync = sync self.sync_timeout = sync_timeout @@ -103,11 +103,6 @@ def prepare_metabase_client(self, dbt_models: Optional[List[MetabaseModel]] = No "Credentials or session ID not supplied" ) - if self.cert and self.pkcs12_data: - raise MetabaseCertificateImplementationError( - "cert and pkcs12_data arguments can't be both defined" - ) - self._client = MetabaseClient( host=self.host, user=self.user, @@ -115,13 +110,13 @@ def prepare_metabase_client(self, dbt_models: Optional[List[MetabaseModel]] = No use_http=self.use_http, verify=self.verify, cert=self.cert, - pkcs12_data=self.pkcs12_data, http_extra_headers=self.http_extra_headers, session_id=self.session_id, sync=self.sync, sync_timeout=self.sync_timeout, exclude_sources=self.exclude_sources, http_timeout=self.http_timeout, + http_adapter=self.http_adapter, ) # Sync and attempt schema alignment prior to execution; if timeout is not explicitly set, proceed regardless of success if self.sync: diff --git a/requirements-extra.txt b/requirements-extra.txt deleted file mode 100644 index b73d53d..0000000 --- a/requirements-extra.txt +++ /dev/null @@ -1 +0,0 @@ -requests-pkcs12>=1.22 \ No newline at end of file diff --git a/setup.py b/setup.py index d871e40..ccb118d 100755 --- a/setup.py +++ b/setup.py @@ -31,7 +31,6 @@ def requires_from_file(filename: str) -> list: install_requires=requires_from_file("requirements.txt"), extras_require={ "test": requires_from_file("requirements-test.txt"), - "pkcs12": requires_from_file("requirements-extra.txt"), }, setup_requires=["setuptools_scm"], classifiers=[