Ejemplo n.º 1
0
async def _throttled_update_user(user, sem):
    async with sem:  # semaphore limits num of simultaneous updated
        try:
            await _update_user(user)
        except (httpx.HTTPError, sqlalchemy.exc.SQLAlchemyError) as err:
            LOGGER.error("Fatal error in update loop for user %s: %s", user.id,
                         err)
Ejemplo n.º 2
0
async def get_or_create_from_session(
        session: Dict[str, Any]) -> Tuple[Optional[User], bool]:
    """
    Get or create a user based on auth info in their session
    """
    created = False
    now = datetime.now(timezone.utc)
    try:
        full_session = FullSession(**session)
    except ValidationError:
        return (None, created)

    try:
        user = await User.objects.get(
            slackId=full_session.slack_id,
            spotifyId=full_session.spotify_id,
        )
    except orm.exceptions.MultipleMatches:
        LOGGER.error(
            "Multiple users returned for slack_id=%s, spotify_id=%s!",
            full_session.slack_id,
            full_session.spotify_id,
        )
        user = None
    except orm.exceptions.NoMatch:
        created = True
        user = await User.objects.create(
            slackId=full_session.slack_id,
            slackAccessToken=full_session.slack_access_token,
            spotifyId=full_session.spotify_id,
            spotifyExpiresAt=full_session.spotify_expires_at,
            spotifyAccessToken=full_session.spotify_access_token,
            spotifyRefreshToken=full_session.spotify_refresh_token,
            createdAt=now,
            updatedAt=now,
        )
    else:
        await user.update(
            slackAccessToken=full_session.slack_access_token,
            spotifyExpiresAt=full_session.spotify_expires_at,
            spotifyAccessToken=full_session.spotify_access_token,
            spotifyRefreshToken=full_session.spotify_refresh_token,
            updatedAt=now,
        )

    return (user, created)