-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
165 lines (126 loc) · 3.64 KB
/
Copy pathmain.py
File metadata and controls
165 lines (126 loc) · 3.64 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import json
import random
from fastapi import FastAPI, Response
from fastapi.middleware.cors import CORSMiddleware
with open("facts.json", "r", encoding="utf-8") as f:
facts = json.load(f)
with open("breeds.json", "r", encoding="utf-8") as f:
breeds = json.load(f)
app = FastAPI(
title="CatFactsAPI",
description="Случайные факты о кошках на русском языке",
version="1.2.0",
contact={
"name": "FelineFantasy",
"email": "[email protected]",
"url": "https://github.com/FelineFantasy/CatFactsAPI",
}
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def info():
return {
"title": "CatFactsAPI",
"version": "1.2.0",
"total_facts": len(facts),
"total_breeds": len(breeds),
"endpoints": {
"/": "API info",
"/fact": "Random fact",
"/facts": "Multiple facts",
"/fact/{fact_id}": "Fact by ID",
"/breed": "Random breed",
"/breeds": "Multiple breeds",
"/breed/{breed_id}": "Breed by ID"
},
"docs": "https://catfactsapi.onrender.com/docs"
}
@app.get("/fact")
async def random_fact():
ind = random.randint(0, len(facts) - 1)
fact = facts[ind]
return {
"fact": fact,
"id": ind,
"length": len(fact)
}
@app.get("/facts")
async def multiple_facts(limit: int = 1):
limit = max(1, min(limit, len(facts)))
selected = random.sample(facts, limit)
return {
"facts": selected,
"count": limit,
"total": len(facts)
}
@app.get("/fact/{fact_id}")
async def get_fact_by_id(fact_id: int):
if 0 <= fact_id < len(facts):
return {
"fact": facts[fact_id],
"id": fact_id,
"length": len(facts[fact_id])
}
return {"error": f"ID от 0 до {len(facts) - 1}"}
@app.get("/breed")
async def random_breed():
ind = random.randint(0, len(breeds) - 1)
breed = breeds[ind]
return {
**breed,
"id": ind
}
@app.get("/breeds")
async def multiple_breeds(limit: int = 5):
limit = max(1, min(limit, len(breeds)))
selected = random.sample(breeds, limit)
return {
"breeds": selected,
"count": limit,
"total": len(breeds)
}
@app.get("/breed/{breed_id}")
async def get_breed_by_id(breed_id: int):
if 0 <= breed_id < len(breeds):
breed = breeds[breed_id]
return {
**breed,
"id": breed_id
}
return {"error": f"ID от 0 до {len(breeds) - 1}"}
@app.get("/health")
async def health():
return {"status": "ok"}
@app.head("/")
async def info_head():
return Response()
@app.head("/fact")
async def random_fact_head():
return Response()
@app.head("/facts")
async def multiple_facts_head():
return Response()
@app.head("/fact/{fact_id}")
async def get_fact_by_id_head(fact_id: int):
if 0 <= fact_id < len(facts):
return Response()
return Response(status_code=404)
@app.head("/breed")
async def random_breed_head():
return Response()
@app.head("/breeds")
async def multiple_breeds_head():
return Response()
@app.head("/breed/{breed_id}")
async def get_breed_by_id_head(breed_id: int):
if 0 <= breed_id < len(breeds):
return Response()
return Response(status_code=404)
@app.head("/health")
async def health_head():
return Response()