-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_debug_relay2RMM.py
More file actions
93 lines (81 loc) · 3.28 KB
/
02_debug_relay2RMM.py
File metadata and controls
93 lines (81 loc) · 3.28 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
from flask import Flask, request, jsonify
import sqlite3
import json
import requests
from flasgger import Swagger
app = Flask(__name__)
# Initialize Swagger UI
swagger = Swagger(app)
# Function to search for the endpoint in the database
def search_endpoint(query):
conn = sqlite3.connect("api_schema4_rmm.db")
cursor = conn.cursor()
cursor.execute("SELECT path, methods, description, request_body, responses FROM api_endpoints WHERE path LIKE ?", (f"%{query}%",))
results = cursor.fetchall()
conn.close()
return [{"path": path, "methods": methods, "description": description,
"request_body": json.loads(request_body) if request_body != "None" else None,
"responses": json.loads(responses)} for path, methods, description, request_body, responses in results]
# Function to forward requests to the external API
def forward_request(endpoint, methods, headers=None, data=None, params=None):
url = f"https://api.trmm.org{endpoint}"
if methods.lower() == "get":
response = requests.get(url, headers=headers, params=params)
elif methods.lower() == "post":
response = requests.post(url, headers=headers, json=data)
elif methods.lower() == "put":
response = requests.put(url, headers=headers, json=data)
elif methods.lower() == "delete":
response = requests.delete(url, headers=headers)
else:
return jsonify({"error": "Unsupported HTTP method"}), 405
# Forward the response from the external API back to the client
return jsonify(response.json()), response.status_code
# API endpoint to handle the query and forward the request
@app.route('/query', methods=['GET'])
def query_api():
"""
Query the API schema for an endpoint.
---
parameters:
- name: query
in: query
type: string
required: true
description: The path or query to search for in the API schema.
responses:
200:
description: A list of matching API paths, methods, and descriptions.
schema:
type: array
items:
type: object
properties:
path:
type: string
description: API endpoint path.
method:
type: string
description: HTTP method for the endpoint.
description:
type: string
description: Description of the endpoint.
security:
- API Key Auth: []
"""
query = request.args.get("query", "")
results = search_endpoint(query)
if not results:
return jsonify({"error": "No matching endpoints found"}), 404
# Extract endpoint and method from the results
endpoint_info = results[0] # You can customize this if there are multiple results
endpoint = endpoint_info['path']
method = endpoint_info['methods'].lower()
# Forward the request to the external API
api_key = request.headers.get('X-API-KEY')
headers = {"X-API-KEY": api_key}
data = request.get_json() if request.method in ['POST', 'PUT'] else None
params = request.args if request.method == 'GET' else None
return forward_request(endpoint, method, headers=headers, data=data, params=params)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5086)