@@ -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+
0 commit comments