-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (46 loc) · 1.7 KB
/
main.py
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
import random
from typing import Optional, List, Annotated
import uvicorn
from faker import Faker
from faker.providers import BaseProvider
from fastapi import FastAPI, Query
from fields import available_fields
app = FastAPI()
def get_fake_data(fake: Faker, methods: List[str]):
data = {}
for provider in fake.get_providers():
if isinstance(provider, BaseProvider):
provider_methods = [method for method in dir(provider) if method[0] != "_"]
for method in methods:
if method in provider_methods:
data[method] = getattr(provider, method)()
return data
@app.get("/", name="List of available routes")
async def get_index():
skip_route_names = ["openapi", "swagger_ui_redirect"]
routes = [
{"methods": route.methods, "path": route.path, "name": route.name}
for route in app.routes
if route.name not in skip_route_names
]
return routes
@app.get("/available_fields", name="List of available fields")
async def get_fields(locale: Optional[str] = 'en_US'):
fake = Faker(locale)
return get_fake_data(fake, available_fields)
@app.get("/{any}", name="Dynamic data getter")
async def get_data(
locale: Optional[str] = 'en_US',
limit: Annotated[int, Query(gt=1, lt=500)] = 10,
fields: Optional[str] = None
):
fake = Faker(locale)
field_list = [field.strip() for field in fields.split(',')] if fields else []
if not field_list:
field_list = random.choices(available_fields, k=6)
data = []
for _ in range(limit):
data.append(get_fake_data(fake, field_list))
return data
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)