add option to set user-agent to gitea client (#637)

This PR adds the ability to set a custom user-agent to the gitea client

Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/637
Reviewed-by: 6543 <6543@obermui.de>
Reviewed-by: John Olheiser <john+gitea@jolheiser.com>
Co-authored-by: crapStone <crapstone01@gmail.com>
Co-committed-by: crapStone <crapstone01@gmail.com>
This commit is contained in:
crapStone 2023-11-15 01:43:37 +00:00 committed by John Olheiser
parent d5e174e5b5
commit e23e8aa300
1 changed files with 19 additions and 0 deletions

View File

@ -36,6 +36,7 @@ type Client struct {
password string
otp string
sudo string
userAgent string
debug bool
httpsigner *HTTPSign
client *http.Client
@ -215,6 +216,21 @@ func (c *Client) SetSudo(sudo string) {
c.mutex.Unlock()
}
// SetUserAgent is an option for NewClient to set user-agent header
func SetUserAgent(userAgent string) ClientOption {
return func(client *Client) error {
client.SetUserAgent(userAgent)
return nil
}
}
// SetUserAgent sets the user-agent to send with every request.
func (c *Client) SetUserAgent(userAgent string) {
c.mutex.Lock()
c.userAgent = userAgent
c.mutex.Unlock()
}
// SetDebugMode is an option for NewClient to enable debug mode
func SetDebugMode() ClientOption {
return func(client *Client) error {
@ -282,6 +298,9 @@ func (c *Client) doRequest(method, path string, header http.Header, body io.Read
if len(c.sudo) != 0 {
req.Header.Set("Sudo", c.sudo)
}
if len(c.userAgent) != 0 {
req.Header.Set("User-Agent", c.userAgent)
}
client := c.client // client ref can change from this point on so safe it
c.mutex.RUnlock()