From 481379d03fccb2ddafbd576be8541e2adf0a0d32 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 11 Jan 2022 11:20:57 +0000 Subject: [PATCH] Switch to HTTP 204 to indicate no data to export This is more robust, as it indicates the request was successfully authenticated and processed, but that there is no data to export. This is different from the URL not existing (which would also happen if the module was unavailable, which should be a notable error instead). --- snikket_web/prosodyclient.py | 4 +++- snikket_web/user.py | 17 ++++++----------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/snikket_web/prosodyclient.py b/snikket_web/prosodyclient.py index c9a98ec..6b530f1 100644 --- a/snikket_web/prosodyclient.py +++ b/snikket_web/prosodyclient.py @@ -1129,11 +1129,13 @@ class ProsodyClient: self, *, session: aiohttp.ClientSession, - ) -> str: + ) -> typing.Optional[str]: async with session.get( self._xep227_endpoint("/export?stores=roster,vcard,pep"), ) as resp: self._raise_error_from_response(resp) + if resp.status == 204: + return None return await resp.text() @autosession diff --git a/snikket_web/user.py b/snikket_web/user.py index 978ae4b..eb9e278 100644 --- a/snikket_web/user.py +++ b/snikket_web/user.py @@ -1,4 +1,3 @@ -import aiohttp import asyncio import typing import urllib @@ -188,16 +187,12 @@ async def manage_data() -> typing.Union[str, quart.Response]: encoded_address = urllib.parse.quote( user_info["address"].encode(encoding='utf-8', errors='strict') ) - try: - account_data = await client.export_account_data() - except aiohttp.ClientResponseError as e: - if e.status == 404: - await flash( - _("You currently have no account data to export."), - "alert" - ) - else: - raise e + account_data = await client.export_account_data() + if account_data is None: + await flash( + _("You currently have no account data to export."), + "alert" + ) else: return Response(account_data, mimetype="application/xml",