refactor: extract request logic
This commit is contained in:
@@ -1,11 +1,9 @@
|
|||||||
import aiohttp
|
import aiohttp
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
from datetime import date
|
from ppgee.http import HttpClient
|
||||||
from ppgee.errors import RequestException
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
URL_BASE = "https://www.ppgee.ufmg.br/ppgeenet"
|
|
||||||
|
|
||||||
|
|
||||||
class PPGEE:
|
class PPGEE:
|
||||||
@@ -13,9 +11,11 @@ class PPGEE:
|
|||||||
self.user = user
|
self.user = user
|
||||||
self.password = password
|
self.password = password
|
||||||
self.session: aiohttp.ClientSession
|
self.session: aiohttp.ClientSession
|
||||||
|
self.http: HttpClient
|
||||||
|
|
||||||
async def __aenter__(self):
|
async def __aenter__(self):
|
||||||
self.session = aiohttp.ClientSession()
|
self.session = aiohttp.ClientSession()
|
||||||
|
self.http = HttpClient(self.session)
|
||||||
await self.login()
|
await self.login()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@@ -25,33 +25,14 @@ class PPGEE:
|
|||||||
if self.session:
|
if self.session:
|
||||||
await self.session.close()
|
await self.session.close()
|
||||||
|
|
||||||
async def _request(self, method: str, url: str, **kwargs) -> str:
|
|
||||||
async with self.session.request(method, url, **kwargs) as resp:
|
|
||||||
if resp.status != 200:
|
|
||||||
raise RequestException(
|
|
||||||
f"Request to url {url} with method {method} failed with status code {resp.status}."
|
|
||||||
)
|
|
||||||
return await resp.text()
|
|
||||||
|
|
||||||
async def login(self) -> str:
|
async def login(self) -> str:
|
||||||
logger.debug("Sending request to login.")
|
return await self.http.login(self.user, self.password)
|
||||||
data: dict[str, str] = {"login": self.user, "senha": self.password}
|
|
||||||
return await self._request("post", f"{URL_BASE}/login.php", data=data)
|
|
||||||
|
|
||||||
async def frequency(self) -> str:
|
async def frequency(self) -> str:
|
||||||
logger.debug("Sending request to frequency.")
|
return await self.http.frequency()
|
||||||
return await self._request("get", f"{URL_BASE}/afreq.php")
|
|
||||||
|
|
||||||
async def frequency_confirmation(self) -> str:
|
async def frequency_confirmation(self) -> str:
|
||||||
logger.debug("Sending request to confirm frequency.")
|
return await self.http.frequency_confirmation()
|
||||||
today = date.today()
|
|
||||||
data: dict[str, str] = {
|
|
||||||
"freqano": str(today.year),
|
|
||||||
"freqmes": str(today.month),
|
|
||||||
"confirma": "checkbox",
|
|
||||||
}
|
|
||||||
return await self._request("POST", f"{URL_BASE}/afreqpasso2.php", data=data)
|
|
||||||
|
|
||||||
async def logoff(self) -> str:
|
async def logoff(self) -> str:
|
||||||
logger.debug("Sending request to logoff.")
|
return await self.http.logoff()
|
||||||
return await self._request("get", f"{URL_BASE}/logoff.php")
|
|
||||||
|
|||||||
42
ppgee/http.py
Normal file
42
ppgee/http.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
from datetime import date
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
|
from ppgee.errors import RequestException
|
||||||
|
|
||||||
|
URL_BASE = "https://www.ppgee.ufmg.br/ppgeenet"
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class HttpClient:
|
||||||
|
def __init__(self, session: aiohttp.ClientSession):
|
||||||
|
self.session = session
|
||||||
|
|
||||||
|
async def _request(self, method: str, url: str, **kwargs) -> str:
|
||||||
|
async with self.session.request(method, url, **kwargs) as resp:
|
||||||
|
logger.debug(f"Request: {url} Status: {resp.status}")
|
||||||
|
if resp.status != 200:
|
||||||
|
raise RequestException(
|
||||||
|
f"Request to url {url} with method {method} failed with status code {resp.status}."
|
||||||
|
)
|
||||||
|
return await resp.text()
|
||||||
|
|
||||||
|
async def login(self, user: str, password: str) -> str:
|
||||||
|
data: dict[str, str] = {"login": user, "senha": password}
|
||||||
|
return await self._request("post", f"{URL_BASE}/login.php", data=data)
|
||||||
|
|
||||||
|
async def frequency(self) -> str:
|
||||||
|
return await self._request("get", f"{URL_BASE}/afreq.php")
|
||||||
|
|
||||||
|
async def frequency_confirmation(self) -> str:
|
||||||
|
today = date.today()
|
||||||
|
data: dict[str, str] = {
|
||||||
|
"freqano": str(today.year),
|
||||||
|
"freqmes": str(today.month),
|
||||||
|
"confirma": "checkbox",
|
||||||
|
}
|
||||||
|
return await self._request("POST", f"{URL_BASE}/afreqpasso2.php", data=data)
|
||||||
|
|
||||||
|
async def logoff(self) -> str:
|
||||||
|
return await self._request("get", f"{URL_BASE}/logoff.php")
|
||||||
Reference in New Issue
Block a user