diff --git a/.pdm-python b/.pdm-python deleted file mode 100644 index cf1cebf..0000000 --- a/.pdm-python +++ /dev/null @@ -1 +0,0 @@ -/home/runner/work/paste.py/paste.py/.venv/bin/python \ No newline at end of file diff --git a/sdk/example.py b/sdk/example.py new file mode 100644 index 0000000..efca28c --- /dev/null +++ b/sdk/example.py @@ -0,0 +1,27 @@ +from sdk.module import PasteBinSDK + +def test_pastebin_sdk(): + sdk = PasteBinSDK() + + try: + # Create a paste + paste_id = sdk.create_paste("print('Hello, World!')", ".py") + print(f"Created paste with ID: {paste_id}") + + # Retrieve the paste + content = sdk.get_paste(paste_id) + print(f"Retrieved paste content: {content}") + + # Delete the paste + result = sdk.delete_paste(paste_id) + print(f"Delete result: {result}") + + # Get supported languages + languages = sdk.get_languages() + print(f"Number of supported languages: {len(languages)}") + + except RuntimeError as e: + print(f"An error occurred: {e}") + +if __name__ == "__main__": + test_pastebin_sdk() \ No newline at end of file diff --git a/sdk/sdk/module.py b/sdk/sdk/module.py index ef739f6..5b7c428 100644 --- a/sdk/sdk/module.py +++ b/sdk/sdk/module.py @@ -3,58 +3,66 @@ from pathlib import Path class PasteBinSDK: - def __init__(self, base_url: str = "http://paste.fosscu.org"): + def __init__(self, base_url: str = "https://paste.fosscu.org"): self.base_url = base_url - def create_paste(self, content: Union[str, Path], file_extension: Optional[str] = None) -> str: + def create_paste(self, content: Union[str, Path], file_extension: str) -> str: """ Create a new paste. - :param content: The content to paste, either as a string or a Path to a file - :param file_extension: Optional file extension for syntax highlighting + :param file_extension: File extension for syntax highlighting (required) :return: The unique identifier of the created paste """ - if isinstance(content, Path): - with open(content, 'rb') as f: - files = {'file': f} - response = requests.post(f"{self.base_url}/file", files=files) - else: - data = {'content': content} - if file_extension: - data['extension'] = file_extension - response = requests.post(f"{self.base_url}/web", data=data) - - response.raise_for_status() - return response.text.strip() + try: + if isinstance(content, Path): + with open(content, 'r', encoding='utf-8') as f: + content = f.read() - def get_paste(self, uuid: str) -> str: + data = { + 'content': content, + 'extension': file_extension + } + response = requests.post(f"{self.base_url}/api/paste", json=data) + response.raise_for_status() + result = response.json() + return result['uuid'] + except requests.RequestException as e: + raise RuntimeError(f"Error creating paste: {str(e)}") + + def get_paste(self, uuid: str) -> dict: """ Retrieve a paste by its unique identifier. - :param uuid: The unique identifier of the paste - :return: The content of the paste + :return: A dictionary containing the paste details (uuid, content, extension) """ - response = requests.get(f"{self.base_url}/paste/{uuid}") - response.raise_for_status() - return response.text + try: + response = requests.get(f"{self.base_url}/api/paste/{uuid}") + response.raise_for_status() + return response.json() + except requests.RequestException as e: + raise RuntimeError(f"Error retrieving paste: {str(e)}") def delete_paste(self, uuid: str) -> str: """ Delete a paste by its unique identifier. - :param uuid: The unique identifier of the paste :return: A confirmation message """ - response = requests.delete(f"{self.base_url}/paste/{uuid}") - response.raise_for_status() - return response.text + try: + response = requests.delete(f"{self.base_url}/paste/{uuid}") + response.raise_for_status() + return response.text + except requests.RequestException as e: + raise RuntimeError(f"Error deleting paste: {str(e)}") def get_languages(self) -> dict: """ Get the list of supported languages for syntax highlighting. - :return: A dictionary of supported languages """ - response = requests.get(f"{self.base_url}/languages.json") - response.raise_for_status() - return response.json() \ No newline at end of file + try: + response = requests.get(f"{self.base_url}/languages.json") + response.raise_for_status() + return response.json() + except requests.RequestException as e: + raise RuntimeError(f"Error fetching languages: {str(e)}") \ No newline at end of file diff --git a/src/paste/main.py b/src/paste/main.py index 113f6c7..ae4ab3d 100644 --- a/src/paste/main.py +++ b/src/paste/main.py @@ -32,6 +32,7 @@ from pygments.util import ClassNotFound from typing import List, Optional from . import __version__, __author__, __contact__, __url__ +from .schema import PasteCreate, PasteResponse, PasteDetails description: str = "paste.py 🐍 - A pastebin written in python." @@ -306,7 +307,6 @@ async def get_languages() -> JSONResponse: # apis to create and get a paste which returns uuid and url (to be used by SDK) @app.post("/api/paste", response_model=PasteResponse) -@limiter.limit("100/minute") async def create_paste(paste: PasteCreate) -> JSONResponse: try: uuid: str = generate_uuid() diff --git a/src/paste/schema.py b/src/paste/schema.py index 0a89ea9..e5848cc 100644 --- a/src/paste/schema.py +++ b/src/paste/schema.py @@ -1,6 +1,6 @@ +from typing import Optional from pydantic import BaseModel - class Data(BaseModel): input_data: str @@ -15,4 +15,4 @@ class PasteResponse(BaseModel): class PasteDetails(BaseModel): uuid: str content: str - extension: Optional[str] \ No newline at end of file + extension: Optional[str] = None