-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathts_api_check.py
58 lines (51 loc) · 2.01 KB
/
ts_api_check.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
import glob
import json
import os
import re
import subprocess
path = os.path.dirname(os.path.realpath(__file__))
def ts_check():
with open(os.path.join(path, 'tests/snapshots/check.ts'), 'w') as checkfile:
requests = []
responses = []
for snapshot in glob.glob(os.path.join(path, 'tests/snapshots/*.json')):
with open(snapshot) as snapjson:
try:
shot = json.load(snapjson)
except Exception as e:
print(e)
continue
if (
shot.get('request', {}).get('method', None) not in ['GET']
) and 'data' in shot.get('request', {}):
requests.append((os.path.basename(snapshot), shot['request']))
if (shot.get('response', {}).get('status_code', None)) not in [
200,
201,
]:
continue
responses.append((os.path.basename(snapshot), shot['response']))
with open(os.path.join(path, 'tests/type_template.ts')) as template:
checkfile.write(template.read())
checkfile.write('\n\n')
for f, r in requests:
checkfile.write(f'// {f}\n')
parts = [p for p in r['url'].split('/') if p.strip()]
if re.match(r'\d+', parts[-1]):
parts = parts[:-1]
method = r['method'].lower().capitalize()
if parts[-1][-1] == 's':
parts[-1] = parts[-1][:-1]
checkfile.write(
f'{parts[-1]}{method} = {json.dumps(r["data"], indent=True)}\n'
)
checkfile.write('\n\n')
checkfile.write(
'\n'.join(
f'// {f}\nresponse = {json.dumps(r, indent=True)};'
for f, r in responses
)
)
return subprocess.call(['yarn', 'tsc']) == 0
if __name__ == '__main__':
ts_check()