generated from C4T-BuT-S4D/ad-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocs_lib.py
139 lines (111 loc) · 5.3 KB
/
docs_lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from typing import Optional
import checklib
from checklib import BaseChecker
import requests
PORT = 8000
class DocsLib:
@property
def api_url(self):
return f'http://{self.host}:{self.port}/api'
def __init__(self, checker: BaseChecker, port=PORT, host=None):
self.c = checker
self.port = port
self.host = host or self.c.host
def create_org(self, session: requests.Session, domain: str):
document = {
"domain": domain,
}
resp = session.post(
f"{self.api_url}/organizations",
json=document
)
self.c.assert_eq(resp.status_code, 200, 'Failed to create organization')
response_data = self.c.get_json(resp, 'Failed to create organization: invalid JSON')
self.c.assert_eq(type(response_data), dict, 'Failed to create organization: invalid JSON')
return response_data
def list_orgs(self, session: requests.Session):
resp = session.get(
f"{self.api_url}/organizations"
)
self.c.assert_eq(resp.status_code, 200, 'Failed to list organization')
return self.c.get_json(resp, 'Failed to list organization: invalid JSON')
def create_user(self, session: requests.Session, username: str, password: str, token: str):
document = {
"username": username,
"password": password,
"token": token
}
resp = session.post(
f"{self.api_url}/users",
json=document
)
self.c.assert_eq(resp.status_code, 200, 'Failed to create user')
return self.c.get_json(resp, 'Failed to create user: invalid JSON')
def login(self, session: requests.Session, username: str, password: str, status: checklib.Status = checklib.Status.MUMBLE):
document = {
"email": username,
"password": password
}
response = session.post(
f"{self.api_url}/login",
json=document
)
self.c.assert_eq(response.status_code, 200, 'Failed to login', status=status)
resp_json = self.c.get_json(response, 'Failed to login: invalid JSON', status=status)
self.c.assert_eq(type(resp_json), dict, 'Failed to login: invalid JSON', status=status)
token = resp_json.get('token') or ''
session.headers['Authorization'] = f"Bearer {token}"
return session
def get_user(self, session: requests.Session, status: checklib.Status = checklib.Status.MUMBLE):
response = session.get(
f"{self.api_url}/users/me",
)
self.c.assert_eq(response.status_code, 200, 'Failed to get user', status=status)
return self.c.get_json(response, 'Failed to get user: invalid JSON', status=status)
def create_doc(self, session: requests.Session, title: str, content: str, status: checklib.Status = checklib.Status.MUMBLE):
document = {
"title": title,
"content": content
}
response = session.post(
f"{self.api_url}/documents",
json=document
)
self.c.assert_eq(response.status_code, 200, 'Failed to create document', status=status)
return self.c.get_json(response, 'Failed to create document: invalid JSON', status=status)
def update_doc(self, session: requests.Session, doc_id:str, title: str | None, content: str | None = None,
status: checklib.Status = checklib.Status.MUMBLE):
document = {}
if title:
document['title'] = title
if content:
document['content'] = content
response = session.patch(
f"{self.api_url}/documents/{doc_id}",
json=document
)
self.c.assert_eq(response.status_code, 200, 'Failed to update document', status=status)
return self.c.get_json(response, 'Failed to create document: invalid JSON', status=status)
def get_doc(self, session: requests.Session, doc_id: str, status: checklib.Status = checklib.Status.MUMBLE):
response = session.get(
f"{self.api_url}/documents/{doc_id}"
)
self.c.assert_eq(response.status_code, 200, 'Failed to get document', status=status)
return self.c.get_json(response, 'Failed to get document: invalid JSON', status=status)
def delete_doc(self, session: requests.Session, doc_id: str, status: checklib.Status = checklib.Status.MUMBLE):
response = session.delete(
f"{self.api_url}/documents/{doc_id}"
)
self.c.assert_eq(response.status_code, 200, 'Failed to delete document', status=status)
def search(self, session: requests.Session, query: str, status: checklib.Status = checklib.Status.MUMBLE):
response = session.get(f"{self.api_url}/documents",
params={'query': query}
)
self.c.assert_eq(response.status_code, 200, 'Failed to search', status=status)
return self.c.get_json(response, 'Failed to search: invalid JSON', status=status)
def document_get_txt(self, session: requests.Session, doc_id: str, status: checklib.Status = checklib.Status.MUMBLE):
response = session.get(
f"{self.api_url}/document/{doc_id}/text"
)
self.c.assert_eq(response.status_code, 200, 'Failed to get txt', status=status)
return response.text