downloader/access_token.go
2022-08-15 15:55:27 +02:00

40 lines
812 B
Go

package main
import (
"io"
"net/http"
"strings"
)
func GetAnilistAccessToken() (string, error) {
if strings.HasPrefix(AnilistAccessToken, "ey") {
return AnilistAccessToken, nil
}
if StoragePath == "" {
return "", ErrAnilistTokenNotObtainable.New()
} else if StorageUser == "" || StoragePass == "" {
return "", ErrInvalidStorageParams.New()
}
req, err := http.NewRequest("GET", StoragePath, nil)
if err != nil {
return "", ErrStorageRequestFailed.Wrap(err)
}
req.SetBasicAuth(StorageUser, StoragePass)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", ErrStorageRequestFailed.Wrap(err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return "", ErrStorageRequestFailed.Wrap(err)
}
return strings.TrimSpace(string(data)), nil
}