Skip to content

Commit

Permalink
Throw error if fetching anime list did not returned 200 status cdoe
Browse files Browse the repository at this point in the history
  • Loading branch information
aQaTL committed May 25, 2018
1 parent 26e5932 commit 2d4c751
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
15 changes: 10 additions & 5 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ func saveCredentials(credentials string) {
}

//Loads Client statistic data and returns Client's AnimeList
func loadData(c *mal.Client, ctx *cli.Context) mal.AnimeList {
func loadData(c *mal.Client, ctx *cli.Context) (mal.AnimeList, error) {
var list []*mal.Anime

if ctx.GlobalBool("refresh") || cacheNotExist() {
mal.DoFuncWithWaitAnimation("Fetching your list", func() {
list = c.AnimeList(mal.All)
})
{
var err error
mal.DoFuncWithWaitAnimation("Fetching your list", func() {
list, err = c.AnimeList(mal.All)
})
return nil, fmt.Errorf("error fetching your list\n%v", err)
}

cacheList(list)
cacheClient(c)

Expand All @@ -75,7 +80,7 @@ func loadData(c *mal.Client, ctx *cli.Context) mal.AnimeList {
list = loadCachedList()
loadCachedStats(c)
}
return list
return list, nil
}

func cacheNotExist() bool {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ func loadMAL(ctx *cli.Context) (*mal.Client, mal.AnimeList, error) {
return nil, nil, fmt.Errorf("error creating mal.Client")
}

list := loadData(c, ctx)
list, err := loadData(c, ctx)

return c, list, nil
return c, list, err
}

func defaultAction(ctx *cli.Context) error {
Expand Down
13 changes: 7 additions & 6 deletions mal/mal.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"net/http"
"net/url"
"strings"
"text/template"
"sync"
"text/template"
)

const (
Expand Down Expand Up @@ -89,18 +89,19 @@ func newRequest(url, credentials, method string) *http.Request {

//Note: Since anime list endpoint, besides anime list, returns account stats, this method also
//updates Client with them
func (c *Client) AnimeList(status MyStatus) []*Anime {
func (c *Client) AnimeList(status MyStatus) ([]*Anime, error) {
url := fmt.Sprintf(UserAnimeListEndpoint, c.Username, "all") //Anything other than `all` doesn't really work

req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Printf("Request error: %v", err)
return nil
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("List list getting error: %v", err)
return nil, err
} else if resp.StatusCode != 200 {
return nil, fmt.Errorf("server returned: %v", resp.Status)
}

decoder := xml.NewDecoder(resp.Body)
Expand All @@ -123,7 +124,7 @@ func (c *Client) AnimeList(status MyStatus) []*Anime {
}
}

return list
return list, nil
}

func (c *Client) Update(entry *Anime) bool {
Expand Down

0 comments on commit 2d4c751

Please sign in to comment.