-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
245 lines (219 loc) · 8.97 KB
/
main.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import csv
import string
import random
from urllib.parse import urlparse
from flask import jsonify, json
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def random_dict():
random_string = id_generator()
_str = random_string
globals()[_str] = 5000
random_string = {}
return random_string
class PostmanCollection:
def __init__(self, csv_filename, collection_name):
self.csv_filename = csv_filename
self.collection_name = collection_name
def request(self):
collection_shceme = {
"info": {
"_postman_id": "432f9e34-5f9d-4ed2-ad2e-f60b1163248b",
"name": "PostmanCollection",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "24072707"
},
"items": []
}
with open(self.csv_filename) as csv_file:
csv_file = csv.DictReader(csv_file)
for row in csv_file:
# make random dictionary
rand_dict = random_dict()
# add request name to dict
request_name = row['name']
rand_dict['name'] = request_name
# make dictionary request and append to random dictionary
rand_dict['request'] = {}
# add method random dictionary
method = row['method']
rand_dict['request']['method'] = method
# add headers random dictionary
rand_dict['request']['headers'] = []
# init body dictionary to random dictionary
rand_dict['request']['body'] = {}
# add mode, raw body and options to random dictionary
rand_dict['request']['body']['mode'] = 'raw'
body = row['body']
rand_dict['request']['body']['raw'] = body
options = {
"raw": {"language": "json"}
}
rand_dict['request']['body']['options'] = options
# init url dictionary to random dictionary
rand_dict['request']['url'] = {}
# add url to url in random dictionary
url = row['url']
rand_dict['request']['url']['raw'] = url
# extract url to get protocol
_url = urlparse(url)
protocol = _url.scheme
# assing protocol to url protocol
rand_dict['request']['url']['protocol'] = protocol
# extract url to get hostname
hostname = _url.netloc
# extract hostname by (.)
hostname = hostname.split('.')
rand_dict['request']['url']['host'] = []
counter = 0
for x in hostname:
rand_dict['request']['url']['host'].append(hostname[counter])
counter += 1
# add path list
rand_dict['request']['url']['path'] = []
# extract path by (/) and assing to path list
path = _url.path
path = path.split('/')
for x in path:
if x != '':
rand_dict['request']['url']['path'].append(x)
# append to collection_shceme itmes
collection_shceme['items'].append(rand_dict)
# beautify collection by indentation 4 and write to file
collection = json.dumps(collection_shceme, indent=4)
with open(self.collection_name, 'w') as outfile:
outfile.write(collection)
def with_nested_folder(self):
collection_shceme = {
"info": {
"_postman_id": "432f9e34-5f9d-4ed2-ad2e-f60b1163248b",
"name": "PostmanCollection",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "24072707"
},
"item": []
}
with open(self.csv_filename) as csv_file:
csv_file = csv.DictReader(csv_file)
folder_name = None
counter_folder_name = 0
always_true = True
for row in csv_file:
# init random dictionary
rand_dict = random_dict()
rand_dict_item = random_dict()
rand_dict_request = random_dict()
rand_dict_body = random_dict()
rand_dict_url = random_dict()
# for fullfied collection_shceme['item'] if was fullfiled we continue in else
if counter_folder_name == 0:
# get folder name
folder_name = row['folder']
# assing folder_name to name
rand_dict['name'] = folder_name
# assing item with empty list
rand_dict['item'] = []
subfolder = row['subfolder']
request_name = row['request-name']
rand_dict_request['method'] = row['method']
rand_dict_request['header'] = []
rand_dict_request['body'] = rand_dict_body
rand_dict_request['body']['mode'] = 'raw'
rand_dict_request['body']['raw'] = row['body']
rand_dict_request['body']['options'] = {"raw": {"language": "json"}}
rand_dict_request['url'] = rand_dict_url
rand_dict_request['url']['raw'] = row['url']
# extract url to get protocol
_url = urlparse(row['url'])
protocol = _url.scheme
rand_dict_request['url']['protocol'] = protocol
# extract url to get hostname
hostname = _url.netloc
# extract hostname by (.)
hostname = hostname.split('.')
rand_dict_request['url']['host'] = []
counter = 0
for x in hostname:
rand_dict_request['url']['host'].append(hostname[counter])
counter += 1
rand_dict_request['url']['path'] = []
path = _url.path
path = path.split('/')
for x in path:
if x != '':
rand_dict_request['url']['path'].append(x)
rand_dict['item'] .append({'name':subfolder, 'item': [{'name': request_name, 'request': rand_dict_request}]})
collection_shceme['item'].append(rand_dict)
counter_folder_name += 1
else:
# if folder has same name then we want to merge them to one folder
if row['folder'] in collection_shceme['item'][-1]['name']:
subfolder = row['subfolder']
request_name = row['request-name']
rand_dict_request['method'] = row['method']
rand_dict_request['header'] = []
rand_dict_request['body'] = rand_dict_body
rand_dict_request['body']['mode'] = 'raw'
rand_dict_request['body']['raw'] = row['body']
rand_dict_request['body']['options'] = {"raw": {"language": "json"}}
rand_dict_request['url'] = rand_dict_url
rand_dict_request['url']['raw'] = row['url']
# extract url to get protocol
_url = urlparse(row['url'])
protocol = _url.scheme
rand_dict_request['url']['protocol'] = protocol
# extract url to get hostname
hostname = _url.netloc
# extract hostname by (.)
hostname = hostname.split('.')
rand_dict_request['url']['host'] = []
counter = 0
for x in hostname:
rand_dict_request['url']['host'].append(hostname[counter])
counter += 1
rand_dict_request['url']['path'] = []
path = _url.path
path = path.split('/')
for x in path:
if x != '':
rand_dict_request['url']['path'].append(x)
collection_shceme['item'][-1]['item'].append({'name':subfolder, 'item': [{'name': request_name, 'request': rand_dict_request}]})
# if the folder is not same, then we make them
else:
folder_name = row['folder']
rand_dict['name'] = folder_name
rand_dict['item'] = []
subfolder = row['subfolder']
request_name = row['request-name']
rand_dict_request['method'] = row['method']
rand_dict_request['header'] = []
rand_dict_request['body'] = rand_dict_body
rand_dict_request['body']['mode'] = 'raw'
rand_dict_request['body']['raw'] = row['body']
rand_dict_request['body']['options'] = {"raw": {"language": "json"}}
rand_dict_request['url'] = rand_dict_url
rand_dict_request['url']['raw'] = row['url']
# extract url to get protocol
_url = urlparse(row['url'])
protocol = _url.scheme
rand_dict_request['url']['protocol'] = protocol
# extract url to get hostname
hostname = _url.netloc
# extract hostname by (.)
hostname = hostname.split('.')
rand_dict_request['url']['host'] = []
counter = 0
for x in hostname:
rand_dict_request['url']['host'].append(hostname[counter])
counter += 1
rand_dict_request['url']['path'] = []
path = _url.path
path = path.split('/')
for x in path:
if x != '':
rand_dict_request['url']['path'].append(x)
rand_dict['item'] .append({'name':subfolder, 'item': [{'name': request_name, 'request': rand_dict_request}]})
collection_shceme['item'].append(rand_dict)
collection_shceme = json.dumps(collection_shceme, indent=4)
with open(self.collection_name, 'w') as outfile:
outfile.write(collection_shceme)