import logging from aiohttp import ClientSession from connectors import Connector from models import BookingFilter, Club, User LOGGER = logging.getLogger(__name__) class BookingService: def __init__(self, club: Club, connector: Connector): LOGGER.info("Initializing booking service at for club", club.name) self.club = club self.connector = connector self.session: ClientSession | None = None async def __aenter__(self): self.session = ClientSession() return self async def __aexit__(self, exc_type, exc_val, exc_tb): await self.session.close() async def book(self, user: User, booking_filter: BookingFilter) -> int | None: """ Book a court matching the booking filters for a user. The steps to perform a booking are to go to the landing page, to log in, wait and for the time when booking is open and then actually book the court :param user: the user that wants to book a court :param booking_filter: the booking criteria :return: the court number if the booking is successful, None otherwise """ if self.connector is None: LOGGER.error("No connection to Gestion Sports is available") return None if user is None or booking_filter is None: LOGGER.error("Not enough information available to book a court") return None self.connector.book(user, booking_filter)