-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathswagger_to_csv.py
More file actions
136 lines (111 loc) · 4.48 KB
/
swagger_to_csv.py
File metadata and controls
136 lines (111 loc) · 4.48 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
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
"""
Swagger YAML to CSV Converter
Converts Swagger/OpenAPI YAML file to CSV with HTTP methods, API URLs, Deprecated status, Tags, and Description
Run this to use this:
pip install pyyaml
Example:
python3 swagger_to_csv.py swagger.yaml output.csv
"""
import yaml
import csv
import sys
import argparse
from pathlib import Path
def load_swagger_yaml(file_path):
"""Load and parse Swagger YAML file"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
return yaml.safe_load(file)
except FileNotFoundError:
print(f"Error: File '{file_path}' not found.")
sys.exit(1)
except yaml.YAMLError as e:
print(f"Error parsing YAML file: {e}")
sys.exit(1)
except Exception as e:
print(f"Error reading file: {e}")
sys.exit(1)
def extract_api_endpoints(swagger_data):
"""Extract HTTP methods, API URLs, Deprecated status, Tags, and Description from Swagger data"""
endpoints = []
if not swagger_data or 'paths' not in swagger_data:
print("Error: No 'paths' section found in Swagger file.")
return endpoints
base_path = swagger_data.get('basePath', '')
for path, methods in swagger_data['paths'].items():
full_path = base_path + path
for method, details in methods.items():
# Only include standard HTTP methods
if method.upper() in ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']:
# Handle deprecated field
deprecated = details.get('deprecated', False)
if deprecated is True:
deprecated_status = 'true'
elif deprecated is False:
deprecated_status = 'false'
else:
deprecated_status = ''
# Handle tags field (can be array or single value)
tags = details.get('tags', [])
if isinstance(tags, list):
tags_str = ', '.join(tags) if tags else ''
else:
tags_str = str(tags)
# Handle description field
description = details.get('description', '')
if description is None:
description = ''
endpoints.append({
'http_method': method.upper(),
'api_url': full_path,
'deprecated': deprecated_status,
'tags': tags_str,
'description': description
})
return endpoints
def write_to_csv(endpoints, output_file):
"""Write endpoints to CSV file"""
try:
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
fieldnames = ['http_method', 'api_url', 'deprecated', 'tags', 'description']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for endpoint in endpoints:
writer.writerow(endpoint)
print(f"Successfully wrote {len(endpoints)} endpoints to {output_file}")
except Exception as e:
print(f"Error writing to CSV file: {e}")
sys.exit(1)
def main():
"""Main function"""
parser = argparse.ArgumentParser(
description='Convert Swagger YAML to CSV with HTTP methods, API URLs, Deprecated status, Tags, and Description'
)
parser.add_argument(
'input_file',
help='Path to the Swagger YAML file'
)
parser.add_argument(
'output_file',
help='Path to the output CSV file'
)
args = parser.parse_args()
# Check if input file exists
if not Path(args.input_file).exists():
print(f"Error: Input file '{args.input_file}' does not exist.")
sys.exit(1)
# Load and parse Swagger YAML
print(f"Loading Swagger file: {args.input_file}")
swagger_data = load_swagger_yaml(args.input_file)
# Extract endpoints
print("Extracting API endpoints...")
endpoints = extract_api_endpoints(swagger_data)
if not endpoints:
print("No API endpoints found in the Swagger file.")
sys.exit(1)
# Write to CSV
print(f"Writing to CSV: {args.output_file}")
write_to_csv(endpoints, args.output_file)
if __name__ == "__main__":
main()