Compare commits
No commits in common. "468390231ba4064e7460412456fbac2bc6d7727e" and "c443640ae7684069e1e5faca974f8be9233b47d0" have entirely different histories.
468390231b
...
c443640ae7
19
api.py
19
api.py
@ -1,9 +1,8 @@
|
|||||||
from fastapi import FastAPI, HTTPException
|
from fastapi import FastAPI, Response
|
||||||
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")
|
||||||
@ -27,31 +26,29 @@ 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):
|
async def read_url(url_id, response: Response):
|
||||||
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:
|
||||||
raise HTTPException(status_code=404, detail="Url not found")
|
response.status_code = 404
|
||||||
|
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 { "shortenedUrl": "http://localhost:8000/" + url_id }
|
return
|
||||||
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):
|
async def delete_url(url_id, response: Response):
|
||||||
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:
|
||||||
raise HTTPException(status_code=404, detail="Url not found")
|
response.status_code = 404
|
||||||
|
return { "error": "Url not found" }
|
||||||
|
@ -2,4 +2,3 @@ fastapi
|
|||||||
uvicorn
|
uvicorn
|
||||||
couchdb
|
couchdb
|
||||||
aiofiles
|
aiofiles
|
||||||
validators
|
|
||||||
|
@ -10,8 +10,7 @@
|
|||||||
<h1>Zip My Link</h1>
|
<h1>Zip My Link</h1>
|
||||||
<p>Type your long URL into the box and get a shorter URL back!</p>
|
<p>Type your long URL into the box and get a shorter URL back!</p>
|
||||||
<input type="text" id="url-input" placeholder="Enter your URL here">
|
<input type="text" id="url-input" placeholder="Enter your URL here">
|
||||||
<button onclick="shortenUrl()" id="submit-btn">Shorten Url</button>
|
<button onclick="submitUrl()">Shorten Url</button>
|
||||||
<p id="result"></p>
|
<p id="result"></p>
|
||||||
<script src="static/main.js"></script>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
async function shortenUrl() {
|
|
||||||
const url = document.getElementById("url-input").value;
|
|
||||||
const btn = document.getElementById("submit-btn").value;
|
|
||||||
// btn.enab
|
|
||||||
const response = await fetch("/api/v1/urls", {
|
|
||||||
method: 'PUT',
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify({ url: url, username: "anonymous" })
|
|
||||||
});
|
|
||||||
const data = await response.json();
|
|
||||||
const resultElement = document.getElementById("result");
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
resultElement.textContent = `Shortened Url: ${data.shortenedUrl}`
|
|
||||||
resultElement.style.color = "black";
|
|
||||||
} else {
|
|
||||||
resultElement.textContent = `Error: ${data.detail}`
|
|
||||||
resultElement.style.color = "red";
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user