comparison sat/plugins/plugin_comp_ap_gateway/__init__.py @ 3880:03761f8ba8bb

component AP gateway: raise exceptions on apGet + fix exceptions catching in apGetLocalObject: new `exceptions.ExternalRequestError` is raised when request done `apGet` has a return code >= 300. Fix wrong exception catched in `apGetLocalObject`.
author Goffi <goffi@goffi.org>
date Wed, 31 Aug 2022 17:07:01 +0200
parents c0bcbcf5b4b7
children 2e4a0f6050bd
comparison
equal deleted inserted replaced
3879:46930301f0c1 3880:03761f8ba8bb
323 """Retrieve AP JSON from given URL 323 """Retrieve AP JSON from given URL
324 324
325 @raise error.StanzaError: "service-unavailable" is sent when something went wrong 325 @raise error.StanzaError: "service-unavailable" is sent when something went wrong
326 with AP server 326 with AP server
327 """ 327 """
328 resp = await treq.get(
329 url,
330 headers = {
331 "Accept": [MEDIA_TYPE_AP],
332 "Content-Type": [MEDIA_TYPE_AP],
333 }
334 )
335 if resp.code >= 300:
336 text = await resp.text()
337 if resp.code == 404:
338 raise exceptions.NotFound()
339 else:
340 msg = f"HTTP error {resp.code}: {text}"
341 raise exceptions.ExternalRequestError(msg)
328 try: 342 try:
329 return await treq.json_content(await treq.get( 343 return await treq.json_content(resp)
330 url,
331 headers = {
332 "Accept": [MEDIA_TYPE_AP],
333 "Content-Type": [MEDIA_TYPE_AP],
334 }
335 ))
336 except Exception as e: 344 except Exception as e:
337 raise error.StanzaError( 345 raise error.StanzaError(
338 "service-unavailable", 346 "service-unavailable",
339 text=f"Can't get AP data at {url}: {e}" 347 text=f"Can't get AP data at {url}: {e}"
340 ) 348 )
420 found_items, __ = await self._p.getItems( 428 found_items, __ = await self._p.getItems(
421 self.client, author_jid, node, item_ids=[item_id] 429 self.client, author_jid, node, item_ids=[item_id]
422 ) 430 )
423 try: 431 try:
424 found_item = found_items[0] 432 found_item = found_items[0]
425 except KeyError: 433 except IndexError:
426 raise exceptions.NotFound("requested items can't be found") 434 raise exceptions.NotFound("requested items can't be found")
427 435
428 mb_data = await self._m.item2mbdata( 436 mb_data = await self._m.item2mbdata(
429 self.client, found_item, author_jid, node 437 self.client, found_item, author_jid, node
430 ) 438 )