99 Awaitable ,
1010 Callable ,
1111 Generic ,
12+ Mapping ,
1213 TypeVar ,
1314 cast ,
1415)
@@ -46,11 +47,11 @@ def __str__(self) -> str:
4647
4748class HttpComponent (ABC ):
4849 @abstractproperty
49- def cli (self ) -> Metablock :
50+ def cli (self ) -> Metablock : # pragma: no cover
5051 ...
5152
5253 @abstractproperty
53- def url (self ) -> str :
54+ def url (self ) -> str : # pragma: no cover
5455 ...
5556
5657 def __repr__ (self ) -> str :
@@ -146,15 +147,14 @@ def __init__(
146147 self .factory = factory
147148
148149 async def paginate (self , ** params : Any ) -> AsyncIterator [E ]:
149- url = self .list_create_url ()
150- next_ = url
151- url_params = as_params (** params )
150+ next_ = self .list_create_url ()
151+ url_params : Any = as_params (** params )
152152 while next_ :
153- if not next_ . startswith ( url ):
154- next_ = f' { url } ? { next_ . split ( "?" )[ 1 ] } '
155- data = await self . cli . request ( next_ , params = url_params )
156- next_ = data . get ( "next" )
157- for d in data [ "data" ] :
153+ next_ , data = await self . cli . request (
154+ next_ , params = url_params , callback = self . _paginated
155+ )
156+ url_params = None
157+ for d in data :
158158 yield self .entity (d )
159159
160160 async def get_list (self , ** kwargs : Any ) -> list [E ]:
@@ -175,7 +175,7 @@ async def get(self, id_: str, **kwargs: Any) -> E:
175175
176176 async def has (self , id_ : str , ** kwargs : Any ) -> bool :
177177 url = f"{ self .url } /{ id_ } "
178- return cast (bool , await self .cli .get (url , callback = self .head ))
178+ return cast (bool , await self .cli .get (url , callback = self ._head ))
179179
180180 async def create (self , callback : Callback | None = None , ** params : Any ) -> E :
181181 url = self .list_create_url ()
@@ -219,14 +219,6 @@ async def delete_all(self) -> int:
219219 n += 1
220220 return n
221221
222- async def head (self , response : ClientResponse ) -> bool :
223- if response .status == 404 :
224- return False
225- elif response .status == 200 :
226- return True
227- else : # pragma: no cover
228- raise MetablockResponseError (response )
229-
230222 def entity (self , data : dict ) -> E :
231223 return self .factory (self , data )
232224
@@ -241,3 +233,22 @@ def update_url(self, id_name: str) -> str:
241233
242234 def delete_url (self , id_name : str ) -> str :
243235 return f"{ self .url } /{ id_name } "
236+
237+ # callbacks
238+
239+ async def _head (self , response : ClientResponse ) -> bool :
240+ if response .status == 404 :
241+ return False
242+ elif response .status == 200 :
243+ return True
244+ else : # pragma: no cover
245+ raise MetablockResponseError (response )
246+
247+ async def _paginated (self , response : ClientResponse ) -> Any :
248+ next = response .links .get ("next" )
249+ if isinstance (next , Mapping ):
250+ url = next .get ("url" )
251+ else :
252+ url = None
253+ data = await self .cli .handle_response (response )
254+ return (url , data )
0 commit comments