66 lines
1.3 KiB
Python
66 lines
1.3 KiB
Python
from fastapi import FastAPI, Header, Path
|
|
from pydantic import BaseModel
|
|
from typing import List
|
|
from random import randint
|
|
from starlette.routing import Route
|
|
|
|
|
|
app = FastAPI(
|
|
title='Capacity API', description='API for returning metric data.',
|
|
version='1.0.0', redoc_url='/documentation'
|
|
)
|
|
|
|
|
|
class MetricResponse(BaseModel):
|
|
data: List[int]
|
|
time: List[int]
|
|
metric: str
|
|
|
|
|
|
@app.get("/metrics/{metric}", response_model=MetricResponse)
|
|
def get_metric_data(
|
|
metric: str = Path(
|
|
...,
|
|
title='Metric',
|
|
description='A data metric. Valid metrics are ["bets", '
|
|
'"deposits", "registrations"]',
|
|
regex=r'^(bets$)|(deposits$)|(registrations$)',
|
|
),
|
|
earliest: str = Header(None, title='Earliest time',),
|
|
):
|
|
"""
|
|
Will return metric data for a timespan.
|
|
"""
|
|
# do something
|
|
time = []
|
|
data = []
|
|
|
|
for i in range(0, 10):
|
|
time.append(randint(1582238840, 1582238890))
|
|
data.append(randint(1, 5000))
|
|
|
|
return {
|
|
'metric': metric,
|
|
'time': time,
|
|
'data': data,
|
|
}
|
|
|
|
|
|
def test(request):
|
|
return 'request'
|
|
|
|
|
|
app.route('/test', [test])
|
|
|
|
|
|
class Item(BaseModel):
|
|
name: str
|
|
description: str = None
|
|
price: float
|
|
tax: float = None
|
|
|
|
|
|
@app.post("/items/")
|
|
async def create_item(item: Item):
|
|
return item
|