package superset

import (
	"fmt"

	"github.com/fiskerinc/cloud-services/pkg/redis"
	"github.com/pkg/errors"
)

var errTokenNotFound = errors.New("token isn't found")

func getCachedAccToken(c redis.Client) (string, error) {
	res, err := c.Get(redis.SupersetAccTokenKey)
	if err != nil {
		return "", err
	}

	if res == nil {
		return "", errors.WithStack(errTokenNotFound)
	}

	tokenBytes, ok := res.([]byte)
	if !ok {
		return "", errors.WithStack(fmt.Errorf("invalid superset access token type (expected []byte]); access token: %v", res))
	}

	return string(tokenBytes), nil
}
