Skip to content

Commit 42036aa

Browse files
authored
Added the ability to use other instances of the mystbin application. (#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.
1 parent 95ae7d2 commit 42036aa

File tree

6 files changed

+44
-23
lines changed

6 files changed

+44
-23
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.idea/
55
docs/build
66
dist/
7+
.ruff_cache/

mystbin/client.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,23 @@
3838

3939

4040
class Client:
41+
"""
42+
The main client class that interacts with the mystb.in API.
43+
44+
Parameters
45+
-----------
46+
session: Optional[:class:`aiohttp.ClientSession`]
47+
The session to use for the HTTP requests.
48+
If not provided, a new session will be created.
49+
api_base: :class:`str`
50+
The base URL for the mystbin instance.
51+
Defaults to ``https://mystb.in/``.
52+
This should begin with ``https://`` and should be the root URL of the mystbin instance.
53+
"""
4154
__slots__ = ("http",)
4255

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

4659
async def __aenter__(self) -> Self:
4760
return self

mystbin/http.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -107,38 +107,47 @@ def __exit__(
107107

108108
class Route:
109109
__slots__ = (
110-
"verb",
111110
"path",
112111
"url",
112+
"verb",
113113
)
114114

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

117117
def __init__(self, verb: SupportedHTTPVerb, path: str, **params: Any) -> None:
118+
118119
self.verb: SupportedHTTPVerb = verb
119120
self.path: str = path
120121
url = self.API_BASE + path
121122
if params:
122123
url = url.format_map({k: _uriquote(v) if isinstance(v, str) else v for k, v in params.items()})
123124
self.url: str = url
124125

125-
126126
class HTTPClient:
127127
__slots__ = (
128-
"_session",
129-
"_owns_session",
130128
"_async",
131-
"_token",
132129
"_locks",
133-
"user_agent",
130+
"_owns_session",
131+
"_session",
132+
"_token",
133+
"api_base",
134+
"user_agent"
134135
)
135136

136-
def __init__(self, *, session: aiohttp.ClientSession | None = None) -> None:
137+
def __init__(self, *, session: aiohttp.ClientSession | None = None, api_base: str | None = None) -> None:
137138
self._session: aiohttp.ClientSession | None = session
138139
self._owns_session: bool = False
139140
self._locks: weakref.WeakValueDictionary[str, asyncio.Lock] = weakref.WeakValueDictionary()
140141
user_agent = "mystbin.py (https://github.com/PythonistaGuild/mystbin.py {0}) Python/{1[0]}.{1[1]} aiohttp/{2}"
141142
self.user_agent: str = user_agent.format(__version__, sys.version_info, aiohttp.__version__)
143+
self._resolve_api(api_base)
144+
145+
def _resolve_api(self, api: str | None) -> None:
146+
if api:
147+
Route.API_BASE = api + "api" if api.endswith("/") else api + "/api"
148+
self.api_base = api + ("/" if not api.endswith("/") else "")
149+
else:
150+
self.api_base = "https://mystb.in/"
142151

143152
async def close(self) -> None:
144153
if self._session and self._owns_session:

mystbin/paste.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ class File:
5757
"""
5858

5959
__slots__ = (
60-
"filename",
61-
"content",
62-
"_lines_of_code",
60+
"_annotation",
6361
"_character_count",
62+
"_lines_of_code",
6463
"_parent_id",
65-
"_annotation",
64+
"content",
65+
"filename",
6666
)
6767

6868
def __init__(self, *, filename: str, content: str) -> None:
@@ -107,7 +107,7 @@ class Paste:
107107
_views: int | None
108108
_security: str | None
109109

110-
"""Represents a Paste object from mystb.in.
110+
"""Represents a Paste object from mystbin instances.
111111
112112
Attributes
113113
-----------
@@ -120,14 +120,14 @@ class Paste:
120120
"""
121121

122122
__slots__ = (
123-
"id",
123+
"_expires",
124+
"_http",
125+
"_security",
126+
"_views",
124127
"author_id",
125128
"created_at",
126129
"files",
127-
"_security",
128-
"_expires",
129-
"_views",
130-
"_http",
130+
"id",
131131
)
132132

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

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

149149
@property
150150
def expires(self) -> datetime.datetime | None:

mystbin/types/responses.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
from typing import TypedDict
2626

2727
__all__ = (
28+
"CreatePasteResponse",
2829
"FileResponse",
2930
"GetPasteResponse",
30-
"CreatePasteResponse",
3131
)
3232

3333

pyproject.toml

-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ ignore = [
9595
"SIM105",
9696
"UP034",
9797
"UP038",
98-
"ANN101",
99-
"ANN102",
10098
"ANN401",
10199
]
102100

0 commit comments

Comments
 (0)