diff --git a/.gitignore b/.gitignore index dee9eb0..d75087c 100755 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .idea/ docs/build dist/ +.ruff_cache/ \ No newline at end of file diff --git a/mystbin/client.py b/mystbin/client.py index ddb48bb..e4530e0 100755 --- a/mystbin/client.py +++ b/mystbin/client.py @@ -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 diff --git a/mystbin/http.py b/mystbin/http.py index c49d877..90fe1a0 100755 --- a/mystbin/http.py +++ b/mystbin/http.py @@ -107,14 +107,15 @@ 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 @@ -122,23 +123,31 @@ def __init__(self, verb: SupportedHTTPVerb, path: str, **params: Any) -> None: 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: diff --git a/mystbin/paste.py b/mystbin/paste.py index ddb9923..85942db 100755 --- a/mystbin/paste.py +++ b/mystbin/paste.py @@ -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: @@ -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 ----------- @@ -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: @@ -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: diff --git a/mystbin/types/responses.py b/mystbin/types/responses.py index 7957f0a..ecec910 100755 --- a/mystbin/types/responses.py +++ b/mystbin/types/responses.py @@ -25,9 +25,9 @@ from typing import TypedDict __all__ = ( + "CreatePasteResponse", "FileResponse", "GetPasteResponse", - "CreatePasteResponse", ) diff --git a/pyproject.toml b/pyproject.toml index 56b7f3e..03ec658 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -95,8 +95,6 @@ ignore = [ "SIM105", "UP034", "UP038", - "ANN101", - "ANN102", "ANN401", ]