-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathitchio.go
72 lines (61 loc) · 1.82 KB
/
itchio.go
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
package itchio
import (
"net/http"
"time"
"github.com/itchio/httpkit/timeout"
"golang.org/x/time/rate"
)
// OnRateLimited is the callback type for rate limiting events
type OnRateLimited func(req *http.Request, res *http.Response)
// OnRateLimited is the callback type for rate limiting events
type OnOutgoingRequest func(req *http.Request)
// A Client allows consuming the itch.io API
type Client struct {
Key string
HTTPClient *http.Client
BaseURL string
RetryPatterns []time.Duration
UserAgent string
AcceptedLanguage string
Limiter *rate.Limiter
onRateLimited OnRateLimited
onOutgoingRequest OnOutgoingRequest
}
func defaultRetryPatterns() []time.Duration {
return []time.Duration{
1 * time.Second,
2 * time.Second,
4 * time.Second,
8 * time.Second,
16 * time.Second,
}
}
// ClientWithKey creates a new itch.io API client with a given API key
func ClientWithKey(key string) *Client {
c := &Client{
Key: key,
HTTPClient: timeout.NewDefaultClient(),
RetryPatterns: defaultRetryPatterns(),
UserAgent: "go-itchio",
AcceptedLanguage: "*",
Limiter: DefaultRateLimiter(),
}
c.SetServer("https://api.itch.io")
return c
}
// OnRateLimited allows registering a function that gets called
// every time the server responds with 503
func (c *Client) OnRateLimited(cb OnRateLimited) {
c.onRateLimited = cb
}
// OnOutgoingRequest allows registering a function that gets called
// every time the client makes an HTTP request
func (c *Client) OnOutgoingRequest(cb OnOutgoingRequest) {
c.onOutgoingRequest = cb
}
// SetServer allows changing the server to which we're making API
// requests (which defaults to the reference itch.io server)
func (c *Client) SetServer(itchioServer string) *Client {
c.BaseURL = itchioServer
return c
}