Skip to content

Commit

Permalink
Added partial support for user impersonation.
Browse files Browse the repository at this point in the history
  • Loading branch information
m4ng0 committed Nov 24, 2020
1 parent c3a0287 commit a710c6c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
21 changes: 17 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

type Client struct {
endpoint string
apikey string
endpoint string
apikey string
switchUser string
*http.Client
Limit int
Offset int
Expand All @@ -24,12 +25,12 @@ var DefaultLimit int = -1 // "-1" means "No setting"
var DefaultOffset int = -1 //"-1" means "No setting"

func NewClient(endpoint, apikey string) *Client {
return &Client{endpoint, apikey, http.DefaultClient, DefaultLimit, DefaultOffset}
return &Client{endpoint, apikey, "", http.DefaultClient, DefaultLimit, DefaultOffset}
}

func NewFullTraversingClient(endpoint, apikey string) *FullTraversingClient {
//return &FullTraversingClient{NewClient(endpoint, apikey)}
return &FullTraversingClient{&Client{endpoint, apikey, http.DefaultClient, 100, 0}}
return &FullTraversingClient{&Client{endpoint, apikey, "", http.DefaultClient, 100, 0}}
}

// URLWithFilter return string url by concat endpoint, path and filter
Expand Down Expand Up @@ -62,6 +63,18 @@ func (c *Client) getPaginationClause() string {
return clause
}

func (c *Client) SwitchUser(userLogin string) {
c.switchUser = userLogin
}

func (c *Client) Do(req *http.Request) (*http.Response, error) {
req.Header.Add("X-Redmine-API-Key", c.apikey)
if c.switchUser != "" {
req.Header.Add("X-Redmine-Switch-User", c.switchUser)
}
return c.Client.Do(req)
}

type errorsResult struct {
Errors []string `json:"errors"`
}
Expand Down
1 change: 0 additions & 1 deletion issue_customfields.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func (c *Client) CustomFields() ([]CustomField, error) {
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Redmine-API-Key", c.apikey)

res, err := c.Do(req)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion project.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func (c *Client) getProjects(filter *ProjectsFilter) (*projectsResult, error) {
if err != nil {
return nil, err
}
req.Header.Add("X-Redmine-API-Key", c.apikey)
res, err := c.Do(req)
if err != nil {
return nil, err
Expand Down
1 change: 0 additions & 1 deletion time_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func (c *Client) TimeEntriesWithFilter(filter Filter) ([]TimeEntry, error) {
if err != nil {
return nil, err
}
req.Header.Add("X-Redmine-API-Key", c.apikey)
res, err := c.Do(req)
if err != nil {
return nil, err
Expand Down
2 changes: 0 additions & 2 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func (c *Client) UsersWithFilter(filter *UsersFilter) ([]User, error) {
if err != nil {
return nil, err
}
req.Header.Add("X-Redmine-API-Key", c.apikey)
res, err := c.Do(req)
if err != nil {
return nil, err
Expand Down Expand Up @@ -163,7 +162,6 @@ func (c *Client) UserByIdAndFilter(id int, filter *UserByIdFilter) (*User, error
if err != nil {
return nil, err
}
req.Header.Add("X-Redmine-API-Key", c.apikey)
res, err := c.Do(req)
if err != nil {
return nil, err
Expand Down

0 comments on commit a710c6c

Please sign in to comment.