-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharm_api.go
More file actions
104 lines (88 loc) · 2.87 KB
/
arm_api.go
File metadata and controls
104 lines (88 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
const defaultARMBaseURL = "https://arm.haglund.dev"
// ARMClient is an HTTP client for the ARM API (https://arm.haglund.dev).
type ARMClient struct {
baseURL string
httpClient HTTPClient
}
// ARMResponse represents the response from /api/v2/ids.
type ARMResponse struct {
AniList *int `json:"anilist"`
MyAnimeList *int `json:"myanimelist"`
AniDB *int `json:"anidb"`
Kitsu *int `json:"kitsu"`
}
// NewARMClient creates a new ARM API client.
func NewARMClient(baseURL string, timeout time.Duration) *ARMClient {
if baseURL == "" {
baseURL = defaultARMBaseURL
}
return &ARMClient{
baseURL: baseURL,
httpClient: NewRetryableClient(&http.Client{
Timeout: timeout,
}, 3),
}
}
// GetAniListID returns the AniList ID for a given MAL ID.
func (c *ARMClient) GetAniListID(ctx context.Context, malID int) (int, bool, error) {
url := fmt.Sprintf("%s/api/v2/ids?source=myanimelist&id=%d&include=anilist", c.baseURL, malID)
LogDebug(ctx, "[ARM API] GET %s", url)
resp, err := c.doRequest(ctx, url)
if err != nil {
LogDebug(ctx, "[ARM API] Error: %v", err)
return 0, false, err
}
if resp == nil || resp.AniList == nil {
LogDebug(ctx, "[ARM API] Response: not found (404 or null)")
return 0, false, nil
}
LogDebug(ctx, "[ARM API] Response: AniList ID = %d", *resp.AniList)
return *resp.AniList, true, nil
}
// GetMALID returns the MAL ID for a given AniList ID.
func (c *ARMClient) GetMALID(ctx context.Context, anilistID int) (int, bool, error) {
url := fmt.Sprintf("%s/api/v2/ids?source=anilist&id=%d&include=myanimelist", c.baseURL, anilistID)
LogDebug(ctx, "[ARM API] GET %s", url)
resp, err := c.doRequest(ctx, url)
if err != nil {
LogDebug(ctx, "[ARM API] Error: %v", err)
return 0, false, err
}
if resp == nil || resp.MyAnimeList == nil {
LogDebug(ctx, "[ARM API] Response: not found (404 or null)")
return 0, false, nil
}
LogDebug(ctx, "[ARM API] Response: MAL ID = %d", *resp.MyAnimeList)
return *resp.MyAnimeList, true, nil
}
func (c *ARMClient) doRequest(ctx context.Context, url string) (*ARMResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
defer resp.Body.Close() //nolint:errcheck // best effort close
LogDebug(ctx, "[ARM API] Status: %d", resp.StatusCode)
if resp.StatusCode == http.StatusNotFound {
return nil, nil //nolint:nilnil // nil means "not found", not an error
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status: %s", resp.Status)
}
var armResp ARMResponse
if err := json.NewDecoder(resp.Body).Decode(&armResp); err != nil {
return nil, fmt.Errorf("decode response: %w", err)
}
return &armResp, nil
}