Skip to content

Commit a121276

Browse files
committed
fix compile time errors
1 parent e42f0fd commit a121276

File tree

5 files changed

+68
-34
lines changed

5 files changed

+68
-34
lines changed

.pdm-python

Lines changed: 0 additions & 1 deletion
This file was deleted.

sdk/example.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from sdk.module import PasteBinSDK
2+
3+
def test_pastebin_sdk():
4+
sdk = PasteBinSDK()
5+
6+
try:
7+
# Create a paste
8+
paste_id = sdk.create_paste("print('Hello, World!')", ".py")
9+
print(f"Created paste with ID: {paste_id}")
10+
11+
# Retrieve the paste
12+
content = sdk.get_paste(paste_id)
13+
print(f"Retrieved paste content: {content}")
14+
15+
# Delete the paste
16+
result = sdk.delete_paste(paste_id)
17+
print(f"Delete result: {result}")
18+
19+
# Get supported languages
20+
languages = sdk.get_languages()
21+
print(f"Number of supported languages: {len(languages)}")
22+
23+
except RuntimeError as e:
24+
print(f"An error occurred: {e}")
25+
26+
if __name__ == "__main__":
27+
test_pastebin_sdk()

sdk/sdk/module.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,66 @@
33
from pathlib import Path
44

55
class 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)}")

src/paste/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from pygments.util import ClassNotFound
3333
from typing import List, Optional
3434
from . import __version__, __author__, __contact__, __url__
35+
from .schema import PasteCreate, PasteResponse, PasteDetails
3536

3637
description: str = "paste.py 🐍 - A pastebin written in python."
3738

@@ -306,7 +307,6 @@ async def get_languages() -> JSONResponse:
306307

307308
# apis to create and get a paste which returns uuid and url (to be used by SDK)
308309
@app.post("/api/paste", response_model=PasteResponse)
309-
@limiter.limit("100/minute")
310310
async def create_paste(paste: PasteCreate) -> JSONResponse:
311311
try:
312312
uuid: str = generate_uuid()

src/paste/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
from typing import Optional
12
from pydantic import BaseModel
23

3-
44
class Data(BaseModel):
55
input_data: str
66

@@ -15,4 +15,4 @@ class PasteResponse(BaseModel):
1515
class PasteDetails(BaseModel):
1616
uuid: str
1717
content: str
18-
extension: Optional[str]
18+
extension: Optional[str] = None

0 commit comments

Comments
 (0)