Exemple #1
0
def get_session(
    access_token_cache=None,
    credentials_path=None,
    profile_name=None,
    domain=None,
    protocol=None,
    client_id=None,
    client_secret=None,
):
    key = (
        access_token_cache,
        credentials_path,
        profile_name,
        domain,
        protocol,
        client_id,
        client_secret,
    )
    try:
        session = _SESSION_CACHE[key]
    except KeyError:
        profile = faculty.config.resolve_profile(
            credentials_path=credentials_path,
            profile_name=profile_name,
            domain=domain,
            protocol=protocol,
            client_id=client_id,
            client_secret=client_secret,
        )
        access_token_cache = access_token_cache or AccessTokenMemoryCache()
        session = Session(profile, access_token_cache)
        _SESSION_CACHE[key] = session
    return session
Exemple #2
0
def get_session(access_token_cache=None, **kwargs):
    key = tuple(kwargs.items()) + (access_token_cache, )
    try:
        session = _SESSION_CACHE[key]
    except KeyError:
        profile = faculty.config.resolve_profile(**kwargs)
        access_token_cache = access_token_cache or AccessTokenMemoryCache()
        session = Session(profile, access_token_cache)
        _SESSION_CACHE[key] = session
    return session
def test_access_token_memory_cache_expired(mock_datetime_now):
    cache = AccessTokenMemoryCache()
    cache.add(PROFILE, EXPIRED_ACCESS_TOKEN)
    assert cache.get(PROFILE) is None
def test_access_token_memory_cache_miss(mocker, mock_datetime_now):
    cache = AccessTokenMemoryCache()
    cache.add(PROFILE, VALID_ACCESS_TOKEN)
    assert cache.get(mocker.Mock()) is None
def test_access_token_memory_cache(mock_datetime_now):
    cache = AccessTokenMemoryCache()
    cache.add(PROFILE, VALID_ACCESS_TOKEN)
    assert cache.get(PROFILE) == VALID_ACCESS_TOKEN
Exemple #6
0
def get_session(
    access_token_cache=None,
    credentials_path=None,
    profile_name=None,
    domain=None,
    protocol=None,
    client_id=None,
    client_secret=None,
):
    """Get a Faculty session for a given configuration.

    Sessions returned by this function are cached. If called multiple times
    with the same arguments, the same session instance will be returned.

    Configuration settings are resolved as described in
    :func:`faculty.config.resolve_profile`.

    Parameters
    ----------
    access_token_cache : faculty.config.accesstoken.AccessTokenMemoryCache or \
            faculty.config.accesstoken.AccessTokenFileSystemCache
        A cache for keeping access tokens in this session. The default is a
        :class:`faculty.config.accesstoken.AccessTokenMemoryCache`.
    credentials_path : str or pathlib.Path, optional
        The path of the credentials file to load.
    profile_name : str, optional
        The name of the profile to load from the credentials file.
    domain : str, optional
        The domain name where Faculty services are hosted.
    protocol : str, optional
        The protocol to use when making requests to Faculty services.
    client_id : str, optional
        The OAuth client ID to authenticate requests with.
    client_secret : str, optional
        The OAuth client secret to authenticate requests with.

    Returns
    -------
    Session
        The resulting Faculty session.
    """
    key = (
        access_token_cache,
        credentials_path,
        profile_name,
        domain,
        protocol,
        client_id,
        client_secret,
    )
    try:
        session = _SESSION_CACHE[key]
    except KeyError:
        profile = faculty.config.resolve_profile(
            credentials_path=credentials_path,
            profile_name=profile_name,
            domain=domain,
            protocol=protocol,
            client_id=client_id,
            client_secret=client_secret,
        )
        access_token_cache = access_token_cache or AccessTokenMemoryCache()
        session = Session(profile, access_token_cache)
        _SESSION_CACHE[key] = session
    return session