init python-api lib

This commit is contained in:
starnakin 2023-11-02 23:26:05 +01:00
commit 66398936f0
4 changed files with 43 additions and 0 deletions

30
src/Client.py Normal file
View File

@ -0,0 +1,30 @@
import requests
from requets.Session import Session
from requests import Response, Requests
import urls
class Client:
def __init__(self, url: str):
self.url: str = url
self.token: str = None
self.session: Session = Session()
response: Response = s.get(self.url)
self.csrf_token = response.cookies['csrftoken']
def is_authentificate(self: Client):
return (not self.token is None)
def login(self: Client, username: str, password: str):
response: Response = self._post(urls.accounts_login_url, {'username': username, 'password': password})
return response.content
def register(self: Client, username: str, password: str):
response: Response = self._post(urls.accounts_login_url, {'username': username, 'password': password})
return response.content
def _post(self: Client, uri: str, data: dict):
url: str = self.url + uri
data.update({'csrfmiddlewaretoken': self.csrf_token})
response: Response = s.post(url, data = data, headers = dict(Referer=url))
return response.content

3
src/urls.py Normal file
View File

@ -0,0 +1,3 @@
accounts_url: str = "/api/accounts/"
accounts_login_url: str = "login"
accounts_register_url: str = "register"

0
tests/accounts/login.py Normal file
View File

View File

@ -0,0 +1,10 @@
from unittest import TestCase
import uuid
class RegisterTest(TestCase):
def setUp(self):
self.username: str = uuid.uuid4()
self.password: str = uuid.uuid4()
def test_valid