From d2ef2658070a10dfb846531bb7e7934dcdc6e655 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Tue, 30 Jul 2024 20:38:55 -0400 Subject: [PATCH] renamed and created anilist and simkl user functions renamed anilist userid to user function. Created simkl user functions --- AniListUserFunctions.go | 25 +++++++++++++-- SimklUserFunctions.go | 52 +++++++++++++++++++++---------- frontend/wailsjs/go/main/App.d.ts | 10 ++++-- 3 files changed, 64 insertions(+), 23 deletions(-) diff --git a/AniListUserFunctions.go b/AniListUserFunctions.go index 9665033..092ff94 100644 --- a/AniListUserFunctions.go +++ b/AniListUserFunctions.go @@ -27,14 +27,27 @@ var aniCtxShutdown, aniCancel = context.WithCancel(context.Background()) func (a *App) CheckIfAniListLoggedIn() bool { if (AniListJWT{} == aniListJwt) { - return false + tokenType, err := aniRing.Get("anilistTokenType") + expiresIn, err := aniRing.Get("anilistTokenExpiresIn") + accessToken, err := aniRing.Get("anilistAccessToken") + refreshToken, err := aniRing.Get("anilistRefreshToken") + if err != nil { + return false + } else { + aniListJwt.TokenType = string(tokenType.Data) + aniListJwt.AccessToken = string(accessToken.Data) + aniListJwt.RefreshToken = string(refreshToken.Data) + expiresInString := string(expiresIn.Data) + aniListJwt.ExpiresIn, _ = strconv.Atoi(expiresInString) + return true + } } else { return true } } func (a *App) AniListLogin() { - if a.CheckIfSimklLoggedIn() == false { + if (AniListJWT{} == aniListJwt) { tokenType, err := aniRing.Get("anilistTokenType") expiresIn, err := aniRing.Get("anilistTokenExpiresIn") accessToken, err := aniRing.Get("anilistAccessToken") @@ -150,7 +163,7 @@ func getAniListAuthorizationToken(content string) AniListJWT { return post } -func (a *App) GetAniListLoggedInUserId() AniListUser { +func (a *App) GetAniListLoggedInUser() AniListUser { a.AniListLogin() body := struct { Query string `json:"query"` @@ -160,6 +173,12 @@ func (a *App) GetAniListLoggedInUserId() AniListUser { Viewer { id name + avatar { + large + medium + } + bannerImage + siteUrl } } `, diff --git a/SimklUserFunctions.go b/SimklUserFunctions.go index a446876..126f642 100644 --- a/SimklUserFunctions.go +++ b/SimklUserFunctions.go @@ -25,7 +25,17 @@ var simklCtxShutdown, simklCancel = context.WithCancel(context.Background()) func (a *App) CheckIfSimklLoggedIn() bool { if (SimklJWT{} == simklJwt) { - return false + tokenType, err := simklRing.Get("SimklTokenType") + accessToken, err := simklRing.Get("SimklAccessToken") + scope, err := simklRing.Get("SimklScope") + if err != nil { + return false + } else { + simklJwt.TokenType = string(tokenType.Data) + simklJwt.AccessToken = string(accessToken.Data) + simklJwt.Scope = string(scope.Data) + return true + } } else { return true } @@ -144,28 +154,36 @@ func getSimklAuthorizationToken(content string) SimklJWT { return post } -func (a *App) GetSimklLoggedInUserId() SimklUser { +func (a *App) GetSimklLoggedInUser() SimklUser { a.SimklLogin() - body := struct { - Query string `json:"query"` - }{ - Query: ` - query { - Viewer { - id - name - } - } - `, + + fmt.Println("Token: ", simklJwt.AccessToken) + + client := &http.Client{} + + req, _ := http.NewRequest("POST", "https://api.simkl.com/users/settings", nil) + + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Authorization", "Bearer "+simklJwt.AccessToken) + req.Header.Add("simkl-api-key", os.Getenv("SIMKL_CLIENT_ID")) + + response, err := client.Do(req) + + if err != nil { + log.Printf("Failed at request, %s\n", err) + return SimklUser{} } - user, _ := SimklQuery(body, true) + defer response.Body.Close() - var post SimklUser - err := json.Unmarshal(user, &post) + var user SimklUser + + respBody, _ := io.ReadAll(response.Body) + + err = json.Unmarshal(respBody, &user) if err != nil { log.Printf("Failed at unmarshal, %s\n", err) } - return post + return user } diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index 8c3abfa..4c322b3 100755 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -8,14 +8,18 @@ export function AniListSearch(arg1:string):Promise; export function AniListUpdateEntry(arg1:number,arg2:string,arg3:string,arg4:number,arg5:number,arg6:string,arg7:number,arg8:number,arg9:number,arg10:number,arg11:number,arg12:number):Promise; +export function CheckIfAniListLoggedIn():Promise; + +export function CheckIfSimklLoggedIn():Promise; + export function GetAniListItem(arg1:number,arg2:boolean):Promise; -export function GetAniListLoggedInUserId():Promise; +export function GetAniListLoggedInUser():Promise; export function GetAniListUserWatchingList(arg1:number,arg2:number,arg3:string):Promise; -export function GetSimklLoggedInUserId():Promise; +export function GetSimklLoggedInUser():Promise; -export function Greet(arg1:string):Promise; +export function SimklGetUserWatchlist():Promise; export function SimklLogin():Promise;