Set User-Agent

pull/104/head
Yasuhiro Matsumoto 2019-06-22 01:14:52 +09:00 committed by mattn
parent 8f6192e26b
commit 3268207afe
2 changed files with 9 additions and 1 deletions

View File

@ -370,6 +370,7 @@ func run() int {
}
client := mastodon.NewClient(config)
client.UserAgent = "mstdn"
app.Metadata = map[string]interface{}{
"client": client,
"config": config,

View File

@ -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