From dd879d75470f4f6f061363772b8ffa2f9e14d539 Mon Sep 17 00:00:00 2001 From: gtosh4 Date: Fri, 30 Jul 2021 13:04:43 -0400 Subject: [PATCH 1/2] Add Get/SetHTTPClient to allow overriding This is useful for adding timeouts, ratelimiting, or caching. --- v2/blizzard.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/v2/blizzard.go b/v2/blizzard.go index b9fbec5..ba0dc81 100644 --- a/v2/blizzard.go +++ b/v2/blizzard.go @@ -184,6 +184,16 @@ func (c *Client) GetStaticClassicNamespace() string { return c.staticClassicNamespace } +// GetHTTPClient returns the http.Client used for making requests +func (c *Client) GetHTTPClient() *http.Client { + return c.httpClient +} + +// SetHTTPClient sets the http.Client used for making requests +func (c *Client) SetHTTPClient(h *http.Client) { + c.httpClient = h +} + // buildSearchParams builds params for searches func buildSearchParams(opts ...wowsearch.Opt) string { if len(opts) == 0 { From 62ac8364401914f2418fc45882fcb3cfa7b7ee3a Mon Sep 17 00:00:00 2001 From: gtosh4 Date: Fri, 30 Jul 2021 13:11:57 -0400 Subject: [PATCH 2/2] Add example and improve docs for setting HTTP client --- examples/v2/timeout/main.go | 51 +++++++++++++++++++++++++++++++++++++ v2/blizzard.go | 5 +++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 examples/v2/timeout/main.go diff --git a/examples/v2/timeout/main.go b/examples/v2/timeout/main.go new file mode 100644 index 0000000..e8637b7 --- /dev/null +++ b/examples/v2/timeout/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "context" + "fmt" + "log" + "net/http" + "os" + "time" + + "github.com/FuzzyStatic/blizzard/v2" +) + +var ( + clientID string + clientSecret string + blizz *blizzard.Client +) + +func init() { + clientID = os.Getenv("CLIENT_ID") + if clientID == "" { + log.Fatal("Set the environment variable CLIENT_ID before retrying.") + } + + clientSecret = os.Getenv("CLIENT_SECRET") + if clientSecret == "" { + log.Fatal("Set the environment variable CLIENT_SECRET before retrying.") + } +} + +func main() { + blizz = blizzard.NewClient( + clientID, + clientSecret, + blizzard.US, + blizzard.EnUS, + ) + + blizz.SetHTTPClient(clientWithTimeout(blizz.GetHTTPClient())) + + err := blizz.AccessTokenRequest(context.Background()) + if err != nil { + fmt.Println(err) + } +} + +func clientWithTimeout(c *http.Client) *http.Client { + c.Timeout = 10 * time.Second + return c +} diff --git a/v2/blizzard.go b/v2/blizzard.go index ba0dc81..4af9d71 100644 --- a/v2/blizzard.go +++ b/v2/blizzard.go @@ -189,7 +189,10 @@ func (c *Client) GetHTTPClient() *http.Client { return c.httpClient } -// SetHTTPClient sets the http.Client used for making requests +// SetHTTPClient sets the http.Client used for making requests. +// Calling `SetRegion` will overwrite this client. +// It is recommended to wrap the client returned by `GetHTTPClient` because +// the default one handles the oauth flow (see `golang.org/x/oauth2.NewClient`). func (c *Client) SetHTTPClient(h *http.Client) { c.httpClient = h }