Skip to content

Commit

Permalink
Allow HTTP connection to Metabase (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
gouline authored Jan 29, 2020
1 parent fd1f571 commit ae931aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 7 additions & 4 deletions dbtmetabase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from .dbt import DbtReader
from .metabase import MetabaseClient

__version__ = '0.1.4'
__version__ = '0.1.5'

def export(dbt_path: str,
mb_host: str, mb_user: str, mb_password: str,
mb_host: str, mb_user: str, mb_password: str,
database: str, schema: str,
sync = True, sync_timeout = 30):
mb_https = True, sync = True, sync_timeout = 30):
"""Exports models from dbt to Metabase.
Arguments:
Expand All @@ -20,11 +20,12 @@ def export(dbt_path: str,
schema {str} -- Target schema name.
Keyword Arguments:
mb_https {bool} -- Use HTTPS to connect to Metabase instead of HTTP. (default: {True})
sync {bool} -- Synchronize Metabase database before export. (default: {True})
sync_timeout {int} -- Synchronization timeout in seconds. (default: {30})
"""

mbc = MetabaseClient(mb_host, mb_user, mb_password)
mbc = MetabaseClient(mb_host, mb_user, mb_password, mb_https)
models = DbtReader(dbt_path).read_models()

if sync:
Expand All @@ -47,6 +48,7 @@ def main(args: list = None):
parser.add_argument('--mb_host', metavar='HOST', required=True, help="Metabase hostname")
parser.add_argument('--mb_user', metavar='USER', required=True, help="Metabase username")
parser.add_argument('--mb_password', metavar='PASS', required=True, help="Metabase password")
parser.add_argument('--mb_https', metavar='HTTPS', type=bool, default=True, help="use HTTPS to connect to Metabase instead of HTTP")
parser.add_argument('--database', metavar='DB', required=True, help="target database name")
parser.add_argument('--schema', metavar='SCHEMA', required=True, help="target schema name")
parser.add_argument('--sync', metavar='ENABLE', type=bool, default=True, help="synchronize Metabase database before export")
Expand All @@ -59,6 +61,7 @@ def main(args: list = None):
mb_host=parsed.mb_host,
mb_user=parsed.mb_user,
mb_password=parsed.mb_password,
mb_https=parsed.mb_https,
database=parsed.database,
schema=parsed.schema,
sync=parsed.sync,
Expand Down
9 changes: 6 additions & 3 deletions dbtmetabase/metabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ class MetabaseClient:

_SYNC_PERIOD_SECS = 5

def __init__(self, host: str, user: str, password: str):
def __init__(self, host: str, user: str, password: str, https = True):
"""Constructor.
Arguments:
host {str} -- Metabase hostname.
user {str} -- Metabase username.
password {str} -- Metabase password.
Keyword Arguments:
https {bool} -- Use HTTPS instead of HTTP. (default: {True})
"""

self.host = host
self.protocol = "https" if https else "http"
self.session_id = self.get_session_id(user, password)
logging.info("Session established successfully")

Expand Down Expand Up @@ -283,10 +287,9 @@ def api(self, method: str, path: str, authenticated = True, critical = True, **k
if authenticated:
headers['X-Metabase-Session'] = self.session_id

response = requests.request(method, f"https://{self.host}{path}", **kwargs)
response = requests.request(method, f"{self.protocol}://{self.host}{path}", **kwargs)
if critical:
response.raise_for_status()
elif not response.ok:
return False
return json.loads(response.text)

0 comments on commit ae931aa

Please sign in to comment.