cleaned up errors in go code
This commit is contained in:
@ -50,7 +50,7 @@ func base64URLEncode(str []byte) string {
|
||||
|
||||
func verifier() (*CodeVerifier, error) {
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
b := make([]byte, length, length)
|
||||
b := make([]byte, length)
|
||||
for i := 0; i < length; i++ {
|
||||
b[i] = byte(r.Intn(255))
|
||||
}
|
||||
@ -70,17 +70,19 @@ func (v *CodeVerifier) CodeChallengeS256() string {
|
||||
}
|
||||
|
||||
func (a *App) CheckIfMyAnimeListLoggedIn() bool {
|
||||
fmt.Println("check function reached")
|
||||
if (MyAnimeListJWT{} == myAnimeListJwt) {
|
||||
tokenType, err := myAnimeListRing.Get("MyAnimeListTokenType")
|
||||
expiresIn, err := myAnimeListRing.Get("MyAnimeListExpiresIn")
|
||||
accessToken, err := myAnimeListRing.Get("MyAnimeListAccessToken")
|
||||
refreshToken, err := myAnimeListRing.Get("MyAnimeListRefreshToken")
|
||||
if err != nil || len(accessToken.Data) == 0 {
|
||||
tokenType, tokenErr := myAnimeListRing.Get("MyAnimeListTokenType")
|
||||
expiresIn, expiresInErr := myAnimeListRing.Get("MyAnimeListExpiresIn")
|
||||
refreshToken, refreshTokenErr := myAnimeListRing.Get("MyAnimeListAccessToken")
|
||||
accessToken, accessTokenErr := myAnimeListRing.Get("MyAnimeListRefreshToken")
|
||||
if (tokenErr != nil || expiresInErr != nil || refreshTokenErr != nil || accessTokenErr != nil) || len(accessToken.Data) == 0 {
|
||||
return false
|
||||
} else {
|
||||
var expresInConvertErr error
|
||||
myAnimeListJwt.TokenType = string(tokenType.Data)
|
||||
myAnimeListJwt.ExpiresIn, err = strconv.Atoi(string(expiresIn.Data))
|
||||
if err != nil {
|
||||
myAnimeListJwt.ExpiresIn, expresInConvertErr = strconv.Atoi(string(expiresIn.Data))
|
||||
if expresInConvertErr != nil {
|
||||
fmt.Println("unable to convert string to int")
|
||||
}
|
||||
myAnimeListJwt.AccessToken = string(accessToken.Data)
|
||||
@ -93,12 +95,14 @@ func (a *App) CheckIfMyAnimeListLoggedIn() bool {
|
||||
}
|
||||
|
||||
func (a *App) MyAnimeListLogin() {
|
||||
if a.CheckIfMyAnimeListLoggedIn() == false {
|
||||
tokenType, err := myAnimeListRing.Get("MyAnimeListTokenType")
|
||||
expiresIn, err := myAnimeListRing.Get("MyAnimeListExpiresIn")
|
||||
accessToken, err := myAnimeListRing.Get("MyAnimeListAccessToken")
|
||||
refreshToken, err := myAnimeListRing.Get("MyAnimeListRefreshToken")
|
||||
if err != nil || len(accessToken.Data) == 0 {
|
||||
fmt.Println("login function reached")
|
||||
if !a.CheckIfMyAnimeListLoggedIn() {
|
||||
fmt.Println("check logged in function failed")
|
||||
tokenType, tokenErr := myAnimeListRing.Get("MyAnimeListTokenType")
|
||||
expiresIn, expiresInErr := myAnimeListRing.Get("MyAnimeListExpiresIn")
|
||||
refreshToken, refreshTokenErr := myAnimeListRing.Get("MyAnimeListAccessToken")
|
||||
accessToken, accessTokenErr := myAnimeListRing.Get("MyAnimeListRefreshToken")
|
||||
if (tokenErr != nil || expiresInErr != nil || refreshTokenErr != nil || accessTokenErr != nil) || len(accessToken.Data) == 0 {
|
||||
verifier, _ := verifier()
|
||||
getMyAnimeListCodeUrl := "https://myanimelist.net/v1/oauth2/authorize?response_type=code&client_id=" + Environment.MAL_CLIENT_ID + "&redirect_uri=" + Environment.MAL_CALLBACK_URI + "&code_challenge=" + verifier.Value + "&code_challenge_method=plain"
|
||||
runtime.BrowserOpenURL(*wailsContext, getMyAnimeListCodeUrl)
|
||||
@ -107,9 +111,10 @@ func (a *App) MyAnimeListLogin() {
|
||||
a.handleMyAnimeListCallback(serverDone, verifier)
|
||||
serverDone.Wait()
|
||||
} else {
|
||||
var expresInConvertErr error
|
||||
myAnimeListJwt.TokenType = string(tokenType.Data)
|
||||
myAnimeListJwt.ExpiresIn, err = strconv.Atoi(string(expiresIn.Data))
|
||||
if err != nil {
|
||||
myAnimeListJwt.ExpiresIn, expresInConvertErr = strconv.Atoi(string(expiresIn.Data))
|
||||
if expresInConvertErr != nil {
|
||||
fmt.Println("unable to convert string to int in Login function")
|
||||
}
|
||||
myAnimeListJwt.AccessToken = string(accessToken.Data)
|
||||
@ -218,6 +223,9 @@ func getMyAnimeListAuthorizationToken(content string, verifier *CodeVerifier) My
|
||||
defer res.Body.Close()
|
||||
|
||||
returnedBody, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Printf("Could not read returned body, %s\n", err)
|
||||
}
|
||||
|
||||
var post MyAnimeListJWT
|
||||
err = json.Unmarshal(returnedBody, &post)
|
||||
@ -265,6 +273,9 @@ func refreshMyAnimeListAuthorizationToken() {
|
||||
defer res.Body.Close()
|
||||
|
||||
returnedBody, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Printf("Could not read returned body, %s\n", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(returnedBody, &myAnimeListJwt)
|
||||
if err != nil {
|
||||
@ -291,12 +302,9 @@ func refreshMyAnimeListAuthorizationToken() {
|
||||
Title: "MyAnimeList Authorization",
|
||||
Message: "It is now safe to close your browser tab",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (a *App) GetMyAnimeListLoggedInUser() MyAnimeListUser {
|
||||
@ -311,7 +319,6 @@ func (a *App) GetMyAnimeListLoggedInUser() MyAnimeListUser {
|
||||
req.Header.Add("myAnimeList-api-key", Environment.MAL_CLIENT_ID)
|
||||
|
||||
response, err := client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed at request, %s\n", err)
|
||||
return MyAnimeListUser{}
|
||||
@ -333,13 +340,12 @@ func (a *App) GetMyAnimeListLoggedInUser() MyAnimeListUser {
|
||||
|
||||
func (a *App) LogoutMyAnimeList() string {
|
||||
if (MyAnimeListJWT{} != myAnimeListJwt) {
|
||||
err := myAnimeListRing.Remove("MyAnimeListTokenType")
|
||||
err = myAnimeListRing.Remove("MyAnimeListExpiresIn")
|
||||
err = myAnimeListRing.Remove("MyAnimeListAccessToken")
|
||||
err = myAnimeListRing.Remove("MyAnimeListRefreshToken")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("MAL Logout Failed", err)
|
||||
typeErr := myAnimeListRing.Remove("MyAnimeListTokenType")
|
||||
expiresInErr := myAnimeListRing.Remove("MyAnimeListExpiresIn")
|
||||
accessTokenErr := myAnimeListRing.Remove("MyAnimeListAccessToken")
|
||||
refreshTokenErr := myAnimeListRing.Remove("MyAnimeListRefreshToken")
|
||||
if typeErr != nil || expiresInErr != nil || accessTokenErr != nil || refreshTokenErr != nil {
|
||||
fmt.Println("MAL Logout Failed")
|
||||
}
|
||||
myAnimeListJwt = MyAnimeListJWT{}
|
||||
}
|
||||
|
Reference in New Issue
Block a user