mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2025-12-09 20:09:30 +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:
|
||||
raise exceptions.Unauthorized()
|
||||
if response.status_code == 429:
|
||||
raise exceptions.RateLimited(response)
|
||||
raise exceptions.RateLimited(response.json(), response.headers)
|
||||
|
||||
try:
|
||||
return response.json()
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import json
|
||||
|
||||
|
||||
class HttpException(Exception):
|
||||
"""Base Exception class representing a HTTP exception."""
|
||||
|
||||
@@ -12,8 +9,6 @@ class RateLimited(HttpException):
|
||||
|
||||
Attributes
|
||||
----------
|
||||
response : requests.Response
|
||||
The actual response object received from Discord API.
|
||||
json : dict
|
||||
The actual JSON data received. Shorthand to ``response.json()``.
|
||||
message : str
|
||||
@@ -25,19 +20,13 @@ class RateLimited(HttpException):
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, response):
|
||||
self.response = response
|
||||
try:
|
||||
self.json = self.response.json()
|
||||
except json.JSONDecodeError:
|
||||
self.json = dict()
|
||||
self.message = self.response.text
|
||||
else:
|
||||
self.message = self.json["message"]
|
||||
self.is_global = self.json["global"]
|
||||
self.retry_after = self.json["retry_after"]
|
||||
finally:
|
||||
super().__init__(self.message)
|
||||
def __init__(self, json, headers):
|
||||
self.json = json
|
||||
self.headers = headers
|
||||
self.message = self.json["message"]
|
||||
self.is_global = self.json["global"]
|
||||
self.retry_after = self.json["retry_after"]
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
class Unauthorized(HttpException):
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
|
||||
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__)
|
||||
@@ -85,7 +85,7 @@ def me():
|
||||
|
||||
@app.route("/me/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])
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user