added ability to delete entries. Added MAL RefreshToken Function

This commit is contained in:
2024-09-18 14:05:41 -04:00
parent 5cdf86a147
commit 00930f611e
14 changed files with 282 additions and 23 deletions

View File

@ -11,13 +11,14 @@ import (
"strings"
)
func MALHelper(method string, malUrl string, body url.Values) json.RawMessage {
func MALHelper(method string, malUrl string, body url.Values) (json.RawMessage, string) {
client := &http.Client{}
req, _ := http.NewRequest(method, malUrl, strings.NewReader(body.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Authorization", "Bearer "+myAnimeListJwt.AccessToken)
fmt.Println(myAnimeListJwt.AccessToken)
resp, err := client.Do(req)
@ -29,13 +30,18 @@ func MALHelper(method string, malUrl string, body url.Values) json.RawMessage {
Message: "Errored when sending request to the server" + err.Error(),
})
return message
return message, resp.Status
}
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
return respBody
if resp.Status == "401 Unauthorized" {
refreshMyAnimeListAuthorizationToken()
MALHelper(method, malUrl, body)
}
return respBody, resp.Status
}
func (a *App) GetMyAnimeList(count int) MALWatchlist {
@ -45,11 +51,13 @@ func (a *App) GetMyAnimeList(count int) MALWatchlist {
var malList MALWatchlist
respBody := MALHelper("GET", malUrl, nil)
respBody, resStatus := MALHelper("GET", malUrl, nil)
err := json.Unmarshal(respBody, &malList)
if err != nil {
log.Printf("Failed to unmarshal json response, %s\n", err)
if resStatus == "200 OK" {
err := json.Unmarshal(respBody, &malList)
if err != nil {
log.Printf("Failed to unmarshal json response, %s\n", err)
}
}
return malList
@ -71,23 +79,40 @@ func (a *App) MyAnimeListUpdate(anime MALAnime, update MALUploadStatus) MalListS
malUrl := "https://api.myanimelist.net/v2/anime/" + strconv.Itoa(anime.Id) + "/my_list_status"
var status MalListStatus
respBody := MALHelper("PATCH", malUrl, body)
err := json.Unmarshal(respBody, &status)
if err != nil {
log.Printf("Failed to unmarshal json response, %s\n", err)
respBody, respStatus := MALHelper("PATCH", malUrl, body)
if respStatus == "200 OK" {
err := json.Unmarshal(respBody, &status)
if err != nil {
log.Printf("Failed to unmarshal json response, %s\n", err)
}
}
return status
}
func (a *App) GetMyAnimeListAnime(id int) MALAnime {
fmt.Println(id)
malUrl := "https://api.myanimelist.net/v2/anime/" + strconv.Itoa(id) + "?fields=id,title,main_picture,alternative_titles,start_date,end_date,synopsis,mean,rank,popularity,num_list_users,num_scoring_users,nsfw,genres,created_at,updated_at,media_type,status,my_list_status,num_episodes,start_season,broadcast,source,average_episode_duration,rating,pictures,background,related_anime,recommendations,studios,statistics"
respBody := MALHelper("GET", malUrl, nil)
respBody, respStatus := MALHelper("GET", malUrl, nil)
var malAnime MALAnime
err := json.Unmarshal(respBody, &malAnime)
if err != nil {
log.Printf("Failed to unmarshal json response, %s\n", err)
if respStatus == "200 OK" {
err := json.Unmarshal(respBody, &malAnime)
if err != nil {
log.Printf("Failed to unmarshal json response, %s\n", err)
}
}
return malAnime
}
func (a *App) DeleteMyAnimeListEntry(id int) bool {
malUrl := "https://api.myanimelist.net/v2/anime/" + strconv.Itoa(id) + "/my_list_status"
_, respStatus := MALHelper("DELETE", malUrl, nil)
if respStatus == "200 OK" {
return true
} else {
return false
}
}