🤞 Convert DiscordOAuth2Session into its own package

This commit is contained in:
thec0sm0s
2020-08-29 13:44:05 +05:30
parent d23441a070
commit bfe5adcc2c
11 changed files with 55 additions and 48 deletions

View File

@@ -8,11 +8,11 @@ attributes and available methods.
Discord OAuth2 Client
---------------------
.. autoclass:: flask_discord.DiscordOAuth2Session
.. autoclass:: flask_discord.oauth2.DiscordOAuth2Session
:members:
:inherited-members:
.. autoclass:: flask_discord._http.DiscordOAuth2HttpClient
.. autoclass:: flask_discord.oauth2._http.DiscordOAuth2HttpClient
:members:
:inherited-members:
@@ -20,23 +20,23 @@ Discord OAuth2 Client
Models
------
.. autoclass:: flask_discord.models.Guild
.. autoclass:: flask_discord.oauth2.models.Guild
:members:
:inherited-members:
.. autoclass:: flask_discord.models.User
.. autoclass:: flask_discord.oauth2.models.User
:members:
:inherited-members:
.. autoclass:: flask_discord.models.Bot
.. autoclass:: flask_discord.oauth2.models.Bot
:members:
:inherited-members:
.. autoclass:: flask_discord.models.Integration
.. autoclass:: flask_discord.oauth2.models.Integration
:members:
:inherited-members:
.. autoclass:: flask_discord.models.UserConnection
.. autoclass:: flask_discord.oauth2.models.UserConnection
:members:
:inherited-members:

View File

@@ -1,7 +1,7 @@
from .exceptions import *
from .utils import *
from .oauth2.client import DiscordOAuth2Session
from .client import DiscordOAuth2Session
from .utils import *
from .exceptions import *
__all__ = [

View File

@@ -0,0 +1,2 @@
from .models import *
from .client import DiscordOAuth2Session

View File

@@ -5,8 +5,8 @@ import json
import os
import abc
from . import configs
from . import exceptions
from flask_discord import configs
from flask_discord import exceptions
from flask import session, request
from collections.abc import Mapping

View File

@@ -2,9 +2,10 @@ import jwt
import typing
import discord
from . import configs, _http, models, utils, exceptions
from . import _http, models
from oauthlib.common import add_params_to_uri
from flask_discord import configs, utils, exceptions
from flask import request, session, redirect, current_app
@@ -12,7 +13,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
"""Main client class representing hypothetical OAuth2 session with discord.
It uses Flask `session <http://flask.pocoo.org/docs/1.0/api/#flask.session>`_ local proxy object
to save state, authorization token and keeps record of users sessions across different requests.
This class inherits :py:class:`flask_discord._http.DiscordOAuth2HttpClient` class.
This class inherits :py:class:`flask_discord.oauth2._http.DiscordOAuth2HttpClient` class.
Parameters
----------
@@ -71,7 +72,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
<https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes>`_.
data : dict, optional
A mapping of your any custom data which you want to access after authorization grant. Use
`:py:meth:flask_discord.DiscordOAuth2Session.callback` to retrieve this data in your callback view.
`:py:meth:flask_discord.oauth2.DiscordOAuth2Session.callback` to retrieve this data in your callback view.
prompt : bool, optional
Determines if the OAuth2 grant should be explicitly prompted and re-approved. Defaults to True.
Specify False for implicit grant which will skip the authorization screen and redirect to redirect URI.
@@ -122,7 +123,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
Meaning by default, it uses client side session handling.
Override this method if you want to handle the user's session server side. If this method is overridden then,
you must also override :py:meth:`flask_discord.DiscordOAuth2Session.get_authorization_token`.
you must also override :py:meth:`flask_discord.oauth2.DiscordOAuth2Session.get_authorization_token`.
"""
session["DISCORD_OAUTH2_TOKEN"] = token
@@ -130,7 +131,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
@staticmethod
def get_authorization_token() -> dict:
"""A static method which returns a dict containing Discord OAuth2 token and other secrets which was saved
previously by `:py:meth:`flask_discord.DiscordOAuth2Session.save_authorization_token` from user's cookies.
by `:py:meth:`flask_discord.oauth2.DiscordOAuth2Session.save_authorization_token` from user's cookies.
You must override this method if you are implementing server side session handling.
@@ -183,7 +184,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
Returns
-------
flask_discord.models.User
flask_discord.oauth2.models.User
"""
return models.User.get_from_cache() or models.User.fetch_from_api()
@@ -196,7 +197,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
Returns
-------
list
List of :py:class:`flask_discord.models.UserConnection` objects.
List of :py:class:`flask_discord.oauth2.models.UserConnection` objects.
"""
user = models.User.get_from_cache()
@@ -216,7 +217,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
Returns
-------
list
List of :py:class:`flask_discord.models.Guild` objects.
List of :py:class:`flask_discord.oauth2.models.Guild` objects.
"""
user = models.User.get_from_cache()

View File

@@ -23,15 +23,15 @@ class DiscordModelsBase(metaclass=DiscordModelsMeta):
@staticmethod
def _request(*args, **kwargs):
"""A shorthand to :py:func:flask_discord.request`. It uses Flask current_app local proxy to get the
Flask-Discord client.
"""A shorthand to :py:func:flask_discord.oauth2.DiscordOAuth2Session.request`.
It uses Flask current_app local proxy to get the instance of Flask-Discord DiscordOAuth2Session.
"""
return current_app.discord.request(*args, **kwargs)
@staticmethod
def _bot_request(*args, **kwargs):
"""A shorthand to :py:func:flask_discord.bot_request`."""
"""A shorthand to :py:func:flask_discord.oauth2.DiscordOAuth2Session.bot_request`."""
return current_app.discord.bot_request(*args, **kwargs)
@classmethod

View File

@@ -59,19 +59,20 @@ class UserConnection(DiscordModelsBase):
@classmethod
def fetch_from_api(cls, cache=True):
"""A class method which returns an instance or list of instances of this model by implicitly making an
API call to Discord. If an instance of :py:class:`flask_discord.User` exists in the users internal cache
who are attached to these connections then, the cached property :py:attr:`flask_discord.User.connections`
is updated.
API call to Discord. If an instance of :py:class:`flask_discord.oauth2.models.User` exists in the
users internal cache who are attached to these connections then, the cached property
:py:attr:`flask_discord.oauth2.models.User.connections` is updated.
Parameters
----------
cache : bool
Determines if the :py:attr:`flask_discord.User.guilds` cache should be updated with the new guilds.
Determines if the :py:attr:`flask_discord.oauth2.models.User.guilds` cache should be
updated with the new guilds.
Returns
-------
list[flask_discord.UserConnection, ...]
List of instances of :py:class:`flask_discord.UserConnection` to which this user belongs.
list[flask_discord.oauth2.models.UserConnection, ...]
List of instances of :py:class:`flask_discord.oauth2.models.UserConnection` to which this user belongs.
"""
connections = super().fetch_from_api()

View File

@@ -1,8 +1,9 @@
from .base import DiscordModelsBase
from flask import current_app
import discord
from .. import configs
from flask import current_app
from flask_discord import configs
class Guild(DiscordModelsBase):
@@ -68,18 +69,20 @@ class Guild(DiscordModelsBase):
@classmethod
def fetch_from_api(cls, cache=True):
"""A class method which returns an instance or list of instances of this model by implicitly making an
API call to Discord. If an instance of :py:class:`flask_discord.User` exists in the users internal cache
who belongs to these guilds then, the cached property :py:attr:`flask_discord.User.guilds` is updated.
API call to Discord. If an instance of :py:class:`flask_discord.oauth2.models.Guild` exists in the
users internal cache who belongs to these guilds then, the cached property
:py:attr:`flask_discord.oauth2.models.User.guilds` is updated.
Parameters
----------
cache : bool
Determines if the :py:attr:`flask_discord.User.guilds` cache should be updated with the new guilds.
Determines if the :py:attr:`flask_discord.oauth2.models.User.guilds` cache should be updated with
the new guilds.
Returns
-------
list[flask_discord.Guild, ...]
List of instances of :py:class:`flask_discord.Guild` to which this user belongs.
list[flask_discord.oauth2.models.Guild, ...]
List of instances of :py:class:`flask_discord.oauth2.models.Guild` to which this user belongs.
"""
guilds = super().fetch_from_api()

View File

@@ -1,10 +1,9 @@
from .. import configs
from .guild import Guild
from .. import exceptions
from .base import DiscordModelsBase
from .guild import Guild
from .connections import UserConnection
from flask_discord import configs
from flask_discord import exceptions
from flask import current_app, session
@@ -74,8 +73,8 @@ class User(DiscordModelsBase):
@property
def guilds(self):
"""A cached mapping of user's guild ID to :py:class:`flask_discord.Guild`. The guilds are cached when the first
API call for guilds is requested so it might be an empty dict.
"""A cached mapping of user's guild ID to :py:class:`flask_discord.oauth2.models.Guild`.
The guilds are cached when the first API call for guilds is requested so it might be an empty dict.
"""
try:
@@ -133,10 +132,11 @@ class User(DiscordModelsBase):
----------
guilds : bool
A boolean indicating if user's guilds should be cached or not. Defaults to ``False``. If chose to not
cache, user's guilds can always be obtained from :py:func:`flask_discord.Guilds.fetch_from_api()`.
cache, user's guilds can be obtained from :py:func:`flask_discord.oauth2.models.Guilds.fetch_from_api()`.
connections : bool
A boolean indicating if user's connections should be cached or not. Defaults to ``False``. If chose to not
cache, user's connections can always be obtained from :py:func:`flask_discord.Connections.fetch_from_api()`.
A boolean indicating if user's connections should be cached or not. Defaults to ``False``.
If chose to notvcache, user's connections can be obtained from
:py:func:`flask_discord.oauth2.models.UserConnection.fetch_from_api()`.
Returns
-------
@@ -161,7 +161,7 @@ class User(DiscordModelsBase):
Returns
-------
flask_discord.User
flask_discord.oauth2.models.User
An user instance if it exists in internal cache.
None
If the current doesn't exists in internal cache.
@@ -201,7 +201,7 @@ class User(DiscordModelsBase):
Returns
-------
list
List of :py:class:`flask_discord.Guilds` instances.
List of :py:class:`flask_discord.oauth2.models.Guilds` instances.
"""
self._guilds = {guild.id: guild for guild in Guild.fetch_from_api(cache=False)}
@@ -214,7 +214,7 @@ class User(DiscordModelsBase):
Returns
-------
list
A list of :py:class:`flask_discord.UserConnection` instances.
A list of :py:class:`flask_discord.oauth2.models.UserConnection` instances.
"""
self.connections = UserConnection.fetch_from_api(cache=False)