From 7ed7983b7b7d56c4fd49a6ed89a977c701cb4830 Mon Sep 17 00:00:00 2001 From: Neo <65282986+Neoration@users.noreply.github.com> Date: Tue, 12 Apr 2022 08:48:29 +0900 Subject: [PATCH] api: Add *Client.WithLocale --- api/api.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/api/api.go b/api/api.go index 61a4ee1..ad9a3a9 100644 --- a/api/api.go +++ b/api/api.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/diamondburned/arikawa/v3/api/rate" + "github.com/diamondburned/arikawa/v3/discord" "github.com/diamondburned/arikawa/v3/utils/httputil" "github.com/diamondburned/arikawa/v3/utils/httputil/httpdriver" ) @@ -49,6 +50,22 @@ func NewCustomClient(token string, httpClient *httputil.Client) *Client { return c } +// WithLocale creates a copy of Client with an explicitly stated language locale +// using the X-Discord-Locale HTTP header. +func (c *Client) WithLocale(language discord.Language) *Client { + client := c.Client.Copy() + client.OnRequest = append(client.OnRequest, func(r httpdriver.Request) error { + r.AddHeader(http.Header{"X-Discord-Locale": []string{string(language)}}) + return nil + }) + + return &Client{ + Client: client, + Session: c.Session, + AcquireOptions: c.AcquireOptions, + } +} + // WithContext returns a shallow copy of Client with the given context. It's // used for method timeouts and such. This method is thread-safe. func (c *Client) WithContext(ctx context.Context) *Client {