@@ -11,8 +11,9 @@ class Resource:
1111 ENDPOINT : str
1212 PRIMARY_KEY : str = "id"
1313
14- def __init__ (self , ** kwargs ):
14+ def __init__ (self , _using : Metabase , ** kwargs ):
1515 self ._attributes = []
16+ self ._using = _using
1617
1718 for k , v in kwargs .items ():
1819 self ._attributes .append (k )
@@ -31,42 +32,38 @@ def __repr__(self):
3132 + ")"
3233 )
3334
34- @staticmethod
35- def connection () -> Metabase :
36- return Metabase ()
37-
3835
3936class ListResource (Resource ):
4037 @classmethod
41- def list (cls ):
38+ def list (cls , using : Metabase ):
4239 """List all instances."""
43- response = cls . connection () .get (cls .ENDPOINT )
44- records = [cls (** record ) for record in response .json ()]
40+ response = using .get (cls .ENDPOINT )
41+ records = [cls (_using = using , ** record ) for record in response .json ()]
4542 return records
4643
4744
4845class GetResource (Resource ):
4946 @classmethod
50- def get (cls , id : int ):
47+ def get (cls , id : int , using : Metabase ):
5148 """Get a single instance by ID."""
52- response = cls . connection () .get (cls .ENDPOINT + f"/{ id } " )
49+ response = using .get (cls .ENDPOINT + f"/{ id } " )
5350
5451 if response .status_code == 404 or response .status_code == 204 :
5552 raise NotFoundError (f"{ cls .__name__ } (id={ id } ) was not found." )
5653
57- return cls (** response .json ())
54+ return cls (_using = using , ** response .json ())
5855
5956
6057class CreateResource (Resource ):
6158 @classmethod
62- def create (cls , ** kwargs ):
59+ def create (cls , using : Metabase , ** kwargs ):
6360 """Create an instance and save it."""
64- response = cls . connection () .post (cls .ENDPOINT , json = kwargs )
61+ response = using .post (cls .ENDPOINT , json = kwargs )
6562
6663 if response .status_code not in (200 , 202 ):
6764 raise HTTPError (response .content .decode ())
6865
69- return cls (** response .json ())
66+ return cls (_using = using , ** response .json ())
7067
7168
7269class UpdateResource (Resource ):
@@ -77,11 +74,11 @@ def update(self, **kwargs) -> None:
7774 ignored from the request.
7875 """
7976 params = {k : v for k , v in kwargs .items () if v != MISSING }
80- response = self .connection () .put (
77+ response = self ._using .put (
8178 self .ENDPOINT + f"/{ getattr (self , self .PRIMARY_KEY )} " , json = params
8279 )
8380
84- if response .status_code != 200 :
81+ if response .status_code not in ( 200 , 202 ) :
8582 raise HTTPError (response .json ())
8683
8784 for k , v in kwargs .items ():
@@ -91,7 +88,7 @@ def update(self, **kwargs) -> None:
9188class DeleteResource (Resource ):
9289 def delete (self ) -> None :
9390 """Delete an instance."""
94- response = self .connection () .delete (
91+ response = self ._using .delete (
9592 self .ENDPOINT + f"/{ getattr (self , self .PRIMARY_KEY )} "
9693 )
9794
0 commit comments