Module xmodel_rest.auth

Expand source code
from requests.models import PreparedRequest

from xinject import Dependency
from requests.auth import AuthBase as Requests_AuthBase
from requests import PreparedRequest
from .settings import RestSettings

__pdoc__ = {
    "RestAuth.__call__": True
}


class RestAuth(Dependency, Requests_AuthBase):
    """ Abstract type from which all-other api-auth-context's are descended from.
        For an example of one used for our Xyngular API's (that can also be used directly with
        the Requests 3rd party library), see: `xyn_sdk.core.common.Auth`.
    """

    def requests_callable(self, settings: RestSettings) -> Requests_AuthBase:
        """ Right now returns self by default, since by default we will use the current/default
            settings (see `RestAuth.__call__`).

            This is an opportunity to map/return a custom or shared
            `requests.auth.AuthBase` resource customized for the settings that are passed in.

            .. todo:: Put some common logic in here to map passed in settings object
                We want to use a standard set of things we return here to map the passed
                in settings to a callable that the `requests` library can use to inject
                credentials into it's request.

                For now we just return self and expect the current settings to be used,
                which should be good enough for now.
         """
        return self

    def refresh_token(self, settings: RestSettings = None):
        """ Forces the token/credentials to be refreshed, can use if the token is about to expire.

            When Requests calls to get new token, the expiration should be checked and refreshed
            if needed, which the result of you can pass back [ie: block].

            Args:
                settings (xynlib.orm.base.settings.Settings): Will pass in the settings that
                    need the token refresh.

                    If None (default): The subclass will retrieve the current default settings
                        and use them (the Auth subclass should know what base-settings it needs).
        """
        pass

    def __call__(self, request: PreparedRequest):
        """ Called from requests library to modify request as needed to provide auth.
            Modify the request as needed and return it. Whatever is returned is what is executed.

            `BaseAuth` by default just simply returns the request unmodified.

            If you need Settings, get the default one via, normally you do this by calling
            `xynlib.context.Resource.resource` on the specific
            `xynlib.orm.base.settings.BaseSettings` subclass that you normally use.
        Args:
            request (requests.PreparedRequest): Is the `requests.PreparedRequest` of the request
                that needs the authorization added.
        Returns:
            requests.PreparedRequest: The request object you passed in, modified as needed.
        """
        return request

Classes

class RestAuth

Abstract type from which all-other api-auth-context's are descended from. For an example of one used for our Xyngular API's (that can also be used directly with the Requests 3rd party library), see: xyn_sdk.core.common.Auth.

Expand source code
class RestAuth(Dependency, Requests_AuthBase):
    """ Abstract type from which all-other api-auth-context's are descended from.
        For an example of one used for our Xyngular API's (that can also be used directly with
        the Requests 3rd party library), see: `xyn_sdk.core.common.Auth`.
    """

    def requests_callable(self, settings: RestSettings) -> Requests_AuthBase:
        """ Right now returns self by default, since by default we will use the current/default
            settings (see `RestAuth.__call__`).

            This is an opportunity to map/return a custom or shared
            `requests.auth.AuthBase` resource customized for the settings that are passed in.

            .. todo:: Put some common logic in here to map passed in settings object
                We want to use a standard set of things we return here to map the passed
                in settings to a callable that the `requests` library can use to inject
                credentials into it's request.

                For now we just return self and expect the current settings to be used,
                which should be good enough for now.
         """
        return self

    def refresh_token(self, settings: RestSettings = None):
        """ Forces the token/credentials to be refreshed, can use if the token is about to expire.

            When Requests calls to get new token, the expiration should be checked and refreshed
            if needed, which the result of you can pass back [ie: block].

            Args:
                settings (xynlib.orm.base.settings.Settings): Will pass in the settings that
                    need the token refresh.

                    If None (default): The subclass will retrieve the current default settings
                        and use them (the Auth subclass should know what base-settings it needs).
        """
        pass

    def __call__(self, request: PreparedRequest):
        """ Called from requests library to modify request as needed to provide auth.
            Modify the request as needed and return it. Whatever is returned is what is executed.

            `BaseAuth` by default just simply returns the request unmodified.

            If you need Settings, get the default one via, normally you do this by calling
            `xynlib.context.Resource.resource` on the specific
            `xynlib.orm.base.settings.BaseSettings` subclass that you normally use.
        Args:
            request (requests.PreparedRequest): Is the `requests.PreparedRequest` of the request
                that needs the authorization added.
        Returns:
            requests.PreparedRequest: The request object you passed in, modified as needed.
        """
        return request

Ancestors

Class variables

var objDependency

Inherited from: Dependency.obj

class property/attribute that will return the current dependency for the subclass it's asked on by calling Dependency.grab, passing no extra …

Static methods

def __init_subclass__(thread_sharable=Default, attributes_to_skip_while_copying: Optional[Iterable[str]] = Default, **kwargs)

Inherited from: Dependency.__init_subclass__

Args

thread_sharable
If False: While a dependency is lazily auto-created, we will ensure we do it per-thread, and not make it visible …
def grab() ‑> ~T

Inherited from: Dependency.grab

Gets a potentially shared dependency from the current udpend.context.XContext

def proxy() ‑> ~R

Inherited from: Dependency.proxy

Returns a proxy-object, that when and attribute is asked for, it will proxy it to the current object of cls

def proxy_attribute(attribute_name: str) ‑> Any

Inherited from: Dependency.proxy_attribute

Returns a proxy-object, that when and attribute is asked for, it will proxy it to the current attribute value on the current object of cls

Methods

def __call__(self, request: requests.models.PreparedRequest)

Called from requests library to modify request as needed to provide auth. Modify the request as needed and return it. Whatever is returned is what is executed.

<code>BaseAuth</code> by default just simply returns the request unmodified.

If you need Settings, get the default one via, normally you do this by calling
<code>xynlib.context.Resource.resource</code> on the specific
<code>xynlib.orm.base.settings.BaseSettings</code> subclass that you normally use.

Args

request : requests.PreparedRequest
Is the requests.PreparedRequest of the request that needs the authorization added.

Returns

requests.PreparedRequest
The request object you passed in, modified as needed.
Expand source code
def __call__(self, request: PreparedRequest):
    """ Called from requests library to modify request as needed to provide auth.
        Modify the request as needed and return it. Whatever is returned is what is executed.

        `BaseAuth` by default just simply returns the request unmodified.

        If you need Settings, get the default one via, normally you do this by calling
        `xynlib.context.Resource.resource` on the specific
        `xynlib.orm.base.settings.BaseSettings` subclass that you normally use.
    Args:
        request (requests.PreparedRequest): Is the `requests.PreparedRequest` of the request
            that needs the authorization added.
    Returns:
        requests.PreparedRequest: The request object you passed in, modified as needed.
    """
    return request
def __copy__(self)

Inherited from: Dependency.__copy__

Basic shallow copy protection (I am wondering if I should just remove this default copy code) …

def refresh_token(self, settings: RestSettings = None)

Forces the token/credentials to be refreshed, can use if the token is about to expire.

When Requests calls to get new token, the expiration should be checked and refreshed if needed, which the result of you can pass back [ie: block].

Args

settings : xynlib.orm.base.settings.Settings

Will pass in the settings that need the token refresh.

If None (default): The subclass will retrieve the current default settings and use them (the Auth subclass should know what base-settings it needs).

Expand source code
def refresh_token(self, settings: RestSettings = None):
    """ Forces the token/credentials to be refreshed, can use if the token is about to expire.

        When Requests calls to get new token, the expiration should be checked and refreshed
        if needed, which the result of you can pass back [ie: block].

        Args:
            settings (xynlib.orm.base.settings.Settings): Will pass in the settings that
                need the token refresh.

                If None (default): The subclass will retrieve the current default settings
                    and use them (the Auth subclass should know what base-settings it needs).
    """
    pass
def requests_callable(self, settings: RestSettings) ‑> requests.auth.AuthBase

Right now returns self by default, since by default we will use the current/default settings (see RestAuth.__call__()).

This is an opportunity to map/return a custom or shared requests.auth.AuthBase resource customized for the settings that are passed in.

TODO

Put some common logic in here to map passed in settings object We want to use a standard set of things we return here to map the passed in settings to a callable that the requests library can use to inject credentials into it's request.

For now we just return self and expect the current settings to be used, which should be good enough for now.

Expand source code
def requests_callable(self, settings: RestSettings) -> Requests_AuthBase:
    """ Right now returns self by default, since by default we will use the current/default
        settings (see `RestAuth.__call__`).

        This is an opportunity to map/return a custom or shared
        `requests.auth.AuthBase` resource customized for the settings that are passed in.

        .. todo:: Put some common logic in here to map passed in settings object
            We want to use a standard set of things we return here to map the passed
            in settings to a callable that the `requests` library can use to inject
            credentials into it's request.

            For now we just return self and expect the current settings to be used,
            which should be good enough for now.
     """
    return self