Skip to content

Commit f0f78b3

Browse files
committed
Add example for importing postman environment file
1 parent ef230a1 commit f0f78b3

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
"""
3+
Import postman environment file
4+
5+
To set postman environment for existing API target
6+
with Postman collection schema type
7+
8+
"""
9+
import json
10+
from urllib.parse import urljoin
11+
12+
import requests
13+
14+
15+
token = input("API Token:")
16+
headers = {"Authorization": "JWT {}".format(token)}
17+
target_id = input("Target ID: ")
18+
postman_env_file = input("Postman collection file: ")
19+
20+
with open(postman_env_file) as fd:
21+
postman_env = json.load(fd)
22+
23+
parsed_env = [
24+
{"name": value["key"], "value": value["value"]}
25+
for value in postman_env["values"]
26+
if value["enabled"]
27+
]
28+
29+
api_base_url = "https://api.probely.com"
30+
target_endpoint = urljoin(api_base_url, "targets/{target_id}/")
31+
32+
response = requests.get(target_endpoint.format(target_id=target_id), headers=headers)
33+
assert response.status_code == 200, response.json()
34+
35+
custom_api_parameters = (
36+
response.json()["site"]["api_scan_settings"]["custom_api_parameters"] or []
37+
)
38+
39+
updated_field_names = [entry["name"] for entry in parsed_env]
40+
41+
custom_api_parameters = [
42+
*[
43+
entry
44+
for entry in custom_api_parameters
45+
if entry["name"] not in updated_field_names
46+
],
47+
*parsed_env,
48+
]
49+
response = requests.patch(
50+
target_endpoint.format(target_id=target_id),
51+
headers=headers,
52+
json={
53+
"site": {"api_scan_settings": {"custom_api_parameters": custom_api_parameters}}
54+
},
55+
)
56+
assert response.status_code == 200, response.json()

0 commit comments

Comments
 (0)