Skip to content

Commit

Permalink
feat: add quotes route
Browse files Browse the repository at this point in the history
  • Loading branch information
Vyvy-vi committed Aug 21, 2021
1 parent 5b5ffdf commit 6a9558a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from fastapi import FastAPI, responses

# from pydantic import BaseModel
from .routes import coinflip, diceroll, jokes
from .routes import coinflip, diceroll, jokes, quotes

app = FastAPI(
title="heptagram-api",
Expand All @@ -16,7 +16,7 @@
app.include_router(coinflip.router)
app.include_router(jokes.router)
app.include_router(diceroll.router)

app.include_router(quotes.router)

@app.get("/")
async def index():
Expand Down
40 changes: 40 additions & 0 deletions src/routes/quotes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import random

from fastapi import APIRouter, Path, Query

router = APIRouter()

quotes = {
0: ("Quotes are going through", "potato"),
1: ("Quotes", "person"),
}


@router.get("/quotes/all")
async def all_quotes():
return {
"quotes": [
{"id": q_id, "quote": quotes[q_id][0], "author": quotes[q_id][1]}
for q_id in quotes
]
}


@router.get("/quotes/{quote_id}")
async def quotes_by_id(quote_id: int = Path(..., ge=0, lt=len(quotes))):
return {"id": quote_id, "quote": quotes[quote_id][0], "author": quotes[quote_id][1]}


@router.get("/quotes")
async def get_quotes(num: int = Query(1, ge=1, lt=len(quotes))):
random_ids = sorted(random.sample(range(len(quotes)), num))
return {
"quotes": [
{
"id": quote_id,
"quote": quotes[quote_id][0],
"author": quotes[quote_id][1],
}
for quote_id in random_ids
]
}

0 comments on commit 6a9558a

Please sign in to comment.