-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoas-api-enhancer.py
More file actions
executable file
·125 lines (104 loc) · 3.81 KB
/
oas-api-enhancer.py
File metadata and controls
executable file
·125 lines (104 loc) · 3.81 KB
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
#!/usr/bin/env python
# oas-api-enhancer.py
from datetime import datetime, timedelta
from dateutil.parser import parse
import datetime
import dateutil.parser as dp
import json
import oas
import os
import requests
def utc_now():
return datetime.datetime.utcnow().isoformat()+'Z'
def utc_now_tz():
return datetime.datetime. utcnow().replace(tzinfo=datetime.timezone.utc).isoformat()
def local_time():
return datetime.datetime.now().isoformat()
def dt2ts(dt):
return dp.parse(dt).strftime('%s.%f')
def dt_diff(start, end):
return float(dt2ts (end))-float (dt2ts (start))
def read_json_file(file):
try:
with open(file) as f:
res = json. load (f)
except Exception as e:
print(file+' Read Error: '+str(e))
res = {}
return res
start = utc_now()
schema = 'http://'
host = 'localhost:5100'
basePath = '/gr2/api/tar'
fullPath = schema+host+basePath
url = fullPath+'/swagger.json'
baseHttp = fullPath
baseHttps = fullPath.replace('http://', 'https://')
path = os.path.dirname(os.path.abspath(__file__))
r = requests.get(url)
obj = r.json()
obj['tags'] = read_json_file(path+'/oas/tags.json')
obj['schemes'] = read_json_file(path+'/oas/schemes.json')
obj['externalDocs'] = read_json_file(path+'/oas/externalDocs.json')
obj['definitions'] = read_json_file(path+'/oas/definitions.json')
if 'host' not in obj:
obj ['host'] = host
i = 0
res = {
'total_items':0,
'items': []
}
resTxt = ''
verbs = ['get', 'post', 'put', 'delete']
tagsCounter = {}
for p in obj['paths']:
oo = obj ['paths'][p]
for v in verbs:
if v in oo and 'description' in oo[v]:
i = i + 1
spc = ''
while len(spc)+len(str(i))<6:
spc = ' '+spc
description= oo[v]['description']
summary = oo[v]['description']
tag = p[1:].replace('','').replace('}','')
tag = tag.split('/')[0] if len(tag.split('/'))>1 else 'default'
if p in ['/{pop}']:
tag = 'pop'
if p in ['/title-map-data', '/uplynk-account','/pops', '/oas','/endpoints', '/uplynk-accounts']:
tag = 'default'
if '.' in summary:
summary= summary.split('.')[0]
if '\n' in summary:
summary= summary.split('\n')[-1]
resTxt = resTxt+spc+str(i)+'. '+tag+' '+v.upper()+' '+p+' | '+summary+'\n'
res ['items'].append({'Verb':v.upper(), 'Path':p, 'Description': description, 'Summary': summary, 'Tag': tag})
obj['paths'][p][v]['summary'] = summary
obj ['paths'][p][v]['tags'] = [tag]
if tag not in tagsCounter:
tagsCounter[tag] = 1
else:
tagsCounter[tag] = tagsCounter[tag] + 1
res['total_items'] = len(res ['items'])
#print(json.dumps (obj, default=str, indent=2, separators=(',', ':')))
print(resTxt)
end = utc_now()
print('Datetime ------------------------------------------------------------------')
print('UTC Start ................', start)
print('UTC End ..................', end)
print('Local Time ...............', local_time())
print('UTC Start TS .............', dt2ts (start))
print('UTC End TS ...............', dt2ts(end))
print('UTC End - UTC Start ......', dt_diff(start, end))
print('Tags ----------------------------------------------------------------------')
print(json.dumps(obj['tags'], default=str, indent=2, separators=(',', ': ')))
print('Tagged Operations Summary -------------------------------------------------')
print(json.dumps(tagsCounter, default=str,indent=2, separators=(',', ':')))
res_file = 'swagger.json'
print('Updatting '+res_file)
try:
with open(res_file, 'w') as f:
json.dump(obj, f, default=str,indent=2, separators=(',', ':'))
except Exception as e:
print('Write exception for '+res_file)
print(str(e))