31 lines
1.2 KiB
Python
31 lines
1.2 KiB
Python
import urllib.request, json, base64, random
|
|
|
|
BASE = "http://localhost:8080"
|
|
|
|
def req(path, method="GET", data=None, headers=None):
|
|
r = urllib.request.Request(BASE + path, method=method)
|
|
if headers:
|
|
for k, v in headers.items(): r.add_header(k, v)
|
|
if data:
|
|
r.add_header("Content-Type", "application/json")
|
|
r.data = json.dumps(data).encode("utf-8")
|
|
try:
|
|
with urllib.request.urlopen(r) as resp:
|
|
return resp.status, json.loads(resp.read())
|
|
except urllib.error.HTTPError as e:
|
|
return e.code, json.loads(e.read())
|
|
|
|
u = "test_" + str(random.randint(1000, 9999))
|
|
print("Создаём пользователя:", u)
|
|
s, user = req("/api/v1/users", "POST", {"username": u, "display_name": "Тест", "password": "testpassword1234"})
|
|
print("Пользователь:", s, user)
|
|
|
|
print("Логинимся...")
|
|
cred = base64.b64encode((u + ":testpassword1234").encode()).decode()
|
|
s, sess = req("/api/v1/auth/sessions", "POST", headers={"Authorization": "Basic " + cred})
|
|
print("Сессия:", s)
|
|
token = sess["token"]
|
|
|
|
print("Создаём чат...")
|
|
s, chat = req("/api/v1/chats", "POST", {"title": "Тест", "member_ids": [user["id"]]}, {"Authorization": "Bearer " + token})
|
|
print("Чат:", s, chat) |