Skip to content

Commit

Permalink
Revert changes and add optional http_adapter parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexis.flipo committed Dec 5, 2023
1 parent 2582a9f commit c4fd6ab
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 42 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 6 additions & 21 deletions dbtmetabase/metabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -122,14 +122,14 @@ 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,
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.
Expand All @@ -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}"
Expand All @@ -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

Expand Down
8 changes: 0 additions & 8 deletions dbtmetabase/models/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
15 changes: 5 additions & 10 deletions dbtmetabase/models/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -103,25 +103,20 @@ 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,
password=self.password,
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:
Expand Down
1 change: 0 additions & 1 deletion requirements-extra.txt

This file was deleted.

1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down

0 comments on commit c4fd6ab

Please sign in to comment.