-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.py
executable file
·207 lines (174 loc) · 6.89 KB
/
client.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import asyncio
import json
import os
import hmac
import hashlib
from datetime import datetime
from time import time
import aiohttp
import click
SIGNING_KEY = os.getenv('CLIENT_SIGNING_KEY', 'testing').encode()
BASE_URL = os.getenv('CLIENT_BASE_URL', 'http://localhost:5000/')
print(f'using shared secret {SIGNING_KEY} and url {BASE_URL}')
# BASE_URL = 'https://socket.tutorcruncher.com/'
CONN = aiohttp.TCPConnector(verify_ssl=False)
commands = []
def command(func):
commands.append(func)
return func
@command
async def index(**kwargs):
async with aiohttp.ClientSession(connector=CONN) as session:
async with session.get(BASE_URL) as r:
print(f'status: {r.status}')
text = await r.text()
print(f'response: {text}')
@command
async def company_list(**kwargs):
payload = f'{time():0.0f}'
b_payload = payload.encode()
m = hmac.new(SIGNING_KEY, b_payload, hashlib.sha256)
headers = {
'Signature': m.hexdigest(),
'Request-Time': payload,
}
async with aiohttp.ClientSession(connector=CONN) as session:
async with session.get(BASE_URL + 'companies', headers=headers) as r:
print(f'status: {r.status}')
text = await r.text()
print(f'response: {text}')
@command
async def company_create(*, public_key=None, data=None, **kwargs):
post_data = {
'name': f'company {datetime.utcnow():%y-%m-%d %H:%M:%S}',
'public_key': public_key,
'_request_time': int(time()),
}
data and post_data.update(data)
payload = json.dumps(post_data)
b_payload = payload.encode()
m = hmac.new(SIGNING_KEY, b_payload, hashlib.sha256)
headers = {
'Webhook-Signature': m.hexdigest(),
'User-Agent': 'TutorCruncher',
'Content-Type': 'application/json',
}
async with aiohttp.ClientSession(connector=CONN) as session:
async with session.post(BASE_URL + 'companies/create', data=payload, headers=headers) as r:
print(f'status: {r.status}')
text = await r.text()
print(f'response: {text}')
@command
async def company_update(*, public_key, data=None, **kwargs):
payload = dict(_request_time=int(time()))
payload.update(data or {})
payload = json.dumps(payload)
b_payload = payload.encode()
m = hmac.new(SIGNING_KEY, b_payload, hashlib.sha256)
headers = {
'Webhook-Signature': m.hexdigest(),
'User-Agent': 'TutorCruncher',
'Content-Type': 'application/json',
}
async with aiohttp.ClientSession(connector=CONN) as session:
async with session.post(BASE_URL + f'{public_key}/webhook/options', data=payload, headers=headers) as r:
print(f'status: {r.status}')
text = await r.text()
print(f'response: {text}')
@command
async def company_options(*, public_key, **kwargs):
async with aiohttp.ClientSession(connector=CONN) as session:
async with session.get(BASE_URL + f'{public_key}/options') as r:
print(f'status: {r.status}')
text = await r.text()
print(f'response: {text}')
CON_DATA = {
'id': 23502,
'deleted': False,
'first_name': 'Gerry',
'last_name': 'Howell',
'town': 'Edinburgh',
'country': 'United Kingdom',
'location': {'latitude': None, 'longitude': None},
'photo': 'http://unsplash.com/photos/vltMzn0jqsA/download',
'extra_attributes': [
{
'machine_name': None,
'name': 'Bio',
'type': 'text_extended',
'sort_index': 0,
'value': 'The returned group is itself an iterator that shares the underlying iterable with groupby(). '
'Because the source is shared, when the groupby() object is advanced, the previous group is no '
'longer visible. So, if that data is needed later, it should be stored as a list:',
'id': 195,
},
{
'machine_name': None,
'name': 'Teaching Experience',
'type': 'text_short',
'sort_index': 0,
'value': 'Harvard',
'id': 196,
},
],
'skills': [
{'qual_level': 'A Level', 'subject': 'Mathematics', 'qual_level_ranking': 18.0, 'category': 'Maths'},
{'qual_level': 'GCSE', 'subject': 'Mathematics', 'qual_level_ranking': 16.0, 'category': 'Maths'},
{'qual_level': 'GCSE', 'subject': 'Algebra', 'qual_level_ranking': 16.0, 'category': 'Maths'},
{'qual_level': 'KS3', 'subject': 'Language', 'qual_level_ranking': 13.0, 'category': 'English'},
{'qual_level': 'Degree', 'subject': 'Mathematics', 'qual_level_ranking': 21.0, 'category': 'Maths'},
],
'labels': [],
'last_updated': '2017-01-08T12:20:46.244Z',
'created': '2015-01-19',
'release_timestamp': '2017-01-08T12:27:07.541165Z',
}
@command
async def contractor_create(*, public_key, **kwargs):
payload = dict(_request_time=int(time()), **CON_DATA)
payload = json.dumps(payload)
b_payload = payload.encode()
m = hmac.new(SIGNING_KEY, b_payload, hashlib.sha256)
headers = {
'Webhook-Signature': m.hexdigest(),
'User-Agent': 'TutorCruncher',
'Content-Type': 'application/json',
}
async with aiohttp.ClientSession(connector=CONN) as session:
async with session.post(BASE_URL + f'{public_key}/webhook/contractor', data=payload, headers=headers) as r:
print(f'status: {r.status}')
text = await r.text()
print(f'response: {text}')
@command
async def contractor_list(*, public_key, **kwargs):
async with aiohttp.ClientSession(connector=CONN) as session:
async with session.get(BASE_URL + f'{public_key}/contractors') as r:
print(f'status: {r.status}')
text = await r.text()
print(f'response: {text}')
@command
async def submit_enquiry(*, public_key, data, **kwargs):
async with aiohttp.ClientSession(connector=CONN) as session:
headers = {
'User-Agent': 'Testing Browser',
'Referer': 'https://www.example.com/referrer',
}
async with session.post(BASE_URL + f'{public_key}/enquiry', data=json.dumps(data), headers=headers) as r:
print(f'status: {r.status}')
text = await r.text()
print(f'response: {text}')
missing = object()
@click.command()
@click.argument('command', type=click.Choice([c.__name__ for c in commands]))
@click.option('-p', '--public-key', default=missing)
@click.option('-d', '--data', default=missing)
def cli(command, **kwargs):
command_lookup = {c.__name__: c for c in commands}
kwargs = {k: v for k, v in kwargs.items() if v != missing}
kwargs['data'] = kwargs.get('data') and json.loads(kwargs.get('data'))
func = command_lookup[command]
print(f'running {func.__name__}, kwargs = {kwargs}...')
loop = asyncio.get_event_loop()
loop.run_until_complete(func(**kwargs))
if __name__ == '__main__':
cli()