Handle HTTP errors gracefully
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
				
			|||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
 | 
					import logging
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from collections import OrderedDict
 | 
					from collections import OrderedDict
 | 
				
			||||||
@@ -58,11 +59,25 @@ class CombinedCalendarClient:
 | 
				
			|||||||
        self.configured_calendars = configured_calendars
 | 
					        self.configured_calendars = configured_calendars
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def fetch_calendars(self) -> Dict[ConfiguredCalendar, str]:
 | 
					    async def fetch_calendars(self) -> Dict[ConfiguredCalendar, str]:
 | 
				
			||||||
        async with aiohttp.ClientSession() as session:
 | 
					        async with aiohttp.ClientSession(headers={
 | 
				
			||||||
            calendar_clients = [CalendarClient(calendar.url) for calendar in self.configured_calendars]
 | 
					            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0",
 | 
				
			||||||
            responses = await asyncio.gather(*[calendar.get(session) for calendar in calendar_clients])
 | 
					        }) 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
 | 
					    @staticmethod
 | 
				
			||||||
    def combine_calendars(data: Dict[ConfiguredCalendar, str]) -> Calendar:
 | 
					    def combine_calendars(data: Dict[ConfiguredCalendar, str]) -> Calendar:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user