Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(bug)allow passing headers in http do call. fix manage incident call #33

Merged
merged 1 commit into from
Sep 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *Client) GetAddon(id string) (*Addon, error) {
func (c *Client) UpdateAddon(id string, a Addon) (*Addon, error) {
v := make(map[string]Addon)
v["addon"] = a
resp, err := c.put("/addons/"+id, v)
resp, err := c.put("/addons/"+id, v, nil)
if err != nil {
return nil, err
}
Expand Down
18 changes: 12 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,41 @@ func NewClient(authToken string) *Client {
}

func (c *Client) delete(path string) (*http.Response, error) {
return c.do("DELETE", path, nil)
return c.do("DELETE", path, nil, nil)
}

func (c *Client) put(path string, payload interface{}) (*http.Response, error) {
func (c *Client) put(path string, payload interface{}, headers *map[string]string) (*http.Response, error) {
data, err := json.Marshal(payload)
if err != nil {
return nil, err
}
return c.do("PUT", path, bytes.NewBuffer(data))
return c.do("PUT", path, bytes.NewBuffer(data), headers)
}

func (c *Client) post(path string, payload interface{}) (*http.Response, error) {
data, err := json.Marshal(payload)
if err != nil {
return nil, err
}
return c.do("POST", path, bytes.NewBuffer(data))
return c.do("POST", path, bytes.NewBuffer(data), nil)
}

func (c *Client) get(path string) (*http.Response, error) {
return c.do("GET", path, nil)
return c.do("GET", path, nil, nil)
}

func (c *Client) do(method, path string, body io.Reader) (*http.Response, error) {
func (c *Client) do(method, path string, body io.Reader, headers *map[string]string) (*http.Response, error) {
endpoint := apiEndpoint + path
req, _ := http.NewRequest(method, endpoint, body)
req.Header.Set("Accept", "application/vnd.pagerduty+json;version=2")
if headers != nil {
for k, v := range *headers {
req.Header.Set(k, v)
}
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Token token="+c.authToken)

resp, err := http.DefaultClient.Do(req)
return c.checkResponse(resp, err)
}
Expand Down
2 changes: 1 addition & 1 deletion escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *Client) GetEscalationPolicy(id string, o *GetEscalationPolicyOptions) (

// UpdateEscalationPolicy updates an existing escalation policy and its rules.
func (c *Client) UpdateEscalationPolicy(id string, e *EscalationPolicy) (*EscalationPolicy, error) {
resp, err := c.put(escPath+"/"+id, e)
resp, err := c.put(escPath+"/"+id, e, nil)
return getEscalationPolicyFromResponse(c, resp, err)
}

Expand Down
15 changes: 4 additions & 11 deletions incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,13 @@ func (c *Client) ListIncidents(o ListIncidentsOptions) (*ListIncidentsResponse,
return &result, c.decodeJSON(resp, &result)
}

// ManageIncidentsOptions is the data structure used when calling the ManageIncident API endpoint.
type ManageIncidentsOptions struct {
From string `url:"from,omitempty"`
}

// ManageIncidents acknowledges, resolves, escalates, or reassigns one or more incidents.
func (c *Client) ManageIncidents(incidents []Incident, o ManageIncidentsOptions) error {
v, err := query.Values(o)
if err != nil {
return err
}
func (c *Client) ManageIncidents(from string, incidents []Incident) error {
r := make(map[string][]Incident)
headers := make(map[string]string)
headers["From"] = from
r["incidents"] = incidents
_, e := c.put("/incidents?"+v.Encode(), r)
_, e := c.put("/incidents", r, &headers)
return e
}

Expand Down
2 changes: 1 addition & 1 deletion maintenance_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (c *Client) GetMaintenanceWindow(id string, o GetMaintenanceWindowOptions)

// UpdateMaintenanceWindow updates an existing maintenance window.
func (c *Client) UpdateMaintenanceWindow(m MaintenanceWindow) (*MaintenanceWindow, error) {
resp, err := c.put("/maintenance_windows/"+m.ID, m)
resp, err := c.put("/maintenance_windows/"+m.ID, m, nil)
return getMaintenanceWindowFromResponse(c, resp, err)
}

Expand Down
2 changes: 1 addition & 1 deletion schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type UpdateScheduleOptions struct {
func (c *Client) UpdateSchedule(id string, s Schedule) (*Schedule, error) {
v := make(map[string]Schedule)
v["schedule"] = s
resp, err := c.put("/schedules/"+id, v)
resp, err := c.put("/schedules/"+id, v, nil)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (c *Client) CreateService(s Service) (*Service, error) {

// UpdateService updates an existing service.
func (c *Client) UpdateService(s Service) (*Service, error) {
resp, err := c.put("/services/"+s.ID, s)
resp, err := c.put("/services/"+s.ID, s, nil)
return getServiceFromResponse(c, resp, err)
}

Expand Down Expand Up @@ -157,7 +157,7 @@ func (c *Client) GetIntegration(serviceID, integrationID string, o GetIntegratio

// UpdateIntegration updates an integration belonging to a service.
func (c *Client) UpdateIntegration(serviceID string, i Integration) (*Integration, error) {
resp, err := c.put("/services/"+serviceID+"/integrations/"+i.ID, i)
resp, err := c.put("/services/"+serviceID+"/integrations/"+i.ID, i, nil)
return getIntegrationFromResponse(c, resp, err)
}

Expand Down
6 changes: 3 additions & 3 deletions team.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c *Client) GetTeam(id string) (*Team, error) {

// UpdateTeam updates an existing team.
func (c *Client) UpdateTeam(id string, t *Team) (*Team, error) {
resp, err := c.put("/teams/"+id, t)
resp, err := c.put("/teams/"+id, t, nil)
return getTeamFromResponse(c, resp, err)
}

Expand All @@ -72,7 +72,7 @@ func (c *Client) RemoveEscalationPolicyFromTeam(teamID, epID string) error {

// AddEscalationPolicyToTeam adds an escalation policy to a team.
func (c *Client) AddEscalationPolicyToTeam(teamID, epID string) error {
_, err := c.put("/teams/"+teamID+"/escalation_policies/"+epID, nil)
_, err := c.put("/teams/"+teamID+"/escalation_policies/"+epID, nil, nil)
return err
}

Expand All @@ -84,7 +84,7 @@ func (c *Client) RemoveUserFromTeam(teamID, userID string) error {

// AddUserToTeam adds a user to a team.
func (c *Client) AddUserToTeam(teamID, userID string) error {
_, err := c.put("/teams/"+teamID+"/users/"+userID, nil)
_, err := c.put("/teams/"+teamID+"/users/"+userID, nil, nil)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion user.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (c *Client) GetUser(id string, o GetUserOptions) (*User, error) {
func (c *Client) UpdateUser(u User) (*User, error) {
v := make(map[string]User)
v["user"] = u
resp, err := c.put("/users/"+u.ID, v)
resp, err := c.put("/users/"+u.ID, v, nil)
return getUserFromResponse(c, resp, err)
}

Expand Down