mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2025-12-10 04:19:31 +02:00
Add Exceptions
This commit is contained in:
12
docs/api.rst
12
docs/api.rst
@@ -38,3 +38,15 @@ Models
|
|||||||
.. autoclass:: flask_discord.models.UserConnection
|
.. autoclass:: flask_discord.models.UserConnection
|
||||||
:members:
|
:members:
|
||||||
:inherited-members:
|
:inherited-members:
|
||||||
|
|
||||||
|
|
||||||
|
Exceptions
|
||||||
|
----------
|
||||||
|
|
||||||
|
.. autoclass:: flask_discord.exceptions.HttpException
|
||||||
|
:members:
|
||||||
|
:inherited-members:
|
||||||
|
|
||||||
|
.. autoclass:: flask_discord.exceptions.Unauthorized
|
||||||
|
:members:
|
||||||
|
:inherited-members:
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import os
|
|||||||
import abc
|
import abc
|
||||||
|
|
||||||
from . import configs
|
from . import configs
|
||||||
|
from . import exceptions
|
||||||
|
|
||||||
from flask import session, jsonify
|
from flask import session, jsonify
|
||||||
from requests_oauthlib import OAuth2Session
|
from requests_oauthlib import OAuth2Session
|
||||||
@@ -87,8 +88,19 @@ class DiscordOAuth2HttpClient(abc.ABC):
|
|||||||
-------
|
-------
|
||||||
dict
|
dict
|
||||||
Dictionary containing received from sent HTTP GET request.
|
Dictionary containing received from sent HTTP GET request.
|
||||||
|
|
||||||
|
Raises
|
||||||
|
------
|
||||||
|
flask_discord.exceptions.Unauthorized
|
||||||
|
Raises :py:class:`flask_discord.exceptions.Unauthorized` if current user is not authorized.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return self._make_session().get(configs.API_BASE_URL + route).json()
|
response = self._make_session().get(configs.API_BASE_URL + route)
|
||||||
|
|
||||||
|
if response.status_code == 401:
|
||||||
|
raise exceptions.Unauthorized
|
||||||
|
|
||||||
|
return response.json()
|
||||||
|
|
||||||
def get_json(self):
|
def get_json(self):
|
||||||
discord_session = self._make_session(token=session.get("discord_oauth2_token"))
|
discord_session = self._make_session(token=session.get("discord_oauth2_token"))
|
||||||
|
|||||||
6
flask_discord/exceptions.py
Normal file
6
flask_discord/exceptions.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
class HttpException(Exception):
|
||||||
|
"""Base Exception class representing a HTTP exception."""
|
||||||
|
|
||||||
|
|
||||||
|
class Unauthorized(HttpException):
|
||||||
|
"""A HTTP Exception raised when user is not authorized."""
|
||||||
Reference in New Issue
Block a user