22
33from typing import Any , Dict , List
44
5+ from metabase import Metabase
56from metabase .missing import MISSING
67from metabase .resource import (
78 CreateResource ,
@@ -47,18 +48,19 @@ class Database(
4748 created_at : str
4849
4950 @classmethod
50- def list (cls ) -> List [Database ]:
51- response = cls . connection () .get (cls .ENDPOINT )
52- records = [cls (** db ) for db in response .json ().get ("data" , [])]
51+ def list (cls , using : Metabase ) -> List [Database ]:
52+ response = using .get (cls .ENDPOINT )
53+ records = [cls (_using = using , ** db ) for db in response .json ().get ("data" , [])]
5354 return records
5455
5556 @classmethod
56- def get (cls , id : int ) -> Database :
57- return super (Database , cls ).get (id )
57+ def get (cls , id : int , using : Metabase ) -> Database :
58+ return super (Database , cls ).get (id , using = using )
5859
5960 @classmethod
6061 def create (
6162 cls ,
63+ using : Metabase ,
6264 name : str ,
6365 engine : str ,
6466 details : dict ,
@@ -75,6 +77,7 @@ def create(
7577 You must be a superuser to do this.
7678 """
7779 return super (Database , cls ).create (
80+ using = using ,
7881 name = name ,
7982 engine = engine ,
8083 details = details ,
@@ -128,51 +131,41 @@ def delete(self) -> None:
128131
129132 def fields (self ) -> List [Field ]:
130133 """Get a list of all Fields in Database."""
131- fields = (
132- self .connection ()
133- .get (self .ENDPOINT + f"/{ getattr (self , self .PRIMARY_KEY )} " + "/fields" )
134- .json ()
135- )
136- return [Field (** payload ) for payload in fields ]
134+ fields = self ._using .get (
135+ self .ENDPOINT + f"/{ getattr (self , self .PRIMARY_KEY )} " + "/fields"
136+ ).json ()
137+ return [Field (_using = self ._using , ** payload ) for payload in fields ]
137138
138139 def idfields (self ) -> List [Field ]:
139140 """Get a list of all primary key Fields for Database."""
140- fields = (
141- self .connection ()
142- .get (self .ENDPOINT + f"/{ getattr (self , self .PRIMARY_KEY )} " + "/idfields" )
143- .json ()
144- )
145- return [Field (** payload ) for payload in fields ]
141+ fields = self ._using .get (
142+ self .ENDPOINT + f"/{ getattr (self , self .PRIMARY_KEY )} " + "/idfields"
143+ ).json ()
144+ return [Field (_using = self ._using , ** payload ) for payload in fields ]
146145
147146 def schemas (self ) -> List [str ]:
148147 """Returns a list of all the schemas found for the database id."""
149- return (
150- self .connection ()
151- .get (self .ENDPOINT + f"/{ getattr (self , self .PRIMARY_KEY )} " + "/schemas" )
152- .json ()
153- )
148+ return self ._using .get (
149+ self .ENDPOINT + f"/{ getattr (self , self .PRIMARY_KEY )} " + "/schemas"
150+ ).json ()
154151
155152 def tables (self , schema : str ) -> List [Table ]:
156153 """Returns a list of Tables for the given Database id and schema."""
157- tables = (
158- self .connection ()
159- .get (
160- self .ENDPOINT
161- + f"/{ getattr (self , self .PRIMARY_KEY )} "
162- + "/schema"
163- + f"/{ schema } "
164- )
165- .json ()
166- )
167- return [Table (** payload ) for payload in tables ]
154+ tables = self ._using .get (
155+ self .ENDPOINT
156+ + f"/{ getattr (self , self .PRIMARY_KEY )} "
157+ + "/schema"
158+ + f"/{ schema } "
159+ ).json ()
160+ return [Table (_using = self ._using , ** payload ) for payload in tables ]
168161
169162 def discard_values (self ):
170163 """
171164 Discards all saved field values for this Database.
172165
173166 You must be a superuser to do this.
174167 """
175- return self .connection () .post (
168+ return self ._using .post (
176169 self .ENDPOINT + f"/{ getattr (self , self .PRIMARY_KEY )} " + "/discard_values"
177170 )
178171
@@ -182,13 +175,13 @@ def rescan_values(self):
182175
183176 You must be a superuser to do this.
184177 """
185- return self .connection () .post (
178+ return self ._using .post (
186179 self .ENDPOINT + f"/{ getattr (self , self .PRIMARY_KEY )} " + "/rescan_values"
187180 )
188181
189182 def sync (self ):
190183 """Update the metadata for this Database. This happens asynchronously."""
191- return self .connection () .post (
184+ return self ._using .post (
192185 self .ENDPOINT + f"/{ getattr (self , self .PRIMARY_KEY )} " + "/sync"
193186 )
194187
@@ -198,6 +191,6 @@ def sync_schema(self):
198191
199192 You must be a superuser to do this.
200193 """
201- return self .connection () .post (
194+ return self ._using .post (
202195 self .ENDPOINT + f"/{ getattr (self , self .PRIMARY_KEY )} " + "/sync_schema"
203196 )
0 commit comments