-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.py
150 lines (128 loc) · 4.34 KB
/
connection.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import requests
from config.env import (
TASKS_URL,
USERNAME,
PASSWORD,
API_TOKEN,
USERS_LIST_URL,
)
def get_session() -> requests.Session:
"""Returns session instance with authorized credentials.
Authorizes bot via API using given credentials.
Credentials are given either as global or environmental variables.
Returns
-------
session : requests.Session
Session that is used to connect to trainingdata API as admin.
See also
--------
establish_connection()
Driver function to use in other modules and packages to
have a one-liner interface with `connection.py` module.
"""
session = requests.Session()
session.auth = (USERNAME, PASSWORD)
return session
def get_page_size(session: requests.Session,
url: str) -> int:
"""Returns the number of all users.
Desired param in `get_users` and `get_tasks` API query.
"""
response = session.get(url)
page_size = response.json()["count"]
return page_size
def get_users(session: requests.Session) -> list[dict]:
"""Returns a list of users from the JSON response from
corresponding API call.
"""
response = session.get(
USERS_LIST_URL,
params={
"page_size": get_page_size(session, USERS_LIST_URL),
},
headers={
"Authorization": f"Token {API_TOKEN}",
}
)
return response.json()["results"]
def get_user(
session: requests.Session,
**kwargs: dict[str, str]) -> dict[str,
str | int | bool | list[str | None]]:
"""Returns user by specified parameter.
Connects to server via API endpoint and returns either a user or
raises an IndexError in case user not found.
Parameters
----------
session : requests.Session
Session that is used to connect to trainingdata API as admin.
users_list_url : str
URL of API endpoint through which to get user from the server.
**kwargs : dict[str, str]
Either `username=` or `first_name=` or `last_name=`.
These are the filters which are available for searching the db.
Returns
-------
user : {str: str | int | bool | list[str | None]}
Full information about the user fetched from the database.
Includes his URL, id, username, first name, last name,
groups, staff status, date joined, etc.
Raises
------
IndexError
In case the user was not found.
"""
if kwargs.get("username") or kwargs.get("first_name") or kwargs.get("last_name"):
response = session.get(
USERS_LIST_URL,
params={
"search": list(kwargs.values())[0]
},
headers={
"Authorization": f"Token {API_TOKEN}"
}
)
return response.json()["results"][0]
def get_tasks(session: requests.Session) -> list[dict]:
response = session.get(
TASKS_URL,
params={
"page_size": get_page_size(session, TASKS_URL),
},
headers={
"Authorization": f"Token {API_TOKEN}",
}
)
return response.json()["results"]
def get_user_tasks(
session: requests.Session,
**kwargs: dict[str, str]) -> dict[str,
str | int | bool | list[str | None]]:
if kwargs.get("owner") or kwargs.get("assignee") or kwargs.get("name"):
response = session.get(
TASKS_URL,
params={
"search": list(kwargs.values())[0]
},
headers={
"Authorization": f"Token {API_TOKEN}"
}
)
return response.json()["results"]
def get_task_annotations(session: requests.Session,
task_id: int) -> dict[str, str | int | bool | list[str | None]]:
response = session.get(
f"{TASKS_URL}/{task_id}/annotations",
headers={
"Authorization": f"Token {API_TOKEN}"
}
)
return response.json()["shapes"]
def establish_connection() -> requests.Session:
"""Driver function to export to other modules/packages.
See also
--------
get_session()
"""
session = get_session()
return session