generated from C4T-BuT-S4D/ad-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplorers_lib.py
88 lines (75 loc) · 3.78 KB
/
explorers_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
from typing import Optional
import checklib
from checklib import BaseChecker
import requests
PORT = 8000
class ExplorersLib:
@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 signup(self, session: requests.Session, username: str, password: str):
resp = session.post(f'{self.api_url}/signup', json={
'username': username,
'password': password
})
self.c.assert_eq(resp.status_code, 200, 'Failed to signup')
resp_json = self.c.get_json(resp, 'Failed to signup: invalid JSON')
return resp_json
def signin(self, session: requests.Session, username: str, password: str,
status: checklib.Status = checklib.Status.MUMBLE):
resp = session.post(f'{self.api_url}/signin', json={
'username': username,
'password': password
})
self.c.assert_eq(resp.status_code, 200, 'Failed to signin', status=status)
resp_json = self.c.get_json(resp, 'Failed to signin: invalid JSON')
return resp_json
def create_route(self, session: requests.Session, title: str, description: str):
resp = session.post(f'{self.api_url}/route/create', json={
'title': title,
'description': description
})
self.c.assert_eq(resp.status_code, 200, 'Failed to create route')
resp_json = self.c.get_json(resp, 'Failed to create route: invalid JSON')
self.c.assert_eq(type(resp_json), dict, 'Failed to create route: invalid JSON')
return resp_json
def get_route_list(self, session: requests.Session, status: checklib.Status = checklib.Status.MUMBLE):
resp = session.get(f'{self.api_url}/route')
self.c.assert_eq(resp.status_code, 200, 'Failed to get route list', status=status)
resp_json = self.c.get_json(resp, 'Failed to get route list: invalid JSON')
self.c.assert_eq(type(resp_json), list, 'Failed to get route list: invalid JSON')
return resp_json
def get_route(self, session: requests.Session, route_id: str, token: Optional[str] = None,
status: checklib.Status = checklib.Status.MUMBLE):
params = {}
if token:
params['token'] = token
resp = session.get(f'{self.api_url}/route/{route_id}', params=params)
self.c.assert_eq(resp.status_code, 200, 'Failed to get route', status=status)
resp_json = self.c.get_json(resp, 'Failed to get route: invalid JSON')
self.c.assert_eq(type(resp_json), dict, 'Failed to get route: invalid JSON')
return resp_json
def update_route(self, session: requests.Session, route_id: str, description: str, points=list[dict],
waypoints=list[dict]):
pld = {
'description': description,
'track_points': points,
'waypoints': waypoints
}
resp = session.post(f'{self.api_url}/route/{route_id}/update', json=pld)
self.c.assert_eq(resp.status_code, 200, 'Failed to update route')
resp_json = self.c.get_json(resp, 'Failed to update route: invalid JSON')
self.c.assert_eq(type(resp_json), dict, 'Failed to update route: invalid JSON')
return resp_json
def upload_gpx(self, session: requests.Session, route_id: str, file):
resp = session.post(f'{self.api_url}/route/{route_id}/upload', files={
'file': file
})
self.c.assert_eq(resp.status_code, 200, 'Failed to upload gpx')
resp_json = self.c.get_json(resp, 'Failed to upload gpx: invalid JSON')
self.c.assert_eq(type(resp_json), dict, 'Failed to upload gpx: invalid JSON')
return resp_json