diff --git a/cmd/mstdn/main.go b/cmd/mstdn/main.go index f17e27a..4386768 100644 --- a/cmd/mstdn/main.go +++ b/cmd/mstdn/main.go @@ -370,6 +370,7 @@ func run() int { } client := mastodon.NewClient(config) + client.UserAgent = "mstdn" app.Metadata = map[string]interface{}{ "client": client, "config": config, diff --git a/mastodon.go b/mastodon.go index d2f6182..d0a94d4 100644 --- a/mastodon.go +++ b/mastodon.go @@ -31,7 +31,8 @@ type Config struct { // Client is a API client for mastodon. type Client struct { http.Client - config *Config + config *Config + UserAgent string } func (c *Client) doAPI(ctx context.Context, method string, uri string, params interface{}, res interface{}, pg *Pagination) error { @@ -117,6 +118,9 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in if params != nil { req.Header.Set("Content-Type", ct) } + if c.UserAgent != "" { + req.Header.Set("User-Agent", c.UserAgent) + } var resp *http.Response backoff := 1000 * time.Millisecond @@ -211,6 +215,9 @@ func (c *Client) authenticate(ctx context.Context, params url.Values) error { } req = req.WithContext(ctx) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + if c.UserAgent != "" { + req.Header.Set("User-Agent", c.UserAgent) + } resp, err := c.Do(req) if err != nil { return err