initial commit
This commit is contained in:
39
access_token.go
Normal file
39
access_token.go
Normal file
@ -0,0 +1,39 @@
|
||||
package logic
|
||||
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user