Skip to content

Commit

Permalink
feat: Basic API setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Vyvy-vi committed Aug 7, 2021
1 parent 92624ef commit 1f7dfe5
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import uvicorn
import random

from typing import Optional, List, Dict
from fastapi import FastAPI, responses, HTTPException

from fastapi import Path, Query

# from pydantic import BaseModel

app = FastAPI()

jokes = [
"Jokes are going through",
"hehe this is a joke",
"more jokes"
]


@app.get('/')
def index():
body = "<h1>The Heptagram API</h1>"
return responses.HTMLResponse(content=body)

@app.get('/jokes/{joke_id}')
def jokes_by_id(joke_id: int):
if joke_id > len(jokes):
raise HTTPException(
status_code=400,
detail=[{
"loc": [
"path",
"joke_id"
],
"msg": f"Error: joke with ID = {joke_id} not found",
"type": "Bad Request"
}]
)

return responses.JSONResponse(
content={
"joke": jokes[joke_id],
"id": joke_id
}
)



@app.get('/diceroll')
def _diceroll(num: int = 1, sides: int = 6):
if num < 1:
raise HTTPException(
status_code=400,
detail=[{
"loc": [
"query",
"num"
],
"msg": "Error: num should be greater than 0",
"type": "Bad Request"
}]
)
if num > 10000:
raise HTTPException(
status_code=400,
detail=[{
"loc": [
"query",
"num"
],
"msg": "Error: num should be smaller than 10000",
"type": "Bad Request"
}]
)
if sides > 100 or sides < 3:
raise HTTPException(
status_code=400,
detail=[{
"loc": [
"query",
"num"
],
"msg": "Error: sides must be beween 3 and 100",
"type": "Bad Request"
}]
)

return responses.JSONResponse(
content={
"task": f"Diceroll - d{sides} x {num}",
"result": random.choices(range(1, sides), k=num)
}
)

def run():
uvicorn.run("src.main:app", reload=True)

if __name__ == '__main__':
run()

0 comments on commit 1f7dfe5

Please sign in to comment.