Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
142 lines (117 loc) · 3.31 KB
/
Copy pathmain.py
File metadata and controls
142 lines (117 loc) · 3.31 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
#!/usr/bin/env python3
"""
CatFactsAPI - FastAPI service for random cat facts and breeds
"""
import sys
import json
import random
from fastapi import FastAPI, Response
from fastapi.middleware.cors import CORSMiddleware
try:
with open("facts.json", "r", encoding="utf-8") as f:
facts = json.load(f)
except FileNotFoundError:
print("Ошибка: файл facts.json не найден")
sys.exit(1)
except json.JSONDecodeError:
print("Ошибка: файл facts.json имеет неверный формат JSON")
sys.exit(1)
try:
with open("breeds.json", "r", encoding="utf-8") as f:
breeds = json.load(f)
except FileNotFoundError:
print("Ошибка: файл breeds.json не найден")
sys.exit(1)
except json.JSONDecodeError:
print("Ошибка: файл breeds.json имеет неверный формат JSON")
sys.exit(1)
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.randrange(len(facts))
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.randrange(len(breeds))
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"}