mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2025-12-10 04:19:31 +02:00
Use and implement generalized request method rather than just get.
This commit is contained in:
@@ -72,8 +72,8 @@ 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 get(self, route: str) -> dict:
|
def request(self, route: str, method="GET", data=None, **kwargs) -> dict:
|
||||||
"""Sends HTTP GET request to provided route or discord endpoint.
|
"""Sends HTTP request to provided route or discord endpoint.
|
||||||
|
|
||||||
Note
|
Note
|
||||||
----
|
----
|
||||||
@@ -83,6 +83,10 @@ class DiscordOAuth2HttpClient(abc.ABC):
|
|||||||
----------
|
----------
|
||||||
route : str
|
route : str
|
||||||
Route or endpoint URL to send HTTP GET request to. Example: ``/users/@me``
|
Route or endpoint URL to send HTTP GET request to. Example: ``/users/@me``
|
||||||
|
method : str, optional
|
||||||
|
Specify the HTTP method to use to perform this request.
|
||||||
|
data : dict, optional
|
||||||
|
The optional payload the include with the request.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
@@ -95,7 +99,7 @@ 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().get(configs.DISCORD_API_BASE_URL + route)
|
response = self._make_session().request(method, configs.DISCORD_API_BASE_URL + route, data, **kwargs)
|
||||||
|
|
||||||
if response.status_code == 401:
|
if response.status_code == 401:
|
||||||
raise exceptions.Unauthorized
|
raise exceptions.Unauthorized
|
||||||
@@ -103,8 +107,7 @@ class DiscordOAuth2HttpClient(abc.ABC):
|
|||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def get_json(self):
|
def get_json(self):
|
||||||
discord_session = self._make_session(token=session.get("DISCORD_OAUTH2_TOKEN"))
|
user = self.request('/users/@me')
|
||||||
user = discord_session.get(configs.DISCORD_API_BASE_URL + '/users/@me').json()
|
guilds = self.request('/users/@me/guilds')
|
||||||
guilds = discord_session.get(configs.DISCORD_API_BASE_URL + '/users/@me/guilds').json()
|
connections = self.request('/users/@me/connections')
|
||||||
connections = discord_session.get(configs.DISCORD_API_BASE_URL + '/users/@me/connections').json()
|
|
||||||
return jsonify(user=user, guilds=guilds, connections=connections)
|
return jsonify(user=user, guilds=guilds, connections=connections)
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
|||||||
flask_discord.models.User
|
flask_discord.models.User
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return models.User(self.get("/users/@me"))
|
return models.User(self.request("/users/@me"))
|
||||||
|
|
||||||
def fetch_connections(self) -> list:
|
def fetch_connections(self) -> list:
|
||||||
"""Requests and returns connections of current user from discord.
|
"""Requests and returns connections of current user from discord.
|
||||||
@@ -91,7 +91,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
|||||||
List of :py:class:`flask_discord.models.UserConnection` objects.
|
List of :py:class:`flask_discord.models.UserConnection` objects.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
connections_payload = self.get("/users/@me/connections")
|
connections_payload = self.request("/users/@me/connections")
|
||||||
return [models.UserConnection(payload) for payload in connections_payload]
|
return [models.UserConnection(payload) for payload in connections_payload]
|
||||||
|
|
||||||
def fetch_guilds(self) -> list:
|
def fetch_guilds(self) -> list:
|
||||||
@@ -103,5 +103,5 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
|||||||
List of :py:class:`flask_discord.models.Guild` objects.
|
List of :py:class:`flask_discord.models.Guild` objects.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
guilds_payload = self.get("/users/@me/guilds")
|
guilds_payload = self.request("/users/@me/guilds")
|
||||||
return [models.Guild(payload) for payload in guilds_payload]
|
return [models.Guild(payload) for payload in guilds_payload]
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ discord = DiscordOAuth2Session(app)
|
|||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
return discord.create_session()
|
return discord.create_session(["guilds", "connections"])
|
||||||
|
|
||||||
|
|
||||||
@app.route("/callback/")
|
@app.route("/callback/")
|
||||||
@@ -26,6 +26,11 @@ def callback():
|
|||||||
return redirect(url_for(".me"))
|
return redirect(url_for(".me"))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/get_json/")
|
||||||
|
def get_json():
|
||||||
|
return discord.get_json()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/me/")
|
@app.route("/me/")
|
||||||
def me():
|
def me():
|
||||||
user = discord.fetch_user()
|
user = discord.fetch_user()
|
||||||
@@ -36,6 +41,7 @@ def me():
|
|||||||
</head>
|
</head>
|
||||||
<body><img src='{user.avatar_url}' />
|
<body><img src='{user.avatar_url}' />
|
||||||
<a href={url_for("my_connections")}>Connections</a>
|
<a href={url_for("my_connections")}>Connections</a>
|
||||||
|
<a href={url_for("get_json")}>Get default JSON.</a>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user