Replace manual response with HTTPException for errors, validate URLs
This commit is contained in:
parent
a7415c6393
commit
468390231b
19
api.py
19
api.py
@ -1,8 +1,9 @@
|
|||||||
from fastapi import FastAPI, Response
|
from fastapi import FastAPI, HTTPException
|
||||||
from fastapi.responses import RedirectResponse, FileResponse
|
from fastapi.responses import RedirectResponse, FileResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
import couchdb
|
import couchdb
|
||||||
import uuid
|
import uuid
|
||||||
|
import validators
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
|
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
|
||||||
@ -26,29 +27,31 @@ async def read_urls():
|
|||||||
return [ { id: db[id].get('full_url') } for id in db ]
|
return [ { id: db[id].get('full_url') } for id in db ]
|
||||||
|
|
||||||
@app.get("/api/v1/urls/{url_id}")
|
@app.get("/api/v1/urls/{url_id}")
|
||||||
async def read_url(url_id, response: Response):
|
async def read_url(url_id):
|
||||||
db = couch["urls"]
|
db = couch["urls"]
|
||||||
if url_id in db:
|
if url_id in db:
|
||||||
return db[url_id]
|
return db[url_id]
|
||||||
else:
|
else:
|
||||||
response.status_code = 404
|
raise HTTPException(status_code=404, detail="Url not found")
|
||||||
return { "error": "Url not found" }
|
|
||||||
|
|
||||||
# TODO: Throttle
|
# TODO: Throttle
|
||||||
# TODO: If user not found, generate a new JWT
|
# TODO: If user not found, generate a new JWT
|
||||||
@app.put("/api/v1/urls")
|
@app.put("/api/v1/urls")
|
||||||
async def create_url(body: dict):
|
async def create_url(body: dict):
|
||||||
|
if validators.url(body["url"]):
|
||||||
db = couch["urls"]
|
db = couch["urls"]
|
||||||
|
# TODO: Calculate how many unique IDs we are actually generating
|
||||||
url_id = uuid.uuid4().hex[:6]
|
url_id = uuid.uuid4().hex[:6]
|
||||||
db[url_id] = { "full_url": body["url"], "user_id": body["username"] }
|
db[url_id] = { "full_url": body["url"], "user_id": body["username"] }
|
||||||
return
|
return { "shortenedUrl": "http://localhost:8000/" + url_id }
|
||||||
|
else:
|
||||||
|
raise HTTPException(status_code=400, detail="Url provided is invalid")
|
||||||
|
|
||||||
@app.delete("/api/v1/urls/{url_id}")
|
@app.delete("/api/v1/urls/{url_id}")
|
||||||
async def delete_url(url_id, response: Response):
|
async def delete_url(url_id):
|
||||||
db = couch["urls"]
|
db = couch["urls"]
|
||||||
if url_id in db:
|
if url_id in db:
|
||||||
del db[url_id]
|
del db[url_id]
|
||||||
return { "message": "Url deleted" }
|
return { "message": "Url deleted" }
|
||||||
else:
|
else:
|
||||||
response.status_code = 404
|
raise HTTPException(status_code=404, detail="Url not found")
|
||||||
return { "error": "Url not found" }
|
|
||||||
|
@ -2,3 +2,4 @@ fastapi
|
|||||||
uvicorn
|
uvicorn
|
||||||
couchdb
|
couchdb
|
||||||
aiofiles
|
aiofiles
|
||||||
|
validators
|
||||||
|
Loading…
x
Reference in New Issue
Block a user