Skip to content

Commit

Permalink
fix compile time errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Utkarsh4517 committed Sep 21, 2024
1 parent e42f0fd commit a121276
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 34 deletions.
1 change: 0 additions & 1 deletion .pdm-python

This file was deleted.

27 changes: 27 additions & 0 deletions sdk/example.py
Original file line number Diff line number Diff line change
@@ -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()
68 changes: 38 additions & 30 deletions sdk/sdk/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
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)}")
2 changes: 1 addition & 1 deletion src/paste/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/paste/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional
from pydantic import BaseModel


class Data(BaseModel):
input_data: str

Expand All @@ -15,4 +15,4 @@ class PasteResponse(BaseModel):
class PasteDetails(BaseModel):
uuid: str
content: str
extension: Optional[str]
extension: Optional[str] = None

0 comments on commit a121276

Please sign in to comment.