-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_create_database.py
More file actions
49 lines (39 loc) · 1.69 KB
/
01_create_database.py
File metadata and controls
49 lines (39 loc) · 1.69 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
import sqlite3
import json
# Load JSON schema
with open("rmm_schema.json", "r", encoding="utf-8") as file:
schema = json.load(file)
# Connect to SQLite database
conn = sqlite3.connect("api_schema4_rmm.db")
cursor = conn.cursor()
# Drop table (optional, only do this if resetting DB)
# cursor.execute("DROP TABLE IF EXISTS api_endpoints")
# Create table for storing API endpoints
cursor.execute("""
CREATE TABLE IF NOT EXISTS api_endpoints (
id INTEGER PRIMARY KEY AUTOINCREMENT,
path TEXT,
methods TEXT,
description TEXT,
request_body TEXT,
responses TEXT
)
""")
# Insert API paths and details into the database
for path, details in schema.get("paths", {}).items():
for method, method_details in details.items():
description = method_details.get("description", "No description available")
# Extract request body schema (if available)
request_body = method_details.get("requestBody", {}).get("content", {}).get("application/json", {}).get("schema", {})
request_body_str = json.dumps(request_body) if request_body else "None"
# Extract response codes
responses = method_details.get("responses", {})
response_summary = {code: resp.get("description", "No description") for code, resp in responses.items()}
responses_str = json.dumps(response_summary)
# Store in database
cursor.execute("INSERT OR IGNORE INTO api_endpoints (path, methods, description, request_body, responses) VALUES (?, ?, ?, ?, ?)",
(path, method.upper(), description, request_body_str, responses_str))
# Commit changes and close connection
conn.commit()
conn.close()
print("> API schema stored successfully in SQLite!")