Skip to content

Commit e42f0fd

Browse files
committed
created extra create and get apis which return json
1 parent 7df7e3d commit e42f0fd

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/paste/main.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,65 @@ async def get_languages() -> JSONResponse:
303303
detail=f"Error reading languages file: {e}",
304304
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
305305
)
306+
307+
# apis to create and get a paste which returns uuid and url (to be used by SDK)
308+
@app.post("/api/paste", response_model=PasteResponse)
309+
@limiter.limit("100/minute")
310+
async def create_paste(paste: PasteCreate) -> JSONResponse:
311+
try:
312+
uuid: str = generate_uuid()
313+
if uuid in large_uuid_storage:
314+
uuid = generate_uuid()
315+
316+
uuid_with_extension: str = f"{uuid}.{paste.extension}"
317+
path: str = f"data/{uuid_with_extension}"
318+
319+
with open(path, "w", encoding="utf-8") as f:
320+
f.write(paste.content)
321+
322+
large_uuid_storage.append(uuid_with_extension)
323+
324+
return JSONResponse(
325+
content=PasteResponse(
326+
uuid=uuid_with_extension,
327+
url=f"{BASE_URL}/paste/{uuid_with_extension}"
328+
).dict(),
329+
status_code=status.HTTP_201_CREATED
330+
)
331+
except Exception as e:
332+
raise HTTPException(
333+
detail=f"There was an error creating the paste: {str(e)}",
334+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
335+
)
336+
337+
@app.get("/api/paste/{uuid}", response_model=PasteDetails)
338+
async def get_paste_details(uuid: str) -> JSONResponse:
339+
if not "." in uuid:
340+
uuid = _find_without_extension(uuid)
341+
path: str = f"data/{uuid}"
342+
343+
try:
344+
with open(path, "r", encoding="utf-8") as f:
345+
content: str = f.read()
346+
347+
extension: str = Path(path).suffix[1:]
348+
349+
return JSONResponse(
350+
content=PasteDetails(
351+
uuid=uuid,
352+
content=content,
353+
extension=extension
354+
).dict(),
355+
status_code=status.HTTP_200_OK
356+
)
357+
except FileNotFoundError:
358+
raise HTTPException(
359+
detail="Paste not found",
360+
status_code=status.HTTP_404_NOT_FOUND,
361+
)
362+
except Exception as e:
363+
raise HTTPException(
364+
detail=f"Error retrieving paste: {str(e)}",
365+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
366+
)
367+

src/paste/schema.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,17 @@
22

33

44
class Data(BaseModel):
5-
input_data: str
5+
input_data: str
6+
7+
class PasteCreate(BaseModel):
8+
content: str
9+
extension: Optional[str] = None
10+
11+
class PasteResponse(BaseModel):
12+
uuid: str
13+
url: str
14+
15+
class PasteDetails(BaseModel):
16+
uuid: str
17+
content: str
18+
extension: Optional[str]

0 commit comments

Comments
 (0)