-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from Utkarsh4517/feat-sdk
Feat: Created SDK
- Loading branch information
Showing
9 changed files
with
234 additions
and
5 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import requests | ||
from typing import Optional, Union | ||
from pathlib import Path | ||
|
||
class PasteBinSDK: | ||
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: 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: File extension for syntax highlighting (required) | ||
:return: The unique identifier of the created paste | ||
""" | ||
try: | ||
if isinstance(content, Path): | ||
with open(content, 'r', encoding='utf-8') as f: | ||
content = f.read() | ||
|
||
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: A dictionary containing the paste details (uuid, content, extension) | ||
""" | ||
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 | ||
""" | ||
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 | ||
""" | ||
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)}") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
from typing import Optional | ||
from pydantic import BaseModel | ||
|
||
|
||
class Data(BaseModel): | ||
input_data: str | ||
input_data: str | ||
|
||
class PasteCreate(BaseModel): | ||
content: str | ||
extension: Optional[str] = None | ||
|
||
class PasteResponse(BaseModel): | ||
uuid: str | ||
url: str | ||
|
||
class PasteDetails(BaseModel): | ||
uuid: str | ||
content: str | ||
extension: Optional[str] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters