ABle to send the booking request to several courts at the same time

This commit is contained in:
Stanislas Jouffroy 2024-02-15 19:55:07 +01:00
parent fcc08f03f1
commit 51af600d28
11 changed files with 288 additions and 99 deletions

View file

@ -1,11 +1,11 @@
import asyncio
import json
import logging
from aiohttp import ClientSession, ClientResponse
from resa_padel.gestion_sports.gestion_sports_payload_builder import (
GestionSportsPayloadBuilder,
)
from resa_padel.models import User
from aiohttp import ClientResponse, ClientSession
from gestion_sports.gestion_sports_payload_builder import \
GestionSportsPayloadBuilder
from models import BookingFilter, User
LOGGER = logging.getLogger(__name__)
HEADERS = {
@ -30,13 +30,21 @@ class GestionSportsConnector:
self.session = session
self.payload_builder = GestionSportsPayloadBuilder()
def __exit__(self, exc_type, exc_val, exc_tb):
self.session.close()
@property
def connection_url(self) -> str:
return f"{self.url}/connexion.php?"
@property
def login_url(self) -> str:
return f"{self.url}/connexion.php?"
@property
def booking_url(self) -> str:
return f"{self.url}/membre/reservation.html?"
async def connect(self) -> ClientResponse:
LOGGER.info("Connecting to GestionSports API")
connection_url = self.url + "/connexion.php?"
async with self.session.get(connection_url) as response:
async with self.session.get(self.connection_url) as response:
await response.text()
return response
@ -48,29 +56,48 @@ class GestionSportsConnector:
.build_login_payload()
)
login_url = f"{self.url}/connexion.php?"
async with self.session.post(
login_url, data=payload, headers=HEADERS
self.login_url, data=payload, headers=HEADERS
) as response:
await response.text()
return response
async def book(self, booking_filter) -> ClientResponse:
async def book(self, booking_filter: BookingFilter) -> int | None:
bookings = await asyncio.gather(
*[
self.book_one_court(booking_filter, court_id)
for court_id in booking_filter.court_ids
],
return_exceptions=True,
)
for court, is_booked in bookings:
if is_booked:
return court
return None
async def book_one_court(
self, booking_filter: BookingFilter, court_id: int
) -> tuple[int, bool]:
date_format = "%d/%m/%Y"
time_format = "%H:%M"
payload = (
self.payload_builder.date(booking_filter.date.date().strftime(date_format))
.time(booking_filter.date.time().strftime(time_format))
.sport_id(booking_filter.sport_id)
.court_id(booking_filter.court_id)
.court_id(court_id)
.build_booking_payload()
)
return court_id, await self.is_court_booked(payload)
booking_url = f"{self.url}/membre/reservation.html?"
async def is_court_booked(self, payload: str) -> bool:
async with self.session.post(
booking_url, data=payload, headers=HEADERS
self.booking_url, data=payload, headers=HEADERS
) as response:
await response.text()
return response
return self.is_response_status_ok(await response.text())
@staticmethod
def is_response_status_ok(response: str) -> bool:
formatted_result = response.removeprefix('"').removesuffix('"')
result_json = json.loads(formatted_result)
return result_json["status"] == "ok"

View file

@ -1,4 +1,4 @@
from resa_padel.exceptions import ArgumentMissing
from exceptions import ArgumentMissing
class GestionSportsPayloadBuilder:
@ -11,31 +11,31 @@ class GestionSportsPayloadBuilder:
self._sport_id = None
self._court_id = None
def login(self, login):
def login(self, login: str):
self._login = login
return self
def password(self, password):
def password(self, password: str):
self._password = password
return self
def club_id(self, club_id):
def club_id(self, club_id: str):
self._club_id = club_id
return self
def date(self, date):
def date(self, date: str):
self._date = date
return self
def time(self, time):
def time(self, time: str):
self._time = time
return self
def sport_id(self, sport_id):
def sport_id(self, sport_id: int):
self._sport_id = sport_id
return self
def court_id(self, court_id):
def court_id(self, court_id: int):
self._court_id = court_id
return self