Cristobal299 commited on
Commit
f1364d9
·
verified ·
1 Parent(s): 9f255f9

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py CHANGED
@@ -1,10 +1,15 @@
1
  # -*- coding: utf-8 -*-
2
  import os
3
  import sqlite3
 
 
4
  from flask import Flask, request, jsonify, g
5
 
6
  app = Flask(__name__)
7
 
 
 
 
8
  DATABASE = os.path.join(os.path.dirname(__file__), "tasks.db")
9
 
10
  def get_db():
@@ -35,6 +40,9 @@ def close_connection(exception):
35
  if db is not None:
36
  db.close()
37
 
 
 
 
38
  @app.route("/tasks", methods=["GET"])
39
  def list_tasks():
40
  db = get_db()
@@ -85,6 +93,9 @@ def delete_task(task_id):
85
  db.commit()
86
  return "", 204
87
 
 
 
 
88
  @app.route("/", methods=["GET"])
89
  def index():
90
  return "Task API is running."
@@ -93,10 +104,76 @@ def index():
93
  def ping():
94
  return "pong", 200
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  @app.errorhandler(404)
97
  def not_found(e):
98
  return jsonify({"error": "not found"}), 404
99
 
 
 
 
100
  if __name__ == "__main__":
101
  if not os.path.isfile(DATABASE):
102
  init_db()
 
1
  # -*- coding: utf-8 -*-
2
  import os
3
  import sqlite3
4
+ import json
5
+ import requests
6
  from flask import Flask, request, jsonify, g
7
 
8
  app = Flask(__name__)
9
 
10
+ # ----------------------------------------------------------------------
11
+ # Database utilities (unchanged)
12
+ # ----------------------------------------------------------------------
13
  DATABASE = os.path.join(os.path.dirname(__file__), "tasks.db")
14
 
15
  def get_db():
 
40
  if db is not None:
41
  db.close()
42
 
43
+ # ----------------------------------------------------------------------
44
+ # Existing task endpoints (unchanged)
45
+ # ----------------------------------------------------------------------
46
  @app.route("/tasks", methods=["GET"])
47
  def list_tasks():
48
  db = get_db()
 
93
  db.commit()
94
  return "", 204
95
 
96
+ # ----------------------------------------------------------------------
97
+ # Health endpoints (unchanged)
98
+ # ----------------------------------------------------------------------
99
  @app.route("/", methods=["GET"])
100
  def index():
101
  return "Task API is running."
 
104
  def ping():
105
  return "pong", 200
106
 
107
+ # ----------------------------------------------------------------------
108
+ # New: Generic API connector
109
+ # ----------------------------------------------------------------------
110
+ @app.route("/api/connector", methods=["POST"])
111
+ def api_connector():
112
+ """
113
+ Expects a JSON payload with the following fields:
114
+ {
115
+ "url": "https://example.com/endpoint",
116
+ "method": "GET|POST|PUT|DELETE|PATCH",
117
+ "headers": {"Authorization": "Bearer ...", ...}, # optional
118
+ "params": {"q": "search"}, # optional, for query string
119
+ "json": {"key": "value"} # optional, for JSON body
120
+ }
121
+ The endpoint forwards the request to the target API and returns
122
+ the raw response (status code, headers and JSON body if possible).
123
+ """
124
+ payload = request.get_json()
125
+ if not payload:
126
+ return jsonify({"error": "invalid json payload"}), 400
127
+
128
+ url = payload.get("url")
129
+ method = payload.get("method", "GET").upper()
130
+ headers = payload.get("headers", {})
131
+ params = payload.get("params", {})
132
+ json_body = payload.get("json", None)
133
+
134
+ if not url:
135
+ return jsonify({"error": "url is required"}), 400
136
+
137
+ # Optional: inject a default API key from environment if not provided
138
+ default_key = os.environ.get("DEFAULT_API_KEY")
139
+ if default_key and "Authorization" not in headers:
140
+ headers["Authorization"] = f"Bearer {default_key}"
141
+
142
+ try:
143
+ response = requests.request(
144
+ method=method,
145
+ url=url,
146
+ headers=headers,
147
+ params=params,
148
+ json=json_body,
149
+ timeout=30
150
+ )
151
+ except requests.RequestException as e:
152
+ return jsonify({"error": "request failed", "details": str(e)}), 502
153
+
154
+ # Try to parse JSON, fallback to raw text
155
+ try:
156
+ resp_content = response.json()
157
+ except ValueError:
158
+ resp_content = response.text
159
+
160
+ result = {
161
+ "status_code": response.status_code,
162
+ "headers": dict(response.headers),
163
+ "content": resp_content
164
+ }
165
+ return jsonify(result), response.status_code
166
+
167
+ # ----------------------------------------------------------------------
168
+ # Error handling (unchanged)
169
+ # ----------------------------------------------------------------------
170
  @app.errorhandler(404)
171
  def not_found(e):
172
  return jsonify({"error": "not found"}), 404
173
 
174
+ # ----------------------------------------------------------------------
175
+ # Main entry point (unchanged)
176
+ # ----------------------------------------------------------------------
177
  if __name__ == "__main__":
178
  if not os.path.isfile(DATABASE):
179
  init_db()