3
3
from pathlib import Path
4
4
5
5
class PasteBinSDK :
6
- def __init__ (self , base_url : str = "http ://paste.fosscu.org" ):
6
+ def __init__ (self , base_url : str = "https ://paste.fosscu.org" ):
7
7
self .base_url = base_url
8
8
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 :
10
10
"""
11
11
Create a new paste.
12
-
13
12
: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)
15
14
:return: The unique identifier of the created paste
16
15
"""
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 ()
29
20
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 :
31
33
"""
32
34
Retrieve a paste by its unique identifier.
33
-
34
35
: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)
36
37
"""
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 )} " )
40
44
41
45
def delete_paste (self , uuid : str ) -> str :
42
46
"""
43
47
Delete a paste by its unique identifier.
44
-
45
48
:param uuid: The unique identifier of the paste
46
49
:return: A confirmation message
47
50
"""
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 )} " )
51
57
52
58
def get_languages (self ) -> dict :
53
59
"""
54
60
Get the list of supported languages for syntax highlighting.
55
-
56
61
:return: A dictionary of supported languages
57
62
"""
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