Skip to content

Commit

Permalink
add user-agent for integration
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalys committed Feb 8, 2024
1 parent ac034d5 commit 0a99d36
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
7 changes: 4 additions & 3 deletions integrations/myanimelist/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ func New(username string) *API {
return &API{
Username: username,
client: &http.Client{
Transport: roundtripper.NewRateLimitedTransport(
http.DefaultTransport, rate.NewLimiter(rate.Every(time.Second), 1),
),
Transport: roundtripper.NewUserAgentTransport(
roundtripper.NewRateLimitedTransport(
http.DefaultTransport, rate.NewLimiter(rate.Every(time.Second), 1),
), "github.com/sonalys/animeman"),
Timeout: 10 * time.Second,
},
}
Expand Down
4 changes: 2 additions & 2 deletions integrations/nyaa/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func New() *API {
return &API{
client: &http.Client{
Jar: http.DefaultClient.Jar,
Transport: roundtripper.NewRateLimitedTransport(
Transport: roundtripper.NewUserAgentTransport(roundtripper.NewRateLimitedTransport(
http.DefaultTransport, rate.NewLimiter(rate.Every(1*time.Second), 1),
),
), "github.com/sonalys/animeman"),
// Timeout: 10 * time.Second,
},
}
Expand Down
6 changes: 3 additions & 3 deletions internal/roundtripper/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ type throttledTransport struct {
ratelimiter *rate.Limiter
}

func (c *throttledTransport) RoundTrip(r *http.Request) (*http.Response, error) {
err := c.ratelimiter.Wait(r.Context()) // This is a blocking call. Honors the rate limit
func (t *throttledTransport) RoundTrip(r *http.Request) (*http.Response, error) {
err := t.ratelimiter.Wait(r.Context()) // This is a blocking call. Honors the rate limit
if err != nil {
return nil, err
}
return c.roundTripperWrap.RoundTrip(r)
return t.roundTripperWrap.RoundTrip(r)
}

// NewRateLimitedTransport wraps transportWrap with a rate limitter
Expand Down
24 changes: 24 additions & 0 deletions internal/roundtripper/useragent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package roundtripper

import (
"net/http"
)

// throttledTransport Rate Limited HTTP Client
type userAgentTransport struct {
roundTripperWrap http.RoundTripper
userAgent string
}

func (t *userAgentTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Add("User-Agent", t.userAgent)
return t.roundTripperWrap.RoundTrip(r)
}

// NewRateLimitedTransport wraps transportWrap with a rate limitter
func NewUserAgentTransport(wrap http.RoundTripper, userAgent string) http.RoundTripper {
return &userAgentTransport{
roundTripperWrap: wrap,
userAgent: userAgent,
}
}

0 comments on commit 0a99d36

Please sign in to comment.