Skip to content

Commit

Permalink
Add ListContactMethods and GetContactMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Mencarini committed Oct 27, 2018
1 parent ad1e449 commit 801c4af
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
59 changes: 59 additions & 0 deletions contact_method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package pagerduty

import (
"fmt"
"net/http"
)

// ContactMethod is a way of contacting the user.
type ContactMethod struct {
ID string `json:"id"`
Type string `json:"type"`
Summary string `json:"summary"`
Self string `json:"self"`
Label string `json:"label"`
Address string `json:"address"`
SendShortEmail bool `json:"send_short_email,omitempty"`
SendHTMLEmail bool `json:"send_html_email,omitempty"`
Blacklisted bool `json:"blacklisted,omitempty"`
CountryCode int `json:"country_code,omitempty"`
Enabled bool `json:"enabled,omitempty"`
}

// ListContactMethodsResponse is the data structure returned from calling the ListContactMethods API endpoint.
type ListContactMethodsResponse struct {
APIListObject
ContactMethods []ContactMethod
}

// ListContactMethods lists all contact methods for a user.
func (c *Client) ListContactMethods(userID string) (*ListContactMethodsResponse, error) {
resp, err := c.get("/users/" + userID + "/contact_methods")
if err != nil {
return nil, err
}
var result ListContactMethodsResponse
return &result, c.decodeJSON(resp, &result)
}

// GetContactMethod gets details about a contact method.
func (c *Client) GetContactMethod(userID, id string) (*ContactMethod, error) {
resp, err := c.get("/users/" + userID + "/contact_methods/" + id)
return getContactMethodFromResponse(c, resp, err)
}

func getContactMethodFromResponse(c *Client, resp *http.Response, err error) (*ContactMethod, error) {
if err != nil {
return nil, err
}
var target map[string]ContactMethod
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
rootNode := "contact_method"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}
9 changes: 0 additions & 9 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ import (
"github.com/google/go-querystring/query"
)

// ContactMethod is a way of contacting the user.
type ContactMethod struct {
ID string
Label string
Address string
Type string
SendShortEmail bool `json:"send_short_email"`
}

// NotificationRule is a rule for notifying the user.
type NotificationRule struct {
ID string
Expand Down

0 comments on commit 801c4af

Please sign in to comment.