Skip to content

Commit

Permalink
Added the ability to use other instances of the mystbin application. (#…
Browse files Browse the repository at this point in the history
…22)

* add the ability to provide a custom url for pastes

* removed ANN101 and ANN102 from ruff ignore.

they have been removed from ruff checks, and as such are no longer needed

* updated my code to directly patch Route objects, rather than adding that as another parameter

* Improved how api_base is implemented.

Made it more natural - it can end in a / or not.
Also fixed some bugs as a result of not adding api_base to __slots__ of HTTPClient.
  • Loading branch information
ShadowFox88 authored Dec 28, 2024
1 parent 95ae7d2 commit 42036aa
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.idea/
docs/build
dist/
.ruff_cache/
17 changes: 15 additions & 2 deletions mystbin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,23 @@


class Client:
"""
The main client class that interacts with the mystb.in API.
Parameters
-----------
session: Optional[:class:`aiohttp.ClientSession`]
The session to use for the HTTP requests.
If not provided, a new session will be created.
api_base: :class:`str`
The base URL for the mystbin instance.
Defaults to ``https://mystb.in/``.
This should begin with ``https://`` and should be the root URL of the mystbin instance.
"""
__slots__ = ("http",)

def __init__(self, *, session: ClientSession | None = None) -> None:
self.http: HTTPClient = HTTPClient(session=session)
def __init__(self, *, session: ClientSession | None = None, api_base: str = "https://mystb.in/") -> None:
self.http: HTTPClient = HTTPClient(session=session, api_base=api_base)

async def __aenter__(self) -> Self:
return self
Expand Down
23 changes: 16 additions & 7 deletions mystbin/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,38 +107,47 @@ def __exit__(

class Route:
__slots__ = (
"verb",
"path",
"url",
"verb",
)

API_BASE: ClassVar[str] = "https://mystb.in/api"

def __init__(self, verb: SupportedHTTPVerb, path: str, **params: Any) -> None:

self.verb: SupportedHTTPVerb = verb
self.path: str = path
url = self.API_BASE + path
if params:
url = url.format_map({k: _uriquote(v) if isinstance(v, str) else v for k, v in params.items()})
self.url: str = url


class HTTPClient:
__slots__ = (
"_session",
"_owns_session",
"_async",
"_token",
"_locks",
"user_agent",
"_owns_session",
"_session",
"_token",
"api_base",
"user_agent"
)

def __init__(self, *, session: aiohttp.ClientSession | None = None) -> None:
def __init__(self, *, session: aiohttp.ClientSession | None = None, api_base: str | None = None) -> None:
self._session: aiohttp.ClientSession | None = session
self._owns_session: bool = False
self._locks: weakref.WeakValueDictionary[str, asyncio.Lock] = weakref.WeakValueDictionary()
user_agent = "mystbin.py (https://github.com/PythonistaGuild/mystbin.py {0}) Python/{1[0]}.{1[1]} aiohttp/{2}"
self.user_agent: str = user_agent.format(__version__, sys.version_info, aiohttp.__version__)
self._resolve_api(api_base)

def _resolve_api(self, api: str | None) -> None:
if api:
Route.API_BASE = api + "api" if api.endswith("/") else api + "/api"
self.api_base = api + ("/" if not api.endswith("/") else "")
else:
self.api_base = "https://mystb.in/"

async def close(self) -> None:
if self._session and self._owns_session:
Expand Down
22 changes: 11 additions & 11 deletions mystbin/paste.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ class File:
"""

__slots__ = (
"filename",
"content",
"_lines_of_code",
"_annotation",
"_character_count",
"_lines_of_code",
"_parent_id",
"_annotation",
"content",
"filename",
)

def __init__(self, *, filename: str, content: str) -> None:
Expand Down Expand Up @@ -107,7 +107,7 @@ class Paste:
_views: int | None
_security: str | None

"""Represents a Paste object from mystb.in.
"""Represents a Paste object from mystbin instances.
Attributes
-----------
Expand All @@ -120,14 +120,14 @@ class Paste:
"""

__slots__ = (
"id",
"_expires",
"_http",
"_security",
"_views",
"author_id",
"created_at",
"files",
"_security",
"_expires",
"_views",
"_http",
"id",
)

def __init__(self, *, http: HTTPClient, id: str, created_at: str, files: Sequence[File]) -> None:
Expand All @@ -144,7 +144,7 @@ def __repr__(self) -> str:

@property
def url(self) -> str:
return f"https://mystb.in/{self.id}"
return f"{self._http.api_base}{self.id}"

@property
def expires(self) -> datetime.datetime | None:
Expand Down
2 changes: 1 addition & 1 deletion mystbin/types/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
from typing import TypedDict

__all__ = (
"CreatePasteResponse",
"FileResponse",
"GetPasteResponse",
"CreatePasteResponse",
)


Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ ignore = [
"SIM105",
"UP034",
"UP038",
"ANN101",
"ANN102",
"ANN401",
]

Expand Down

0 comments on commit 42036aa

Please sign in to comment.