-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathupdate_postman_collection.py
More file actions
71 lines (59 loc) · 2.24 KB
/
update_postman_collection.py
File metadata and controls
71 lines (59 loc) · 2.24 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
#!/usr/bin/env python3
"""
Updates existing Postman Collection with the JSON file via Postman API.
Prerequisites:
- Existing Postman collection .json file in defs directory
- POSTMAN_API_KEY env variable
- POSTMAN_COLLECTION_ID env variable (API key must have write access to that collection)
- POSTMAN_COLLECTION_FILE env variable
"""
from pathlib import Path
import requests
import json
import os
import sys
def upload_to_postman():
"""Update Postman Collection via Postman API."""
# Get required environment variables
api_key = os.getenv('POSTMAN_API_KEY')
collection_id = os.getenv('POSTMAN_COLLECTION_ID')
collection_file = os.getenv('POSTMAN_COLLECTION_FILE')
if not all([api_key, collection_id, collection_file]):
print("❌ Missing required environment variables:")
print(f" POSTMAN_API_KEY: {'✅' if api_key else '❌'}")
print(f" POSTMAN_COLLECTION_ID: {'✅' if collection_id else '❌'}")
print(f" POSTMAN_COLLECTION_FILE: {'✅' if collection_file else '❌'}")
sys.exit(1)
# Locate collection file
file_path = Path(collection_file)
if not file_path.exists():
print(f"❌ Collection file not found: {file_path}")
sys.exit(1)
# Load collection from file
try:
with open(file_path, 'r') as f:
collection = json.load(f)
except json.JSONDecodeError as e:
print(f"❌ Invalid JSON in {collection_file}: {e}")
sys.exit(1)
except Exception as e:
print(f"❌ Failed to read {collection_file}: {e}")
sys.exit(1)
# Upload to Postman
print("Updating collection...")
try:
response = requests.put(
f'https://api.getpostman.com/collections/{collection_id}',
headers={'X-Api-Key': api_key},
json={'collection': collection}
)
response.raise_for_status()
print("✅ Successfully updated collection")
except requests.exceptions.HTTPError as e:
print(f"❌ API error ({response.status_code}): {response.text}")
sys.exit(1)
except requests.exceptions.RequestException as e:
print(f"❌ Request failed: {e}")
sys.exit(1)
if __name__ == "__main__":
upload_to_postman()