@@ -26,6 +26,8 @@ class BaseRepository(Api):
2626 ENTITY_CLASS = BaseEntity
2727 ADDITIONAL_METHODS = {}
2828 EXCLUDED_METHODS = ()
29+ ADDITIONAL_PARAMS = {}
30+ DATA_CONTAINER = {}
2931
3032 def __init__ (self , base_url = None , auth = None ):
3133 super ().__init__ (base_url = base_url , auth = auth )
@@ -67,60 +69,75 @@ def additionat_method(
6769 _url = (self .DETAIL_URL + "/" + url + "/{additional_id}" ).format (
6870 id = _id , additional_id = additional_id
6971 )
70- return self ._retrieve (_url , entity , headers = self .HEADERS )
72+ return self ._retrieve (
73+ _url , entity , headers = self .HEADERS , params = params
74+ )
7175 elif _id :
7276 _url = (self .DETAIL_URL + "/" + url ).format (id = _id )
73- return self ._list (_url , entity , headers = self .HEADERS )
77+ return self ._list (_url , entity , headers = self .HEADERS , param = params )
7478 elif single_item :
7579 _url = str (self .BASE_URL ) + "/" + url
7680 return self ._retrieve (
77- _url , entity , headers = self .HEADERS , data_key = "data"
81+ _url ,
82+ entity ,
83+ headers = self .HEADERS ,
84+ params = params ,
85+ data_key = "data" ,
7886 )
7987 else :
8088 raise NotSupported
8189
8290 def _retrieve (self , _url , entity_class , data_key : str = "data" , ** kwargs ):
83- response = self .get (_url )
91+ params = kwargs
92+ params .update (self .ADDITIONAL_PARAMS .get ("retrieve" , {}))
93+
94+ response = self .get (_url , params = params )
8495 data = response .json ()
96+ data_key = data_key or self .DATA_CONTAINER .get ('retrieve' , None )
8597 if data_key :
8698 data = data [data_key ]
8799 return entity_class (** data )
88- else :
89- return entity_class (** data )
90100
91101 def retrieve (self , id : int = None , ** kwargs ):
92102 full_url = self .BASE_URL .join (self .DETAIL_URL .format (id = id ))
93103 return self ._retrieve (full_url , self .ENTITY_CLASS )
94104
95- def _list (self , _url , entity_class , ** kwargs ):
96- response = self .get (_url )
97- return [entity_class (** entity ) for entity in response .json ()]
105+ def _list (self , _url , entity_class , data_key : str = None , ** kwargs ):
106+ params = kwargs
107+ params .update (self .ADDITIONAL_PARAMS .get ("list" , {}))
108+
109+ response = self .get (_url , params = params )
110+ data = response .json ()
111+ data_key = data_key or self .DATA_CONTAINER .get ('list' , None )
112+ if data_key :
113+ data = data [data_key ]
114+ return [entity_class (** entity ) for entity in data ]
98115
99116 def list (self , ** kwargs ):
100117 if "list" in self .EXCLUDED_METHODS :
101118 raise MethodNotAllowed
102119 full_url = self .BASE_URL .join (self .LIST_URL )
103- return self ._list (full_url , self .ENTITY_CLASS )
120+ return self ._list (full_url , self .ENTITY_CLASS , ** kwargs )
104121
105- def create (self , entity : ENTITY_CLASS ):
122+ def create (self , entity : ENTITY_CLASS , ** kwargs ):
106123 if "create" in self .EXCLUDED_METHODS :
107124 raise MethodNotAllowed
108125 full_url = self .BASE_URL .join (self .LIST_URL )
109- response = self .post (full_url , data = entity .dict ())
126+ response = self .post (full_url , data = entity .dict (), ** kwargs )
110127 return self .ENTITY_CLASS (** response .json ())
111128
112- def update (self , entity : ENTITY_CLASS ):
129+ def update (self , entity : ENTITY_CLASS , ** kwargs ):
113130 if "update" in self .EXCLUDED_METHODS :
114131 raise MethodNotAllowed
115132 full_url = self .BASE_URL .join (self .DETAIL_URL .format (id = entity .id ))
116- response = self .put (full_url , data = entity .dict ())
133+ response = self .put (full_url , data = entity .dict (), ** kwargs )
117134 return self .ENTITY_CLASS (** response .json ())
118135
119- def partial_update (self , entity : ENTITY_CLASS ):
136+ def partial_update (self , entity : ENTITY_CLASS , ** kwargs ):
120137 if "partial_update" in self .EXCLUDED_METHODS :
121138 raise MethodNotAllowed
122139 full_url = self .BASE_URL .join (self .DETAIL_URL .format (id = entity .id ))
123- response = self .patch (full_url , data = entity .dict ())
140+ response = self .patch (full_url , data = entity .dict (), ** kwargs )
124141 return self .ENTITY_CLASS (** response .json ())
125142
126143
0 commit comments