Modify request method so that it can be used for standard requests as well

This commit is contained in:
thec0sm0s
2020-05-10 17:20:20 +05:30
parent 21e4988d59
commit fbf341a3ec

View File

@@ -1,3 +1,4 @@
import requests
import os import os
import abc import abc
@@ -73,7 +74,7 @@ class DiscordOAuth2HttpClient(abc.ABC):
auto_refresh_url=configs.DISCORD_TOKEN_URL, auto_refresh_url=configs.DISCORD_TOKEN_URL,
token_updater=self._token_updater) token_updater=self._token_updater)
def request(self, route: str, method="GET", data=None, **kwargs) -> dict: def request(self, route: str, method="GET", data=None, oauth=True, **kwargs) -> dict:
"""Sends HTTP request to provided route or discord endpoint. """Sends HTTP request to provided route or discord endpoint.
Note Note
@@ -88,6 +89,8 @@ class DiscordOAuth2HttpClient(abc.ABC):
Specify the HTTP method to use to perform this request. Specify the HTTP method to use to perform this request.
data : dict, optional data : dict, optional
The optional payload the include with the request. The optional payload the include with the request.
oauth : bool
A boolean determining if this should be Discord OAuth2 session request or any standard request.
Returns Returns
------- -------
@@ -100,7 +103,11 @@ class DiscordOAuth2HttpClient(abc.ABC):
Raises :py:class:`flask_discord.Unauthorized` if current user is not authorized. Raises :py:class:`flask_discord.Unauthorized` if current user is not authorized.
""" """
response = self._make_session().request(method, configs.DISCORD_API_BASE_URL + route, data, **kwargs) route = configs.DISCORD_API_BASE_URL + route
if oauth:
response = self._make_session().request(method, route, data, **kwargs)
else:
response = requests.request(method, route, data=data, **kwargs)
if response.status_code == 401: if response.status_code == 401:
raise exceptions.Unauthorized raise exceptions.Unauthorized