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:
parent
d5e174e5b5
commit
e23e8aa300
1 changed files with 19 additions and 0 deletions
|
@ -36,6 +36,7 @@ type Client struct {
|
||||||
password string
|
password string
|
||||||
otp string
|
otp string
|
||||||
sudo string
|
sudo string
|
||||||
|
userAgent string
|
||||||
debug bool
|
debug bool
|
||||||
httpsigner *HTTPSign
|
httpsigner *HTTPSign
|
||||||
client *http.Client
|
client *http.Client
|
||||||
|
@ -215,6 +216,21 @@ func (c *Client) SetSudo(sudo string) {
|
||||||
c.mutex.Unlock()
|
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
|
// SetDebugMode is an option for NewClient to enable debug mode
|
||||||
func SetDebugMode() ClientOption {
|
func SetDebugMode() ClientOption {
|
||||||
return func(client *Client) error {
|
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 {
|
if len(c.sudo) != 0 {
|
||||||
req.Header.Set("Sudo", c.sudo)
|
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
|
client := c.client // client ref can change from this point on so safe it
|
||||||
c.mutex.RUnlock()
|
c.mutex.RUnlock()
|
||||||
|
|
Loading…
Reference in a new issue