From 66398936f0c99364fa148eff6fd41629ba3a9089 Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 2 Nov 2023 23:26:05 +0100 Subject: [PATCH] init python-api lib --- src/Client.py | 30 ++++++++++++++++++++++++++++++ src/urls.py | 3 +++ tests/accounts/login.py | 0 tests/accounts/register.py | 10 ++++++++++ 4 files changed, 43 insertions(+) create mode 100644 src/Client.py create mode 100644 src/urls.py create mode 100644 tests/accounts/login.py create mode 100644 tests/accounts/register.py diff --git a/src/Client.py b/src/Client.py new file mode 100644 index 0000000..2688e96 --- /dev/null +++ b/src/Client.py @@ -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 \ No newline at end of file diff --git a/src/urls.py b/src/urls.py new file mode 100644 index 0000000..afdb53e --- /dev/null +++ b/src/urls.py @@ -0,0 +1,3 @@ +accounts_url: str = "/api/accounts/" +accounts_login_url: str = "login" +accounts_register_url: str = "register" \ No newline at end of file diff --git a/tests/accounts/login.py b/tests/accounts/login.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/accounts/register.py b/tests/accounts/register.py new file mode 100644 index 0000000..bd3ce71 --- /dev/null +++ b/tests/accounts/register.py @@ -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 \ No newline at end of file