Skip to content

Commit 5deb4b3

Browse files
committed
ClientFeatures, deprecate experimental_event_style
1 parent 31df81e commit 5deb4b3

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ testing/
88
.vscode/
99
*.pyc
1010
*.log
11-
venv/
11+
venv/
12+
.DS_Store

docs/api.rst

+6
Original file line numberDiff line numberDiff line change
@@ -2688,6 +2688,12 @@ CategoryUserOverride
26882688
.. autoclass:: CategoryUserOverride()
26892689
:members:
26902690

2691+
ClientFeatures
2692+
~~~~~~~~~~~~~~~
2693+
2694+
.. autoclass:: ClientFeatures()
2695+
:members:
2696+
26912697
Embed
26922698
~~~~~~
26932699

guilded/client.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282

8383
__all__ = (
8484
'Client',
85+
'ClientFeatures',
8586
)
8687

8788

@@ -99,6 +100,30 @@ def __getattr__(self, attr: str) -> None:
99100
_loop: Any = _LoopSentinel()
100101

101102

103+
class ClientFeatures:
104+
"""Opt-in or out of Guilded or guilded.py features.
105+
106+
All parameters are optional.
107+
108+
Parameters
109+
-----------
110+
experimental_event_style: :class:`bool`
111+
Enables a more simplified event handling interface.
112+
Read more about this `here <https://www.guilded.gg/guilded-api/blog/updates/Event-style-experiment>`_.
113+
official_markdown: :class:`bool`
114+
Enables new (2024) markdown support for requests made by the client
115+
as well as events received.
116+
"""
117+
def __init__(
118+
self,
119+
*,
120+
experimental_event_style: bool = False,
121+
official_markdown: bool = False,
122+
) -> None:
123+
self.experimental_event_style = experimental_event_style
124+
self.official_markdown = official_markdown
125+
126+
102127
class Client:
103128
"""The basic client class for interfacing with Guilded.
104129
@@ -109,6 +134,8 @@ class Client:
109134
max_messages: Optional[:class:`int`]
110135
The maximum number of messages to store in the internal message cache.
111136
This defaults to ``1000``. Passing in ``None`` disables the message cache.
137+
features: Optional[:class:`.ClientFeatures`]
138+
Client features to opt in or out of.
112139
113140
Attributes
114141
-----------
@@ -123,20 +150,29 @@ class Client:
123150
max_messages: Optional[:class:`int`]
124151
The maximum number of messages to store in the internal message cache.
125152
This defaults to ``1000``. Passing in ``None`` disables the message cache.
153+
features: :class:`.ClientFeatures`
154+
The features that are enabled or disabled for the client.
126155
"""
127156

128157
def __init__(
129158
self,
130159
*,
131160
internal_server_id: Optional[str] = None,
132161
max_messages: Optional[int] = MISSING,
162+
features: Optional[ClientFeatures] = None,
133163
**options,
134164
):
135165
# internal
136166
self.loop: asyncio.AbstractEventLoop = _loop
137167
self.max_messages: int = 1000 if max_messages is MISSING else max_messages
138168
self._listeners = {}
139169

170+
self.features = features or ClientFeatures()
171+
# This option is deprecated
172+
if options.get('experimental_event_style') is not None:
173+
log.info("The `experimental_event_style` client parameter is deprecated, please switch to using ClientFeatures instead.")
174+
self.features.experimental_event_style = options.pop('experimental_event_style', False)
175+
140176
# state
141177
self._closed: bool = False
142178
self._ready: asyncio.Event = MISSING
@@ -145,7 +181,7 @@ def __init__(
145181
self.ws: Optional[GuildedWebSocket] = None
146182
self.http: HTTPClient = HTTPClient(
147183
max_messages=self.max_messages,
148-
experimental_event_style=options.pop('experimental_event_style', False),
184+
features=self.features,
149185
)
150186

151187
async def __aenter__(self) -> Self:

guilded/http.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
from .asset import Asset
8282
from .category import Category
8383
from .channel import DMChannel, Thread
84+
from .client import ClientFeatures
8485
from .emote import Emote
8586
from .file import Attachment, File
8687
from .gateway import GuildedWebSocket
@@ -220,10 +221,10 @@ def __init__(self, method: str, path: str, *, override_base: str = None):
220221

221222
class HTTPClientBase:
222223
GIL_ID = 'Ann6LewA'
223-
def __init__(self, *, max_messages: int = 1000, experimental_event_style: bool = False):
224+
def __init__(self, *, max_messages: int = 1000, features: Optional[ClientFeatures] = None):
224225
self.session: Optional[aiohttp.ClientSession] = None
225226
self._max_messages = max_messages
226-
self._experimental_event_style = experimental_event_style
227+
self._experimental_event_style = features.experimental_event_style if features else False
227228

228229
self.ws: Optional[GuildedWebSocket] = None
229230
self.user: Optional[ClientUser] = None
@@ -468,8 +469,9 @@ def get_stripped_reaction_list(self):
468469

469470

470471
class HTTPClient(HTTPClientBase):
471-
def __init__(self, *, max_messages=1000, experimental_event_style=False):
472-
super().__init__(max_messages=max_messages, experimental_event_style=experimental_event_style)
472+
def __init__(self, *, max_messages=1000, features=None):
473+
super().__init__(max_messages=max_messages, features=features)
474+
self.client_features = features
473475

474476
self.token: Optional[str] = None
475477

@@ -488,6 +490,12 @@ async def request(self, route, **kwargs):
488490
if self.token:
489491
headers['Authorization'] = f'Bearer {self.token}'
490492

493+
if self.client_features and self.client_features.official_markdown:
494+
# There doesn't really seem to be a standard for boolean values in headers,
495+
# and Guilded doesn't specify what the value should be - all values are
496+
# treated the same. A lowercase `true` seemed appropriate.
497+
headers['x-guilded-bot-api-use-official-markdown'] = "true"
498+
491499
if 'json' in kwargs:
492500
headers['Content-Type'] = 'application/json'
493501
kwargs['data'] = json.dumps(kwargs.pop('json'))
@@ -591,6 +599,9 @@ async def ws_connect(self) -> aiohttp.ClientWebSocketResponse:
591599
# We have connected before, resume and catch up with missed messages
592600
headers['guilded-last-message-id'] = self.ws._last_message_id
593601

602+
if self.client_features and self.client_features.official_markdown:
603+
headers['x-guilded-bot-api-use-official-markdown'] = "true"
604+
594605
log_headers = headers.copy()
595606
log_headers['Authorization'] = 'Bearer [removed]'
596607
log.debug('Connecting to the gateway with %s', log_headers)

0 commit comments

Comments
 (0)