33from pathlib import Path
44
55class PasteBinSDK :
6- def __init__ (self , base_url : str = "http ://paste.fosscu.org" ):
6+ def __init__ (self , base_url : str = "https ://paste.fosscu.org" ):
77 self .base_url = base_url
88
9- def create_paste (self , content : Union [str , Path ], file_extension : Optional [ str ] = None ) -> str :
9+ def create_paste (self , content : Union [str , Path ], file_extension : str ) -> str :
1010 """
1111 Create a new paste.
12-
1312 :param content: The content to paste, either as a string or a Path to a file
14- :param file_extension: Optional file extension for syntax highlighting
13+ :param file_extension: File extension for syntax highlighting (required)
1514 :return: The unique identifier of the created paste
1615 """
17- if isinstance (content , Path ):
18- with open (content , 'rb' ) as f :
19- files = {'file' : f }
20- response = requests .post (f"{ self .base_url } /file" , files = files )
21- else :
22- data = {'content' : content }
23- if file_extension :
24- data ['extension' ] = file_extension
25- response = requests .post (f"{ self .base_url } /web" , data = data )
26-
27- response .raise_for_status ()
28- return response .text .strip ()
16+ try :
17+ if isinstance (content , Path ):
18+ with open (content , 'r' , encoding = 'utf-8' ) as f :
19+ content = f .read ()
2920
30- def get_paste (self , uuid : str ) -> str :
21+ data = {
22+ 'content' : content ,
23+ 'extension' : file_extension
24+ }
25+ response = requests .post (f"{ self .base_url } /api/paste" , json = data )
26+ response .raise_for_status ()
27+ result = response .json ()
28+ return result ['uuid' ]
29+ except requests .RequestException as e :
30+ raise RuntimeError (f"Error creating paste: { str (e )} " )
31+
32+ def get_paste (self , uuid : str ) -> dict :
3133 """
3234 Retrieve a paste by its unique identifier.
33-
3435 :param uuid: The unique identifier of the paste
35- :return: The content of the paste
36+ :return: A dictionary containing the paste details (uuid, content, extension)
3637 """
37- response = requests .get (f"{ self .base_url } /paste/{ uuid } " )
38- response .raise_for_status ()
39- return response .text
38+ try :
39+ response = requests .get (f"{ self .base_url } /api/paste/{ uuid } " )
40+ response .raise_for_status ()
41+ return response .json ()
42+ except requests .RequestException as e :
43+ raise RuntimeError (f"Error retrieving paste: { str (e )} " )
4044
4145 def delete_paste (self , uuid : str ) -> str :
4246 """
4347 Delete a paste by its unique identifier.
44-
4548 :param uuid: The unique identifier of the paste
4649 :return: A confirmation message
4750 """
48- response = requests .delete (f"{ self .base_url } /paste/{ uuid } " )
49- response .raise_for_status ()
50- return response .text
51+ try :
52+ response = requests .delete (f"{ self .base_url } /paste/{ uuid } " )
53+ response .raise_for_status ()
54+ return response .text
55+ except requests .RequestException as e :
56+ raise RuntimeError (f"Error deleting paste: { str (e )} " )
5157
5258 def get_languages (self ) -> dict :
5359 """
5460 Get the list of supported languages for syntax highlighting.
55-
5661 :return: A dictionary of supported languages
5762 """
58- response = requests .get (f"{ self .base_url } /languages.json" )
59- response .raise_for_status ()
60- return response .json ()
63+ try :
64+ response = requests .get (f"{ self .base_url } /languages.json" )
65+ response .raise_for_status ()
66+ return response .json ()
67+ except requests .RequestException as e :
68+ raise RuntimeError (f"Error fetching languages: { str (e )} " )
0 commit comments