Compare commits
10 Commits
4938e05c4a
...
22ff3d58d3
Author | SHA1 | Date | |
---|---|---|---|
22ff3d58d3 | |||
617b67df24 | |||
a17f09e04b | |||
3c96defea3 | |||
1900a6f390 | |||
dc8b64573e | |||
3a0c41e7f4 | |||
8441eb98e5 | |||
519a405baf | |||
e1d102a9c6 |
23
README.md
Normal file
23
README.md
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# Python lib
|
||||||
|
A python lib to interract with the api more easier
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
- Clone the project:
|
||||||
|
``` bash
|
||||||
|
git clone https://git.chauvet.pro/michel/ft_transcendence
|
||||||
|
cd ft_transcendence
|
||||||
|
git switch python-api
|
||||||
|
```
|
||||||
|
- Create python virtual environnement.
|
||||||
|
``` bash
|
||||||
|
python3 -m venv .env
|
||||||
|
```
|
||||||
|
- Source the environnement.
|
||||||
|
``` bash
|
||||||
|
source .env/bin/activate
|
||||||
|
```
|
||||||
|
- Install the requirements
|
||||||
|
``` bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
5
requirements.txt
Normal file
5
requirements.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
certifi==2023.7.22
|
||||||
|
charset-normalizer==3.3.2
|
||||||
|
idna==3.4
|
||||||
|
requests==2.31.0
|
||||||
|
urllib3==2.0.7
|
3
setup.py
Normal file
3
setup.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
|
setup(name='transcendence_api', version='1.0', packages=find_packages())
|
@ -1,14 +0,0 @@
|
|||||||
import urls
|
|
||||||
import Client
|
|
||||||
|
|
||||||
from requests import Response
|
|
||||||
|
|
||||||
class Profile:
|
|
||||||
|
|
||||||
def __init__(self, client, user_id: int):
|
|
||||||
response: Response = client._get(urls.profiles_page + str(user_id))
|
|
||||||
if (response.content == b'Profile Not Found'):
|
|
||||||
return None
|
|
||||||
content: dict = eval(response.content)
|
|
||||||
self.username = content['username']
|
|
||||||
self.password = content['password']
|
|
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
@ -1,6 +1,9 @@
|
|||||||
import urls
|
from src import urls
|
||||||
|
|
||||||
class Account:
|
from requests import Response
|
||||||
|
|
||||||
|
|
||||||
|
class Accounts:
|
||||||
|
|
||||||
def __init__(self, client):
|
def __init__(self, client):
|
||||||
self._client = client
|
self._client = client
|
@ -1,18 +1,17 @@
|
|||||||
import requests
|
import requests
|
||||||
from requests import Response, Request, Session
|
from requests import Response, Request, Session
|
||||||
|
|
||||||
import Profile
|
from src.profiles import Profiles
|
||||||
import Accounts
|
from src.accounts import Accounts
|
||||||
|
from src import urls
|
||||||
import urls
|
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
def __init__(self, url: str):
|
def __init__(self, url: str):
|
||||||
self.url: str = url
|
self.url: str = url
|
||||||
self.token: str = None
|
self.token: str = None
|
||||||
self.session: Session = Session()
|
self.session: Session = Session()
|
||||||
self.accounts: Account = Accounts.Account(self)
|
self.accounts: Accounts = Accounts(self)
|
||||||
self.profile = Profile.Profile
|
self.profiles: Profiles = Profiles(self)
|
||||||
|
|
||||||
def is_authentificate(self):
|
def is_authentificate(self):
|
||||||
return (not self.token is None)
|
return (not self.token is None)
|
27
src/profile.py
Normal file
27
src/profile.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
|
||||||
|
|
||||||
|
class Profile:
|
||||||
|
|
||||||
|
def __init__(self, data: dict = None, username: str = None, title: str = None):
|
||||||
|
|
||||||
|
if (data is None):
|
||||||
|
self._from_value(username, title)
|
||||||
|
else:
|
||||||
|
self._from_dict(data)
|
||||||
|
|
||||||
|
def _from_value(self, username: str, title: str):
|
||||||
|
self.username = username
|
||||||
|
self.title = title
|
||||||
|
return self
|
||||||
|
|
||||||
|
def _from_dict(self, data: dict):
|
||||||
|
self._from_value(data.get('username'), data.get('title'))
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
if isinstance(other, Profile):
|
||||||
|
return self.username == other.username and self.title == other.title
|
||||||
|
return False
|
||||||
|
|
||||||
|
def __ne__(self, other):
|
||||||
|
return not self.__eq__(other)
|
18
src/profiles.py
Normal file
18
src/profiles.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
from src import urls
|
||||||
|
|
||||||
|
from src.profile import Profile
|
||||||
|
|
||||||
|
|
||||||
|
from requests import Response
|
||||||
|
|
||||||
|
class Profiles:
|
||||||
|
|
||||||
|
def __init__(self, client):
|
||||||
|
self.client = client
|
||||||
|
|
||||||
|
def get(self, user_id: int):
|
||||||
|
response: Response = self.client._get(urls.profiles_page + str(user_id))
|
||||||
|
if response.status_code == 404:
|
||||||
|
return None
|
||||||
|
content: dict = eval(response.content)
|
||||||
|
return Profile(data = content)
|
56
src/tests.py
56
src/tests.py
@ -1,56 +0,0 @@
|
|||||||
import Client
|
|
||||||
from urls import *
|
|
||||||
|
|
||||||
from uuid import uuid4
|
|
||||||
|
|
||||||
def test(value, expected_value, title, description = None):
|
|
||||||
if (value == expected_value):
|
|
||||||
print(title, "[OK]")
|
|
||||||
return
|
|
||||||
print (title, "[ERROR]")
|
|
||||||
print ("expected", expected_value, ", got", value)
|
|
||||||
if not description is None:
|
|
||||||
print (description)
|
|
||||||
|
|
||||||
def test_accounts_register(client, username, password):
|
|
||||||
print ("REGISTER")
|
|
||||||
|
|
||||||
test(client.accounts.create(username, password), b'ok: user added', 'normal', None)
|
|
||||||
|
|
||||||
print()
|
|
||||||
|
|
||||||
def test_accounts_login(client, username, password):
|
|
||||||
|
|
||||||
print ("LOGIN")
|
|
||||||
|
|
||||||
test(client.login(username, password), b'ok: account valid', "normal", None)
|
|
||||||
|
|
||||||
print()
|
|
||||||
|
|
||||||
def test_accounts_delete(client):
|
|
||||||
|
|
||||||
print ("DELETE")
|
|
||||||
|
|
||||||
test(client.accounts.delete(), b'ok: account has been deleted', 'normal')
|
|
||||||
|
|
||||||
print()
|
|
||||||
|
|
||||||
def test_profile_get(client):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def tests():
|
|
||||||
|
|
||||||
username = uuid4()
|
|
||||||
password = uuid4()
|
|
||||||
|
|
||||||
client = Client.Client("http://0.0.0.0:8000/")
|
|
||||||
|
|
||||||
|
|
||||||
test_accounts_register(client, username, password)
|
|
||||||
test_accounts_login(client, username, password)
|
|
||||||
test_accounts_delete(client)
|
|
||||||
|
|
||||||
test_profile_get(client)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
tests()
|
|
@ -5,5 +5,5 @@ accounts_login: str = accounts + "login"
|
|||||||
accounts_delete: str = accounts + "delete"
|
accounts_delete: str = accounts + "delete"
|
||||||
accounts_register: str = accounts + "register"
|
accounts_register: str = accounts + "register"
|
||||||
|
|
||||||
profiles: str = "profiles"
|
profiles: str = api + "profiles/"
|
||||||
profiles_page: str = profiles
|
profiles_page: str = profiles
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
35
tests/accounts.py
Normal file
35
tests/accounts.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from tests.utils import test
|
||||||
|
|
||||||
|
def test_accounts_register(client, username, password):
|
||||||
|
print ("REGISTER")
|
||||||
|
|
||||||
|
test(client.accounts.create, (username, password), b'ok: user added', 'normal', None)
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
|
def test_accounts_login(client, username, password):
|
||||||
|
|
||||||
|
print ("LOGIN")
|
||||||
|
|
||||||
|
test(client.login, (username, password), b'ok: account valid', "normal", None)
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
|
def test_accounts_delete(client):
|
||||||
|
|
||||||
|
print ("DELETE")
|
||||||
|
|
||||||
|
test(client.accounts.delete, (), b'ok: account has been deleted', 'normal')
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
|
def test_accounts(client):
|
||||||
|
|
||||||
|
username = uuid4()
|
||||||
|
password = uuid4()
|
||||||
|
|
||||||
|
test_accounts_register(client, username, password)
|
||||||
|
test_accounts_login(client, username, password)
|
||||||
|
test_accounts_delete(client)
|
19
tests/profiles.py
Normal file
19
tests/profiles.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..')) # Add parent directory to system path
|
||||||
|
|
||||||
|
from tests.utils import test
|
||||||
|
|
||||||
|
from src.profile import Profile
|
||||||
|
|
||||||
|
def test_profiles_get(client):
|
||||||
|
|
||||||
|
print ("GET")
|
||||||
|
|
||||||
|
test(client.profiles.get, (1, ), Profile(username="997e13f5-474d-4fea-b55a-ad8a27b9534b", title=""), "normal")
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
|
def test_profiles(client):
|
||||||
|
|
||||||
|
test_profiles_get(client)
|
22
tests/test.py
Normal file
22
tests/test.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..')) # Add parent directory to system path
|
||||||
|
|
||||||
|
from src.client import Client
|
||||||
|
from src import urls
|
||||||
|
|
||||||
|
from tests.accounts import test_accounts
|
||||||
|
from tests.profiles import test_profiles
|
||||||
|
|
||||||
|
def tests():
|
||||||
|
|
||||||
|
client = Client("http://0.0.0.0:8000/")
|
||||||
|
|
||||||
|
print("ACCOUNTS".center(os.get_terminal_size()[0], '-'))
|
||||||
|
test_accounts(client)
|
||||||
|
|
||||||
|
print("PROFILES".center(os.get_terminal_size()[0], '-'))
|
||||||
|
test_profiles(client)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
tests()
|
9
tests/utils.py
Normal file
9
tests/utils.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
def test(func: callable, params, expected_value, title: str, description = None):
|
||||||
|
print(title, end=" ")
|
||||||
|
if (func(*params) == expected_value):
|
||||||
|
print("[OK]")
|
||||||
|
return
|
||||||
|
print ("[ERROR]")
|
||||||
|
print ("expected", expected_value, ", got", value)
|
||||||
|
if not description is None:
|
||||||
|
print (description)
|
Loading…
Reference in New Issue
Block a user