Add method to set Sudo header (#144)

This commit is contained in:
Serhii Kuts 2019-02-16 00:32:08 +02:00 committed by techknowlogick
parent 600c19aa03
commit ad9bcd6a96

View file

@ -23,6 +23,7 @@ func Version() string {
type Client struct { type Client struct {
url string url string
accessToken string accessToken string
sudo string
client *http.Client client *http.Client
} }
@ -40,12 +41,20 @@ func (c *Client) SetHTTPClient(client *http.Client) {
c.client = client c.client = client
} }
// SetSudo sets username to impersonate.
func (c *Client) SetSudo(sudo string) {
c.sudo = sudo
}
func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*http.Response, error) { func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, c.url+"/api/v1"+path, body) req, err := http.NewRequest(method, c.url+"/api/v1"+path, body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.Header.Set("Authorization", "token "+c.accessToken) req.Header.Set("Authorization", "token "+c.accessToken)
if c.sudo != "" {
req.Header.Set("Sudo", c.sudo)
}
for k, v := range header { for k, v := range header {
req.Header[k] = v req.Header[k] = v
} }