-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.py
More file actions
129 lines (93 loc) · 3.28 KB
/
API.py
File metadata and controls
129 lines (93 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
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
import csv
import io
import duckdb
import pandas as pd
from flask import Flask, Response, jsonify, make_response, request
from data_clean import load_and_clean_data
app = Flask(__name__)
df = load_and_clean_data("data.csv")
@app.route("/")
def index():
return "Welcome to my API! Check the README for usage."
@app.route("/data", methods=["GET"])
def list_records():
conn = duckdb.connect("my_database.db")
df = conn.sql("SELECT * FROM loan_data").df()
conn.close()
results = df.copy()
for key, value in request.args.items():
if key in ["limit", "offset", "format"]:
continue
if key in results.columns:
results = results[results[key].astype(str) == value]
try:
limit = int(request.args.get("limit", 10000))
offset = int(request.args.get("offset", 0))
except ValueError:
limit = 10000
offset = 0
if offset < len(results):
results = results[offset : offset + limit]
else:
results = results[0:0]
output_format = request.args.get("format", "json").lower()
if output_format == "csv":
return Response(
results.to_csv(index=False),
mimetype="text/csv",
headers={"Content-disposition": "attachment; filename=data.csv"},
)
else:
return Response(results.to_json(orient="records"), mimetype="application/json")
@app.route("/data/<id>", methods=["GET"])
def get_record(id):
try:
lookup_id = int(id)
except ValueError:
lookup_id = id
record = df[df["id"] == lookup_id]
if record.empty:
return jsonify({"error": "Record not found"}), 404
return Response(record.to_json(orient="records"), mimetype="application/json")
@app.route("/users", methods=["POST"])
def add_user():
data = request.get_json()
username = data.get("username")
age = data.get("age")
country = data.get("country")
if not username or not age or not country:
return jsonify({"error": "Missing username, age, or country"}), 400
conn = duckdb.connect("my_database.db")
conn.execute(
"INSERT INTO users (username, age, country) VALUES (?, ?, ?)",
(username, age, country),
)
conn.close()
return jsonify({"message": f"User '{username}' successfully added!"}), 201
@app.route("/users/stats", methods=["GET"])
def get_user_stats():
conn = duckdb.connect("my_database.db")
total_users = conn.execute("SELECT COUNT(*) FROM users").fetchone()[0]
avg_age = conn.execute("SELECT AVG(age) FROM users").fetchone()[0]
if avg_age is None:
avg_age = 0
else:
avg_age = round(avg_age, 1)
top_countries_data = conn.execute("""
SELECT country, COUNT(*) as user_count
FROM users
GROUP BY country
ORDER BY user_count DESC
LIMIT 3
""").fetchall()
conn.close()
top_countries = [{"country": row[0], "count": row[1]} for row in top_countries_data]
return jsonify(
{
"total_users": total_users,
"average_age": avg_age,
"top_countries": top_countries,
}
)
if __name__ == "__main__":
app.run(debug=True)