# Path: gepify/services/spotify/models.py
# def get_access_token_from_refresh_token(refresh_token):
#     """Request access and refresh tokens from refresh token
# 
#     Parameters
#     ----------
#     refresh_token : str
#         The refresh token from previous authentication.
# 
#     Raises
#     ------
#     RuntimeError
#         If spotify API gives an error.
#     """
#     payload = {
#         'refresh_token': refresh_token,
#         'grant_type': 'refresh_token'
#     }
# 
#     return _get_token(payload)
# 
# def save_token_data_in_session(access_token_data):
#     """Saves token data in session
# 
#     Parameters
#     ----------
#     access_token_data : dict
#         The token data from get_access_token_from_code
#         or get_access_token_from_refresh_token
#     """
#     access_token = access_token_data['access_token']
#     refresh_token = access_token_data['refresh_token']
#     expires_at = int(time.time()) + int(access_token_data['expires_in'])
# 
#     session['spotify_access_token'] = access_token
#     if refresh_token:
#         session['spotify_refresh_token'] = refresh_token
#     session['spotify_expires_at'] = expires_at


from flask import session, redirect, url_for, g, render_template, current_app
from .models import (
    get_access_token_from_refresh_token, save_token_data_in_session
)
from functools import wraps
import time
import spotipy


def login_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        access_token = session.get('spotify_access_token', None)
        refresh_token = session.get('spotify_refresh_token', None)
        expires_at = session.get('spotify_expires_at', None)

        if access_token is None or refresh_token is None or expires_at is None:
            return redirect(url_for('spotify.login'))

        now = int(time.time())
        if now >= expires_at:

# Path: gepify/services/spotify/models.py
# def get_access_token_from_refresh_token(refresh_token):
#     """Request access and refresh tokens from refresh token
# 
#     Parameters
#     ----------
#     refresh_token : str
#         The refresh token from previous authentication.
# 
#     Raises
#     ------
#     RuntimeError
#         If spotify API gives an error.
#     """
#     payload = {
#         'refresh_token': refresh_token,
#         'grant_type': 'refresh_token'
#     }
# 
#     return _get_token(payload)
# 
# def save_token_data_in_session(access_token_data):
#     """Saves token data in session
# 
#     Parameters
#     ----------
#     access_token_data : dict
#         The token data from get_access_token_from_code
#         or get_access_token_from_refresh_token
#     """
#     access_token = access_token_data['access_token']
#     refresh_token = access_token_data['refresh_token']
#     expires_at = int(time.time()) + int(access_token_data['expires_in'])
# 
#     session['spotify_access_token'] = access_token
#     if refresh_token:
#         session['spotify_refresh_token'] = refresh_token
#     session['spotify_expires_at'] = expires_at


# Path: gepify/services/spotify/view_decorators.py
from flask import session, redirect, url_for, g, render_template, current_app
from .models import (
    get_access_token_from_refresh_token, save_token_data_in_session
)
from functools import wraps
import time
import spotipy



def login_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        access_token = session.get('spotify_access_token', None)
        refresh_token = session.get('spotify_refresh_token', None)
        expires_at = session.get('spotify_expires_at', None)

        if access_token is None or refresh_token is None or expires_at is None:
            return redirect(url_for('spotify.login'))

        now = int(time.time())
        if now >= expires_at:

            try: