From cdeca9498cdab97919b9d9c50e296815663604cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=BCller?= Date: Sun, 23 Feb 2025 17:18:43 +0100 Subject: [PATCH] Handle HTTP errors gracefully --- fabcal/calendar_client.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/fabcal/calendar_client.py b/fabcal/calendar_client.py index cde581e..fa672d3 100644 --- a/fabcal/calendar_client.py +++ b/fabcal/calendar_client.py @@ -1,4 +1,5 @@ import asyncio +import logging import os from collections import OrderedDict @@ -58,11 +59,25 @@ class CombinedCalendarClient: self.configured_calendars = configured_calendars async def fetch_calendars(self) -> Dict[ConfiguredCalendar, str]: - async with aiohttp.ClientSession() as session: - calendar_clients = [CalendarClient(calendar.url) for calendar in self.configured_calendars] - responses = await asyncio.gather(*[calendar.get(session) for calendar in calendar_clients]) + async with aiohttp.ClientSession(headers={ + "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0", + }) as session: + # need to make sure we return the calendar and the gathered values together + async def coro(calendar): + print("argh", calendar) + return calendar, await CalendarClient(calendar.url).get(session) - return dict(zip(self.configured_calendars, responses)) + rv = {} + + for coro in asyncio.as_completed([coro(calendar) for calendar in self.configured_calendars]): + try: + cal, response = await coro + rv[cal] = response + + except aiohttp.client_exceptions.ClientResponseError: + logging.exception("Failed to fetch data for calendar, skipping") + + return rv @staticmethod def combine_calendars(data: Dict[ConfiguredCalendar, str]) -> Calendar: