Skip to content

Commit

Permalink
Added Client class and login method
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeusina committed Aug 8, 2023
1 parent ff257e5 commit 96f668c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions petersbugredu_wrap/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import petersbugredu_wrap.client
40 changes: 40 additions & 0 deletions petersbugredu_wrap/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import json
import logging
import requests
from petersbugredu_wrap.utils import endpoints
from petersbugredu_wrap.errors.invalid_login_or_password_exc import InvalidLoginOrPasswordException


class Client:
def __init__(self):
self._token = None
self.logger = logging.getLogger(__name__)
self.logger.debug("Client was created")

def login(self, login: str, password: str):
url = endpoints.LOGIN_URL
self.logger.debug("Preparing payload & headers to login as %login%".replace("%login%", login))
payload = (
'{"type": "email", "login": "%login%", "activation_code": null, "password": "%password%", "_isEmpty": '
'false}'.replace("%login%", login).replace("%password%", password))
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/114.0.0.0 Safari/537.36',
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
}
self.logger.debug("Sending login request as %login%".replace("%login%", login))
response = requests.request("POST", url, headers=headers, data=payload)
self.logger.debug(
"Request was successfully sent, status code: %code%".replace("%code%", str(response.status_code)))
if response.status_code == 200:
json_response: dict = json.loads(response.text)
token = json_response.get("data", None).get("token", None)
if token:
self._token = token
else:
raise IndexError
elif response.status_code == 400:
raise InvalidLoginOrPasswordException
else:
raise ValueError

0 comments on commit 96f668c

Please sign in to comment.