Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 1054872

Browse files
authored
♻️ Added query params parameter to HTTP get requests (#343)
* ♻️ Added query params parameter to http get requests * ♻️ moved `remove_none()` to the http class * 💬 Using `Dict` instead `dict` typehint * ♻️ Added `remove_none()` for headers too
1 parent 3486bdc commit 1054872

File tree

6 files changed

+117
-72
lines changed

6 files changed

+117
-72
lines changed

pincer/core/http.py

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
ForbiddenError, MethodNotAllowedError, RateLimitError, ServerError,
2020
HTTPError
2121
)
22+
from ..utils.conversion import remove_none
2223

2324
if TYPE_CHECKING:
2425
from typing import Any, Dict, Optional, Union
@@ -114,28 +115,40 @@ async def __send(
114115
content_type: str = "application/json",
115116
data: Optional[Union[Dict, str, Payload]] = None,
116117
headers: Optional[Dict[str, Any]] = None,
118+
params: Optional[Dict] = None,
117119
__ttl: int = None
118120
) -> Optional[Dict]:
119121
"""
120122
Send an api request to the Discord REST API.
121123
122-
method:
124+
Parameters
125+
----------
126+
127+
method: :class:`aiohttp.ClientSession.request`
123128
The method for the request. (e.g. GET or POST)
124129
125-
endpoint:
130+
endpoint: :class:`str`
126131
The endpoint to which the request will be sent.
127132
128-
Keyword Arguments:
129-
130-
content_type:
133+
content_type: :class:`str`
131134
The request's content type.
132135
133-
data:
136+
data: Optional[Union[:class:`Dict`, :class:`str`, :class:`aiohttp.payload.Payload`]]
134137
The data which will be added to the request.
138+
|default| :data:`None`
139+
140+
headers: Optional[:class:`Dict`]
141+
The request headers.
142+
|default| :data:`None`
143+
144+
params: Optional[:class:`Dict`]
145+
The query parameters to add to the request.
146+
|default| :data:`None`
135147
136-
__ttl:
148+
__ttl: Optional[:class:`int`]
137149
Private param used for recursively setting the retry amount.
138150
(Eg set to 1 for 1 max retry)
151+
|default| :data:`None`
139152
"""
140153
ttl = __ttl or self.max_ttl
141154

@@ -166,8 +179,9 @@ async def __send(
166179
data=data,
167180
headers={
168181
"Content-Type": content_type,
169-
**(headers or {})
170-
}
182+
**(remove_none(headers) or {})
183+
},
184+
params=remove_none(params)
171185
) as res:
172186
return await self.__handle_response(
173187
res, method, endpoint, content_type, data, ttl
@@ -188,22 +202,25 @@ async def __handle_response(
188202
Side effects:
189203
If a 5xx error code is returned it will retry the request.
190204
191-
res:
205+
Parameters
206+
----------
207+
208+
res: :class:`aiohttp.ClientResponse`
192209
The response from the discord API.
193210
194-
method:
211+
method: :class:`aiohttp.ClientSession.request`
195212
The method which was used to call the endpoint.
196213
197-
endpoint:
214+
endpoint: :class:`str`
198215
The endpoint to which the request was sent.
199216
200-
content_type:
217+
content_type: :class:`str`
201218
The request's content type.
202219
203-
data:
220+
data: Optional[:class:`str`]
204221
The data which was added to the request.
205222
206-
__ttl:
223+
__ttl: :class:`int`
207224
Private param used for recursively setting the retry amount.
208225
(Eg set to 1 for 1 max retry)
209226
"""
@@ -292,7 +309,7 @@ async def delete(
292309
293310
Returns
294311
-------
295-
Optional[:class:`dict`]
312+
Optional[:class:`Dict`]
296313
The response from discord.
297314
"""
298315
return await self.__send(
@@ -301,7 +318,11 @@ async def delete(
301318
headers=headers
302319
)
303320

304-
async def get(self, route: str) -> Optional[Dict]:
321+
async def get(
322+
self,
323+
route: str,
324+
params: Optional[Dict] = None
325+
) -> Optional[Dict]:
305326
"""|coro|
306327
307328
Sends a get request to a Discord REST endpoint.
@@ -310,13 +331,20 @@ async def get(self, route: str) -> Optional[Dict]:
310331
----------
311332
route : :class:`str`
312333
The Discord REST endpoint to send a get request to.
334+
params: Optional[:class:`Dict`]
335+
The query parameters to add to the request.
336+
|default| :data:`None`
313337
314338
Returns
315339
-------
316-
Optional[:class:`dict`]
340+
Optional[:class:`Dict`]
317341
The response from discord.
318342
"""
319-
return await self.__send(self.__session.get, route)
343+
return await self.__send(
344+
self.__session.get,
345+
route,
346+
params=params
347+
)
320348

321349
async def head(self, route: str) -> Optional[Dict]:
322350
"""|coro|
@@ -330,7 +358,7 @@ async def head(self, route: str) -> Optional[Dict]:
330358
331359
Returns
332360
-------
333-
Optional[:class:`dict`]
361+
Optional[:class:`Dict`]
334362
The response from discord.
335363
"""
336364
return await self.__send(self.__session.head, route)
@@ -347,7 +375,7 @@ async def options(self, route: str) -> Optional[Dict]:
347375
348376
Returns
349377
-------
350-
Optional[:class:`dict`]
378+
Optional[:class:`Dict`]
351379
The response from discord.
352380
"""
353381
return await self.__send(self.__session.options, route)
@@ -367,7 +395,7 @@ async def patch(
367395
----------
368396
route : :class:`str`
369397
The Discord REST endpoint to send a patch request to.
370-
data : :class:`dict`
398+
data : :class:`Dict`
371399
The update data for the patch request.
372400
content_type: :class:`str`
373401
Body content type.
@@ -377,7 +405,7 @@ async def patch(
377405
378406
Returns
379407
-------
380-
Optional[:class:`dict`]
408+
Optional[:class:`Dict`]
381409
JSON response from the discord API.
382410
"""
383411
return await self.__send(
@@ -412,7 +440,7 @@ async def post(
412440
413441
Returns
414442
-------
415-
Optional[:class:`dict`]
443+
Optional[:class:`Dict`]
416444
JSON response from the discord API.
417445
"""
418446
return await self.__send(
@@ -447,7 +475,7 @@ async def put(
447475
448476
Returns
449477
-------
450-
Optional[:class:`dict`]
478+
Optional[:class:`Dict`]
451479
JSON response from the discord API.
452480
"""
453481
return await self.__send(

pincer/objects/guild/channel.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ async def edit_permissions(
303303
"""
304304
await self._http.put(
305305
f"channels/{self.id}/permissions/{overwrite.id}",
306-
headers=remove_none({"X-Audit-Log-Reason": reason}),
306+
headers={"X-Audit-Log-Reason": reason},
307307
data={"allow": allow, "deny": deny, "type": type},
308308
)
309309

@@ -323,7 +323,7 @@ async def delete_permission(
323323
"""
324324
await self._http.delete(
325325
f"channels/{self.id}/permissions/{overwrite.id}",
326-
headers=remove_none({"X-Audit-Log-Reason": reason}),
326+
headers={"X-Audit-Log-Reason": reason},
327327
)
328328

329329
async def follow_news_channel(
@@ -389,7 +389,7 @@ async def pin_message(
389389
"""
390390
await self._http.put(
391391
f"channels/{self.id}/pins/{message.id}",
392-
headers=remove_none({"X-Audit-Log-Reason": reason}),
392+
headers={"X-Audit-Log-Reason": reason},
393393
)
394394

395395
async def unpin_message(
@@ -400,7 +400,7 @@ async def unpin_message(
400400
"""
401401
await self._http.delete(
402402
f"channels/{self.id}/pins/{message.id}",
403-
headers=remove_none({"X-Audit-Log-Reason": reason}),
403+
headers={"X-Audit-Log-Reason": reason},
404404
)
405405

406406
async def group_dm_add_recipient(
@@ -459,7 +459,7 @@ async def bulk_delete_messages(
459459
"""
460460
await self._http.post(
461461
f"channels/{self.id}/messages/bulk_delete",
462-
headers=remove_none({"X-Audit-Log-Reason": reason}),
462+
headers={"X-Audit-Log-Reason": reason},
463463
data={"messages": messages},
464464
)
465465

@@ -484,7 +484,7 @@ async def delete(
484484

485485
await self._http.delete(
486486
f"channels/{channel_id}",
487-
headers=remove_none({"X-Audit-Log-Reason": reason}),
487+
headers={"X-Audit-Log-Reason": reason},
488488
)
489489

490490
async def __post_send_handler(self, message: UserMessage):
@@ -621,7 +621,7 @@ async def create_invite(
621621
self._client,
622622
await self._http.post(
623623
f"channels/{self.id}/invites",
624-
headers=remove_none({"X-Audit-Log-Reason": reason}),
624+
headers={"X-Audit-Log-Reason": reason},
625625
data={
626626
"max_age": max_age,
627627
"max_uses": max_uses,
@@ -681,9 +681,12 @@ async def list_public_archived_threads(
681681
construct_client_dict(
682682
self._client,
683683
await self._http.get(
684-
f"channels/{self.id}/threads/archived/public?"
685-
+ urlencode(remove_none({"before": before, "limit": limit}))
686-
),
684+
f"channels/{self.id}/threads/archived/public",
685+
params={
686+
"before": before,
687+
"limit": limit
688+
}
689+
)
687690
)
688691
)
689692

@@ -714,9 +717,12 @@ async def list_private_archived_threads(
714717
construct_client_dict(
715718
self._client,
716719
await self._http.get(
717-
f"channels/{self.id}/threads/archived/private?"
718-
+ urlencode(remove_none({"before": before, "limit": limit}))
719-
),
720+
f"channels/{self.id}/threads/archived/private",
721+
params={
722+
"before": before,
723+
"limit": limit
724+
}
725+
)
720726
)
721727
)
722728

@@ -748,8 +754,11 @@ async def list_joined_private_archived_threads(
748754
construct_client_dict(
749755
self._client,
750756
self._http.get(
751-
f"channels/{self.id}/users/@me/threads/archived/private?"
752-
+ urlencode(remove_none({"before": before, "limit": limit}))
757+
f"channels/{self.id}/users/@me/threads/archived/private",
758+
params={
759+
"before": before,
760+
"limit": limit
761+
}
753762
),
754763
)
755764
)
@@ -946,7 +955,7 @@ async def start(
946955
self._client,
947956
await self._http.post(
948957
f"channels/{self.id}/threads",
949-
headers=remove_none({"X-Audit-Log-Reason": reason}),
958+
headers={"X-Audit-Log-Reason": reason},
950959
data={
951960
"name": name,
952961
"auto_archive_duration": auto_archive_duration,
@@ -1009,7 +1018,7 @@ async def start_with_message(
10091018
self._client,
10101019
await self._http.post(
10111020
f"channels/{self.id}/messages/{message.id}/threads",
1012-
headers=remove_none({"X-Audit-Log-Reason": reason}),
1021+
headers={"X-Audit-Log-Reason": reason},
10131022
data={
10141023
"name": name,
10151024
"auto_archive_duration": auto_archive_duration,

0 commit comments

Comments
 (0)