fix: enforce global rate limit in jikan client and handle relation errors
This commit is contained in:
@@ -39,60 +39,71 @@ func findFirstAnimeRelation(res JikanRelationsResponse, relType string) *int {
|
||||
}
|
||||
|
||||
// fetchChain recursively builds the relational chain (Prequels or Sequels)
|
||||
func (c *Client) fetchChain(startID int, direction string, visited map[int]bool) []RelationEntry {
|
||||
func (c *Client) fetchChain(startID int, direction string, visited map[int]bool) ([]RelationEntry, error) {
|
||||
rels, err := c.GetRelationsData(startID)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nextIDPtr := findFirstAnimeRelation(rels, direction)
|
||||
if nextIDPtr == nil {
|
||||
return nil
|
||||
return nil, nil // normal end of chain
|
||||
}
|
||||
|
||||
nextID := *nextIDPtr
|
||||
if visited[nextID] { // prevent loops
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
visited[nextID] = true
|
||||
|
||||
anime, err := c.GetAnimeByID(nextID)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entry := RelationEntry{Anime: anime, IsCurrent: false}
|
||||
rest := c.fetchChain(nextID, direction, visited)
|
||||
rest, err := c.fetchChain(nextID, direction, visited)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if direction == "Prequel" {
|
||||
return append(rest, entry)
|
||||
return append(rest, entry), nil
|
||||
}
|
||||
return append([]RelationEntry{entry}, rest...)
|
||||
return append([]RelationEntry{entry}, rest...), nil
|
||||
}
|
||||
|
||||
// GetFullRelations resolves the full Prequel/Sequel chronological chain synchronously
|
||||
func (c *Client) GetFullRelations(id int) []RelationEntry {
|
||||
func (c *Client) GetFullRelations(id int) ([]RelationEntry, error) {
|
||||
currentAnime, err := c.GetAnimeByID(id)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
visited := map[int]bool{id: true}
|
||||
|
||||
prequels := c.fetchChain(id, "Prequel", visited)
|
||||
prequels, err1 := c.fetchChain(id, "Prequel", visited)
|
||||
|
||||
// Clone visited set for sequels so we don't block valid paths if there's weird branching
|
||||
visitedSeq := make(map[int]bool)
|
||||
for k, v := range visited {
|
||||
visitedSeq[k] = v
|
||||
}
|
||||
|
||||
sequels := c.fetchChain(id, "Sequel", visitedSeq)
|
||||
sequels, err2 := c.fetchChain(id, "Sequel", visitedSeq)
|
||||
|
||||
// If both chains errored and it wasn't just "no relations", we should probably error out
|
||||
// But it's safer to just return what we have and the error so the UI can decide
|
||||
var result []RelationEntry
|
||||
result = append(result, prequels...)
|
||||
result = append(result, RelationEntry{Anime: currentAnime, IsCurrent: true})
|
||||
result = append(result, sequels...)
|
||||
|
||||
return result
|
||||
var finalErr error
|
||||
if err1 != nil {
|
||||
finalErr = err1
|
||||
} else if err2 != nil {
|
||||
finalErr = err2
|
||||
}
|
||||
|
||||
return result, finalErr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user