-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_api_spec.py
More file actions
83 lines (60 loc) · 2.3 KB
/
get_api_spec.py
File metadata and controls
83 lines (60 loc) · 2.3 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
import argparse
import signal
import sys
import requests
import json
from json import decoder
from bs4 import BeautifulSoup, SoupStrainer
import csv
urlImpinjPlatform = 'https://platform.impinj.com'
versions = [
"1_0",
"1_2",
"1_3",
"1_4",
"1_5"
]
endpointsSpecUrl = []
for version in versions:
endpoint = '/site/docs/reader_api_welcome/archive/v' + version + '/index.gsp'
endpointsSpecUrl.append(endpoint)
# set current API version spec source (replace last version)
endpointsSpecUrl[len(endpointsSpecUrl)-1] = '/site/docs/reader_api_welcome/index.gsp'
# get all API specs from impinj website
apiSpecs = []
for iteration, endpointSpecUrl in enumerate(endpointsSpecUrl):
urlApiOverview = urlImpinjPlatform + endpointSpecUrl
pageOverview = requests.get(urlApiOverview)
soup = BeautifulSoup(pageOverview.content, features="html.parser", parse_only=SoupStrainer('a'))
endpointDownloadJSON = soup.find_all('a', string='JSON format', limit=1)[0]['href']
urlDownloadJSON = urlImpinjPlatform + endpointDownloadJSON
print("Downloading API spec version " + versions[iteration] + " from: ", urlDownloadJSON)
fileApiJson = requests.get(urlDownloadJSON).content
stringApiJson = fileApiJson.decode("utf8")
apiSpecs.append(json.loads(stringApiJson)['paths'])
# parse list of API endpoints from each full spec
for iteration, paths in enumerate(apiSpecs):
endpoints = []
# build array of paths & REST commands
print('path list: ')
for path in paths:
print(path)
for key in paths[path].keys():
print (" ", key)
endpoint = []
endpoint.append(path)
endpoint.append(key)
endpoints.append(endpoint)
print(endpoints)
# setup CSV parameters
fields = ['Endpoint', 'Request Type']
rows = endpoints
filename = 'impinj_iot_dev_intfc_endpoints_v' + versions[iteration] + '.csv'
# write to CSV file
with open(filename, 'w', newline='') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the fields
csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(rows)