-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_client_explicit.py
executable file
·77 lines (56 loc) · 1.63 KB
/
rest_client_explicit.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
#!/usr/bin/python3
"""Browse and query a REST API, based on an internal domain model.
"""
from dataclasses import dataclass
from json import loads
from time import time
from typing import Dict, List
if __name__ == '__main__':
import _extend_path
from mash.filesystem.discoverable import observe
from mash.filesystem.view import Path
from mash.shell.shell import main
from mash.shell import ShellWithFileSystem
from mash.server.server import init as init_server
from mash.server.routes.default import basepath
def get_user(id):
data = init_client().get(f'{basepath}users/{id}')
return loads(data.data)
def init_client():
app = init_server()
client = app.test_client()
return client
def infer_query_parameter(path, key):
i = path.index(key) + 1
id = path[i]
return id
@dataclass
class User:
id: int
name: str
email: int
@staticmethod
def get_value(path: Path) -> dict:
id = infer_query_parameter(path, 'users')
# get raw object
user = get_user(id)
# enrich object
user['id'] = id
user['updated'] = time()
return user
@staticmethod
def get_all(path: Path) -> List[str]:
data = init_client().get(basepath + 'users')
return {k: User for k in loads(data.data)}
@dataclass
class Organization:
users: Dict[str, User]
def init():
shell = ShellWithFileSystem(data={'repository': Organization},
get_value_method=observe)
obj = shell.repository
obj.init_home(['repository'])
return shell, obj
if __name__ == '__main__':
shell, obj = init()
main(shell=shell.shell)