Skip to content

Commit

Permalink
add Paste.delete
Browse files Browse the repository at this point in the history
  • Loading branch information
AbstractUmbra committed May 30, 2024
1 parent fca00b4 commit c3e6f24
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions mystbin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async def create_paste(
The paste that was created.
"""
data = await self.http.create_paste(files=files, password=password, expires=expires)
return Paste.from_create(data, files=files)
return Paste.from_create(data, files=files, http=self.http)

async def delete_paste(self, security_token: str, /) -> None:
"""|coro|
Expand Down Expand Up @@ -130,4 +130,4 @@ async def get_paste(self, paste_id: str, *, password: str | None = None, raw: bo
data = await self.http.get_paste(paste_id=paste_id, password=password)
if raw:
return [item["content"] for item in data["files"]]
return Paste.from_get(data)
return Paste.from_get(data, http=self.http)
19 changes: 15 additions & 4 deletions mystbin/paste.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

from typing_extensions import Self

from mystbin.types.responses import CreatePasteResponse, FileResponse, GetPasteResponse
from .http import HTTPClient
from .types.responses import CreatePasteResponse, FileResponse, GetPasteResponse


__all__ = (
Expand Down Expand Up @@ -126,9 +127,11 @@ class Paste:
"_security",
"_expires",
"_views",
"_http",
)

def __init__(self, *, id: str, created_at: str, files: Sequence[File]) -> None:
def __init__(self, *, http: HTTPClient, id: str, created_at: str, files: Sequence[File]) -> None:
self._http: HTTPClient = http
self.id: str = id
self.created_at: datetime.datetime = datetime.datetime.fromisoformat(created_at)
self.files: Sequence[File] = files
Expand Down Expand Up @@ -156,9 +159,10 @@ def security_token(self) -> str | None:
return self._security

@classmethod
def from_get(cls, payload: GetPasteResponse, /) -> Self:
def from_get(cls, payload: GetPasteResponse, /, *, http: HTTPClient) -> Self:
files = [File.from_data(data) for data in payload["files"]]
self = cls(
http=http,
id=payload["id"],
created_at=payload["created_at"],
files=files,
Expand All @@ -176,8 +180,9 @@ def from_get(cls, payload: GetPasteResponse, /) -> Self:
return self

@classmethod
def from_create(cls, payload: CreatePasteResponse, files: Sequence[File]) -> Self:
def from_create(cls, payload: CreatePasteResponse, files: Sequence[File], *, http: HTTPClient) -> Self:
self = cls(
http=http,
id=payload["id"],
created_at=payload["created_at"],
files=files,
Expand All @@ -193,3 +198,9 @@ def from_create(cls, payload: CreatePasteResponse, files: Sequence[File]) -> Sel
self._security = payload["safety"]

return self

async def delete(self) -> None:
if not self.security_token:
raise ValueError("Cannot delete a Paste with no Security Token set.")

await self._http.delete_paste(self.security_token)

0 comments on commit c3e6f24

Please sign in to comment.