mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2025-12-10 04:19:31 +02:00
Minor refactoring - Separate exception from the parsing logic. Expect Discord API to always return JSON payload with all keys on Ratelimit
This commit is contained in:
@@ -173,7 +173,7 @@ class DiscordOAuth2HttpClient(abc.ABC):
|
|||||||
if response.status_code == 401:
|
if response.status_code == 401:
|
||||||
raise exceptions.Unauthorized()
|
raise exceptions.Unauthorized()
|
||||||
if response.status_code == 429:
|
if response.status_code == 429:
|
||||||
raise exceptions.RateLimited(response)
|
raise exceptions.RateLimited(response.json(), response.headers)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
class HttpException(Exception):
|
class HttpException(Exception):
|
||||||
"""Base Exception class representing a HTTP exception."""
|
"""Base Exception class representing a HTTP exception."""
|
||||||
|
|
||||||
@@ -12,8 +9,6 @@ class RateLimited(HttpException):
|
|||||||
|
|
||||||
Attributes
|
Attributes
|
||||||
----------
|
----------
|
||||||
response : requests.Response
|
|
||||||
The actual response object received from Discord API.
|
|
||||||
json : dict
|
json : dict
|
||||||
The actual JSON data received. Shorthand to ``response.json()``.
|
The actual JSON data received. Shorthand to ``response.json()``.
|
||||||
message : str
|
message : str
|
||||||
@@ -25,19 +20,13 @@ class RateLimited(HttpException):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, response):
|
def __init__(self, json, headers):
|
||||||
self.response = response
|
self.json = json
|
||||||
try:
|
self.headers = headers
|
||||||
self.json = self.response.json()
|
self.message = self.json["message"]
|
||||||
except json.JSONDecodeError:
|
self.is_global = self.json["global"]
|
||||||
self.json = dict()
|
self.retry_after = self.json["retry_after"]
|
||||||
self.message = self.response.text
|
super().__init__(self.message)
|
||||||
else:
|
|
||||||
self.message = self.json["message"]
|
|
||||||
self.is_global = self.json["global"]
|
|
||||||
self.retry_after = self.json["retry_after"]
|
|
||||||
finally:
|
|
||||||
super().__init__(self.message)
|
|
||||||
|
|
||||||
|
|
||||||
class Unauthorized(HttpException):
|
class Unauthorized(HttpException):
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from flask import Flask, redirect, url_for
|
from flask import Flask, redirect, url_for
|
||||||
from flask_discord import DiscordOAuth2Session, requires_authorization
|
from flask_discord import DiscordOAuth2Session, requires_authorization, models
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -85,7 +85,7 @@ def me():
|
|||||||
|
|
||||||
@app.route("/me/guilds/")
|
@app.route("/me/guilds/")
|
||||||
def user_guilds():
|
def user_guilds():
|
||||||
guilds = discord.fetch_guilds()
|
guilds = models.Guild.fetch_from_api(cache=False)
|
||||||
return "<br />".join([f"[ADMIN] {g.name}" if g.permissions.administrator else g.name for g in guilds])
|
return "<br />".join([f"[ADMIN] {g.name}" if g.permissions.administrator else g.name for g in guilds])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user