44from enum import Enum
55from typing import TYPE_CHECKING , override
66
7- import aiohttp
87import bs4
98import discord
109from discord .ext import tasks
1110
1211from config import settings
13- from utils import GLOBAL_SSL_CONTEXT , CommandChecks , TeXBotBaseCog
12+ from utils import CommandChecks , TeXBotBaseCog
1413from utils .error_capture_decorators import (
1514 capture_guild_does_not_exist_error ,
1615)
16+ from utils .msl import fetch_url_content_with_session
1717
1818if TYPE_CHECKING :
19- from collections .abc import Iterable , Mapping , Sequence
19+ from collections .abc import Iterable , Sequence
2020 from collections .abc import Set as AbstractSet
2121 from logging import Logger
2222 from typing import Final
3131
3232logger : "Final[Logger]" = logging .getLogger ("TeX-Bot" )
3333
34- REQUEST_HEADERS : "Final[Mapping[str, str]]" = {
35- "Cache-Control" : "no-cache" ,
36- "Pragma" : "no-cache" ,
37- "Expires" : "0" ,
38- }
39-
40- REQUEST_COOKIES : "Final[Mapping[str, str]]" = {
41- ".AspNet.SharedCookie" : settings ["SU_PLATFORM_ACCESS_COOKIE" ]
42- }
4334
4435SU_PLATFORM_PROFILE_URL : "Final[str]" = "https://guildofstudents.com/profile"
4536SU_PLATFORM_ORGANISATION_URL : "Final[str]" = (
@@ -76,20 +67,10 @@ class SUPlatformAccessCookieStatus(Enum):
7667class CheckSUPlatformAuthorisationBaseCog (TeXBotBaseCog ):
7768 """Cog class that defines the base functionality for cookie authorisation checks."""
7869
79- async def _fetch_url_content_with_session (self , url : str ) -> str :
80- """Fetch the HTTP content at the given URL, using a shared aiohttp session."""
81- async with (
82- aiohttp .ClientSession (
83- headers = REQUEST_HEADERS , cookies = REQUEST_COOKIES
84- ) as http_session ,
85- http_session .get (url = url , ssl = GLOBAL_SSL_CONTEXT ) as http_response ,
86- ):
87- return await http_response .text ()
88-
8970 async def get_su_platform_access_cookie_status (self ) -> SUPlatformAccessCookieStatus :
9071 """Retrieve the current validity status of the SU platform access cookie."""
9172 response_object : bs4 .BeautifulSoup = bs4 .BeautifulSoup (
92- await self . _fetch_url_content_with_session (SU_PLATFORM_PROFILE_URL ), "html.parser"
73+ await fetch_url_content_with_session (SU_PLATFORM_PROFILE_URL ), "html.parser"
9374 )
9475 page_title : bs4 .Tag | bs4 .NavigableString | None = response_object .find ("title" )
9576 if not page_title or "Login" in str (page_title ):
@@ -99,7 +80,7 @@ async def get_su_platform_access_cookie_status(self) -> SUPlatformAccessCookieSt
9980 organisation_admin_url : str = (
10081 f"{ SU_PLATFORM_ORGANISATION_URL } /{ settings ['ORGANISATION_ID' ]} "
10182 )
102- response_html : str = await self . _fetch_url_content_with_session (organisation_admin_url )
83+ response_html : str = await fetch_url_content_with_session (organisation_admin_url )
10384
10485 if "admin tools" in response_html .lower ():
10586 return SUPlatformAccessCookieStatus .AUTHORISED
@@ -115,7 +96,7 @@ async def get_su_platform_access_cookie_status(self) -> SUPlatformAccessCookieSt
11596 async def get_su_platform_organisations (self ) -> "Iterable[str]" :
11697 """Retrieve the MSL organisations the current SU platform cookie has access to."""
11798 response_object : bs4 .BeautifulSoup = bs4 .BeautifulSoup (
118- await self . _fetch_url_content_with_session (SU_PLATFORM_PROFILE_URL ), "html.parser"
99+ await fetch_url_content_with_session (SU_PLATFORM_PROFILE_URL ), "html.parser"
119100 )
120101
121102 page_title : bs4 .Tag | bs4 .NavigableString | None = response_object .find ("title" )
0 commit comments