Browse Source

support token as well as basic auth for rt-rest2

develop
parent
commit
a1849a8bb6
  1. 6
      config.go
  2. 30
      rt2/rt2.go

6
config.go

@ -29,7 +29,8 @@ type rtConfig struct {
type rt2Config struct {
URL string
Token string
User string
Password string
Insecure bool
}
@ -69,7 +70,8 @@ var defaultConfig = config{
},
RT2: rt2Config{
URL: "https://support.example.com",
Token: "secret",
User: "apiuser",
Password: "secret",
Insecure: true,
},
Cache: cacheConfig{

30
rt2/rt2.go

@ -100,17 +100,18 @@ type TicketCommentTemplateRequest struct {
type Client struct {
url *url.URL
user string
token string
password string
insecureSkipVerify bool
debug bool
}
// NewClient prepares a Client for usage.
func NewClient(rtURL string, user, token string, insecureSkipVerify bool) (*Client, error) {
func NewClient(rtURL string, user, token string, insecureSkipVerify, debug bool) (*Client, error) {
x, err := url.Parse(rtURL)
if err != nil {
return nil, err
}
return &Client{url: x, token: token, insecureSkipVerify: insecureSkipVerify}, nil
return &Client{url: x, user: user, password: token, insecureSkipVerify: insecureSkipVerify, debug: debug}, nil
}
func (c *Client) Ticket(id int) (*Ticket, error) {
@ -266,9 +267,17 @@ func BuildPlainRequest(client *Client, method string, url string, body io.Reader
return nil, err
}
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("Authorization", fmt.Sprintf("token %s", client.token))
log.Printf("Request: +%v\n", req)
if client.user == "" {
req.Header.Set("Authorization", fmt.Sprintf("token %s", client.password))
} else {
req.SetBasicAuth(client.user, client.password)
}
if client.debug {
log.Printf("Request: +%v\n", req)
}
return req, nil
}
@ -279,9 +288,14 @@ func BuildJSONRequest(client *Client, method string, url string, body io.Reader)
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("token %s", client.token))
log.Printf("Request: +%v\n", req)
if client.user == "" {
req.Header.Set("Authorization", fmt.Sprintf("token %s", client.token))
} else {
req.SetBasicAuth(client.user, client.token)
}
if client.debug {
log.Printf("Request: +%v\n", req)
}
return req, nil
}

Loading…
Cancel
Save