From d1258c54d321b56e9bbb842654ddb150afaed792 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Tue, 30 Jul 2024 12:59:21 -0400 Subject: [PATCH] renamed anilist variables to allow for other services --- AniListFunctions.go | 4 ++-- AniListUserFunctions.go | 53 ++++++++++++++++++++--------------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/AniListFunctions.go b/AniListFunctions.go index 77eab83..94be911 100644 --- a/AniListFunctions.go +++ b/AniListFunctions.go @@ -15,8 +15,8 @@ func AniListQuery(body interface{}, login bool) (json.RawMessage, string) { if err != nil { log.Printf("Failed at response, %s\n", err) } - if login && (AniListJWT{}) != jwt { - response.Header.Add("Authorization", "Bearer "+jwt.AccessToken) + if login && (AniListJWT{}) != aniListJwt { + response.Header.Add("Authorization", "Bearer "+aniListJwt.AccessToken) } else if login { return nil, "Please login to anilist to make this request" } diff --git a/AniListUserFunctions.go b/AniListUserFunctions.go index e484d6f..c41a775 100644 --- a/AniListUserFunctions.go +++ b/AniListUserFunctions.go @@ -4,8 +4,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/99designs/keyring" - "github.com/wailsapp/wails/v2/pkg/runtime" "io" "log" "net/http" @@ -14,22 +12,25 @@ import ( "strconv" "strings" "sync" + + "github.com/99designs/keyring" + "github.com/wailsapp/wails/v2/pkg/runtime" ) -var jwt AniListJWT +var aniListJwt AniListJWT -var ring, _ = keyring.Open(keyring.Config{ +var aniRing, _ = keyring.Open(keyring.Config{ ServiceName: "AniTrack", }) -var ctxShutdown, cancel = context.WithCancel(context.Background()) +var aniCtxShutdown, aniCancel = context.WithCancel(context.Background()) func (a *App) AniListLogin() { - if (AniListJWT{}) == jwt { - tokenType, err := ring.Get("anilistTokenType") - expiresIn, err := ring.Get("anilistTokenExpiresIn") - accessToken, err := ring.Get("anilistAccessToken") - refreshToken, err := ring.Get("anilistRefreshToken") + if (AniListJWT{}) == aniListJwt { + tokenType, err := aniRing.Get("anilistTokenType") + expiresIn, err := aniRing.Get("anilistTokenExpiresIn") + accessToken, err := aniRing.Get("anilistAccessToken") + refreshToken, err := aniRing.Get("anilistRefreshToken") if err != nil { getAniListCodeUrl := "https://anilist.co/api/v2/oauth/authorize?client_id=" + os.Getenv("ANILIST_APP_ID") + "&redirect_uri=" + os.Getenv("ANILIST_CALLBACK_URI") + "&response_type=code" runtime.BrowserOpenURL(a.ctx, getAniListCodeUrl) @@ -39,11 +40,11 @@ func (a *App) AniListLogin() { handleAniListCallback(serverDone) serverDone.Wait() } else { - jwt.TokenType = string(tokenType.Data) - jwt.AccessToken = string(accessToken.Data) - jwt.RefreshToken = string(refreshToken.Data) + aniListJwt.TokenType = string(tokenType.Data) + aniListJwt.AccessToken = string(accessToken.Data) + aniListJwt.RefreshToken = string(refreshToken.Data) expiresInString := string(expiresIn.Data) - jwt.ExpiresIn, _ = strconv.Atoi(expiresInString) + aniListJwt.ExpiresIn, _ = strconv.Atoi(expiresInString) } } } @@ -52,7 +53,7 @@ func handleAniListCallback(wg *sync.WaitGroup) { srv := &http.Server{Addr: ":6734"} http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) { select { - case <-ctxShutdown.Done(): + case <-aniCtxShutdown.Done(): fmt.Println("Shutting down...") return default: @@ -60,25 +61,25 @@ func handleAniListCallback(wg *sync.WaitGroup) { content := r.FormValue("code") if content != "" { - jwt = getAniListAuthorizationToken(content) - _ = ring.Set(keyring.Item{ + aniListJwt = getAniListAuthorizationToken(content) + _ = aniRing.Set(keyring.Item{ Key: "anilistTokenType", - Data: []byte(jwt.TokenType), + Data: []byte(aniListJwt.TokenType), }) - _ = ring.Set(keyring.Item{ + _ = aniRing.Set(keyring.Item{ Key: "anilistTokenExpiresIn", - Data: []byte(string(jwt.ExpiresIn)), + Data: []byte(string(aniListJwt.ExpiresIn)), }) - _ = ring.Set(keyring.Item{ + _ = aniRing.Set(keyring.Item{ Key: "anilistAccessToken", - Data: []byte(jwt.AccessToken), + Data: []byte(aniListJwt.AccessToken), }) - _ = ring.Set(keyring.Item{ + _ = aniRing.Set(keyring.Item{ Key: "anilistRefreshToken", - Data: []byte(jwt.RefreshToken), + Data: []byte(aniListJwt.RefreshToken), }) fmt.Println("Shutting down...") - cancel() + aniCancel() err := srv.Shutdown(context.Background()) if err != nil { log.Println("server.Shutdown:", err) @@ -89,7 +90,6 @@ func handleAniListCallback(wg *sync.WaitGroup) { return } } - }) go func() { @@ -166,5 +166,4 @@ func (a *App) GetAniListLoggedInUserId() AniListUser { } return post - }