Добавление API для получения всех пользователей, доработка call2api

This commit is contained in:
2026-06-11 13:18:22 +03:00
parent e49f3c4f3c
commit fe3987d2ad
2 changed files with 105 additions and 3 deletions
+51 -3
View File
@@ -1,5 +1,7 @@
import requests
from config import load_config
def create_account(
api_url: str, api_token: str, username: str, password: str, drive_quota: int
@@ -77,6 +79,52 @@ def delete_account(api_url: str, api_token: str, username: str) -> dict:
return {"status_code": -1, "message": f"Request failed: {str(e)}"}
# print(set_quota("http://proxy.del.pw:50020", "test", "test", 1000))
# print(delete_account("http://127.0.0.1:8000", "test", "test"))
# print(create_account("http://127.0.0.1:8000", "test", "test", "test", 999))
def get_all_users(api_url: str, api_token: str) -> dict:
"""
Call the get all users API endpoint.
Args:
api_url: Base URL of the API server (e.g., 'http://localhost:8000')
api_token: Security token for API authentication
Returns:
Dictionary with 'status_code' and 'data' keys.
If successful, 'data' will contain a list of users.
If failed, 'data' will contain an error message.
"""
url = f"{api_url.rstrip('/')}/users"
try:
response = requests.get(url, params={"token": api_token})
if response.status_code == 200:
return {"status_code": response.status_code, "data": response.json()}
else:
return {"status_code": response.status_code, "data": response.text}
except requests.RequestException as e:
return {"status_code": -1, "data": f"Request failed: {str(e)}"}
except ValueError as e:
# Handle case where response isn't valid JSON
return {"status_code": -1, "data": f"Failed to parse response: {str(e)}"}
if __name__ == "__main__":
CONFIG = load_config()
# API_URL = "https://sfs.del.pw"
API_URL = "http://127.0.0.1:8000"
# print(set_quota(API_URL, CONFIG["security"]["api_token"], "sans", 10000))
# print(delete_account(API_URL, CONFIG["security"]["api_token"], "test"))
# print(create_account(API_URL, CONFIG["security"]["api_token"] , "test", "test", 999))
# print(get_all_users(API_URL, CONFIG["security"]["api_token"]))
users = get_all_users(API_URL, CONFIG["security"]["api_token"])["data"]
print(users)
print("-------------------------")
for user in users:
quota = user["quota_mb"]
quota += 100 # +100 MB to each user
print(
set_quota(API_URL, CONFIG["security"]["api_token"], user["username"], quota)
)
print("-------------------------")
print(get_all_users(API_URL, CONFIG["security"]["api_token"]))