Refactoring for reading the config
This commit is contained in:
parent
963ee6b86f
commit
fc11a1e1eb
9 changed files with 264 additions and 166 deletions
|
@ -1,25 +1,20 @@
|
|||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from aiohttp import ClientResponse, ClientSession
|
||||
from gestion_sports.gestion_sports_payload_builder import \
|
||||
GestionSportsPayloadBuilder
|
||||
from models import BookingFilter, User
|
||||
|
||||
import config
|
||||
from gestion_sports.gestion_sports_payload_builder import GestionSportsPayloadBuilder
|
||||
from models import BookingFilter, Club, User
|
||||
|
||||
DATE_FORMAT = "%d/%m/%Y"
|
||||
|
||||
TIME_FORMAT = "%H:%M"
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
HEADERS = {
|
||||
"Connection": "keep-alive",
|
||||
"Accept-Language": "en-US,en;q=0.5",
|
||||
"Accept-Encoding": "gzip, deflate, br",
|
||||
"DNT": "1",
|
||||
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
||||
"Accept": "application/json, text/javascript, */*; q=0.01",
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
"Sec-Fetch-Dest": "empty",
|
||||
"Sec-Fetch-Mode": "cors",
|
||||
"Sec-Fetch-Site": "same-origin",
|
||||
}
|
||||
POST_HEADERS = config.get_post_headers("gestion-sports")
|
||||
|
||||
|
||||
class GestionSportsConnector:
|
||||
|
@ -32,15 +27,15 @@ class GestionSportsConnector:
|
|||
|
||||
@property
|
||||
def connection_url(self) -> str:
|
||||
return f"{self.url}/connexion.php?"
|
||||
return urljoin(self.url, "/connexion.php?")
|
||||
|
||||
@property
|
||||
def login_url(self) -> str:
|
||||
return f"{self.url}/connexion.php?"
|
||||
return urljoin(self.url, "/connexion.php?")
|
||||
|
||||
@property
|
||||
def booking_url(self) -> str:
|
||||
return f"{self.url}/membre/reservation.html?"
|
||||
return urljoin(self.url, "/membre/reservation.html?")
|
||||
|
||||
async def connect(self) -> ClientResponse:
|
||||
LOGGER.info("Connecting to GestionSports API")
|
||||
|
@ -48,29 +43,35 @@ class GestionSportsConnector:
|
|||
await response.text()
|
||||
return response
|
||||
|
||||
async def login(self, user: User) -> ClientResponse:
|
||||
async def login(self, user: User, club: Club) -> ClientResponse:
|
||||
payload = (
|
||||
self.payload_builder.login(user.login)
|
||||
.password(user.password)
|
||||
.club_id(user.club_id)
|
||||
.club_id(club.id)
|
||||
.build_login_payload()
|
||||
)
|
||||
|
||||
async with self.session.post(
|
||||
self.login_url, data=payload, headers=HEADERS
|
||||
self.login_url, data=payload, headers=POST_HEADERS
|
||||
) as response:
|
||||
await response.text()
|
||||
return response
|
||||
|
||||
async def book(self, booking_filter: BookingFilter) -> int | None:
|
||||
async def book(self, booking_filter: BookingFilter, club: Club) -> int | None:
|
||||
# use asyncio to request a booking on every court
|
||||
# the gestion-sports backend is able to book only one court for a user
|
||||
bookings = await asyncio.gather(
|
||||
*[
|
||||
self.book_one_court(booking_filter, court_id)
|
||||
for court_id in booking_filter.court_ids
|
||||
for court_id in club.courts_ids
|
||||
],
|
||||
return_exceptions=True,
|
||||
)
|
||||
|
||||
return await self.get_booked_court(bookings)
|
||||
|
||||
@staticmethod
|
||||
async def get_booked_court(bookings):
|
||||
for court, is_booked in bookings:
|
||||
if is_booked:
|
||||
return court
|
||||
|
@ -79,11 +80,9 @@ class GestionSportsConnector:
|
|||
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))
|
||||
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(court_id)
|
||||
.build_booking_payload()
|
||||
|
@ -92,7 +91,7 @@ class GestionSportsConnector:
|
|||
|
||||
async def is_court_booked(self, payload: str) -> bool:
|
||||
async with self.session.post(
|
||||
self.booking_url, data=payload, headers=HEADERS
|
||||
self.booking_url, data=payload, headers=POST_HEADERS
|
||||
) as response:
|
||||
return self.is_response_status_ok(await response.text())
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue