Skip to content

Commit

Permalink
Merge pull request #286 from PagerDuty/user_context
Browse files Browse the repository at this point in the history
Update user.go to accept a context.Context
  • Loading branch information
Scott McAllister authored Mar 10, 2021
2 parents 1de0ea6 + 2c7abf0 commit 26db9f9
Showing 1 changed file with 188 additions and 49 deletions.
237 changes: 188 additions & 49 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,182 +89,321 @@ type GetCurrentUserOptions struct {
Includes []string `url:"include,omitempty,brackets"`
}

// ListUsers lists users of your PagerDuty account, optionally filtered by a search query.
// ListUsers lists users of your PagerDuty account, optionally filtered by a
// search query. It's recommended to use ListUsersWithContext instead.
func (c *Client) ListUsers(o ListUsersOptions) (*ListUsersResponse, error) {
return c.ListUsersWithContext(context.Background(), o)
}

// ListUsersWithContext lists users of your PagerDuty account, optionally filtered by a search query.
func (c *Client) ListUsersWithContext(ctx context.Context, o ListUsersOptions) (*ListUsersResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(context.TODO(), "/users?"+v.Encode())

resp, err := c.get(ctx, "/users?"+v.Encode())
if err != nil {
return nil, err
}

var result ListUsersResponse
return &result, c.decodeJSON(resp, &result)
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}

return &result, nil
}

// CreateUser creates a new user.
// CreateUser creates a new user. It's recommended to use CreateUserWithContext
// instead.
func (c *Client) CreateUser(u User) (*User, error) {
data := make(map[string]User)
data["user"] = u
resp, err := c.post(context.TODO(), "/users", data, nil)
return c.CreateUserWithContext(context.Background(), u)
}

// CreateUserWithContext creates a new user.
func (c *Client) CreateUserWithContext(ctx context.Context, u User) (*User, error) {
d := map[string]User{
"user": u,
}

resp, err := c.post(ctx, "/users", d, nil)
return getUserFromResponse(c, resp, err)
}

// DeleteUser deletes a user.
// DeleteUser deletes a user. It's recommended to use DeleteUserWithContext
// instead.
func (c *Client) DeleteUser(id string) error {
_, err := c.delete(context.TODO(), "/users/"+id)
return c.DeleteUserWithContext(context.Background(), id)
}

// DeleteUserWithContext deletes a user.
func (c *Client) DeleteUserWithContext(ctx context.Context, id string) error {
_, err := c.delete(ctx, "/users/"+id)
return err
}

// GetUser gets details about an existing user.
// GetUser gets details about an existing user. It's recommended to use
// GetUserWithContext instead.
func (c *Client) GetUser(id string, o GetUserOptions) (*User, error) {
return c.GetUserWithContext(context.Background(), id, o)
}

// GetUserWithContext gets details about an existing user.
func (c *Client) GetUserWithContext(ctx context.Context, id string, o GetUserOptions) (*User, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(context.TODO(), "/users/"+id+"?"+v.Encode())

resp, err := c.get(ctx, "/users/"+id+"?"+v.Encode())
return getUserFromResponse(c, resp, err)
}

// UpdateUser updates an existing user.
// UpdateUser updates an existing user. It's recommended to use
// UpdateUserWithContext instead.
func (c *Client) UpdateUser(u User) (*User, error) {
v := make(map[string]User)
v["user"] = u
resp, err := c.put(context.TODO(), "/users/"+u.ID, v, nil)
return c.UpdateUserWithContext(context.Background(), u)
}

// UpdateUserWithContext updates an existing user.
func (c *Client) UpdateUserWithContext(ctx context.Context, u User) (*User, error) {
d := map[string]User{
"user": u,
}

resp, err := c.put(ctx, "/users/"+u.ID, d, nil)
return getUserFromResponse(c, resp, err)
}

// GetCurrentUser gets details about the authenticated user when using a user-level API key or OAuth token
// GetCurrentUser gets details about the authenticated user when using a
// user-level API key or OAuth token. It's recommended to use
// GetCurrentUserWithContext instead.
func (c *Client) GetCurrentUser(o GetCurrentUserOptions) (*User, error) {
return c.GetCurrentUserWithContext(context.Background(), o)
}

// GetCurrentUserWithContext gets details about the authenticated user when
// using a user-level API key or OAuth token.
func (c *Client) GetCurrentUserWithContext(ctx context.Context, o GetCurrentUserOptions) (*User, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(context.TODO(), "/users/me?"+v.Encode())

resp, err := c.get(ctx, "/users/me?"+v.Encode())
return getUserFromResponse(c, resp, err)
}

func getUserFromResponse(c *Client, resp *http.Response, err error) (*User, error) {
if err != nil {
return nil, err
}

var target map[string]User
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
rootNode := "user"

const rootNode = "user"

t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}

return &t, nil
}

// ListUserContactMethods fetches contact methods of the existing user.
// ListUserContactMethods fetches contact methods of the existing user. It's
// recommended to use ListUserContactMethodsWithContext instead.
func (c *Client) ListUserContactMethods(userID string) (*ListContactMethodsResponse, error) {
resp, err := c.get(context.TODO(), "/users/"+userID+"/contact_methods")
return c.ListUserContactMethodsWithContext(context.Background(), userID)
}

// ListUserContactMethodsWithContext fetches contact methods of the existing user.
func (c *Client) ListUserContactMethodsWithContext(ctx context.Context, userID string) (*ListContactMethodsResponse, error) {
resp, err := c.get(ctx, "/users/"+userID+"/contact_methods")
if err != nil {
return nil, err
}

var result ListContactMethodsResponse
return &result, c.decodeJSON(resp, &result)
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}

return &result, nil
}

// GetUserContactMethod gets details about a contact method.
// GetUserContactMethod gets details about a contact method. It's recommended to
// use GetUserContactMethodWithContext instead.
func (c *Client) GetUserContactMethod(userID, contactMethodID string) (*ContactMethod, error) {
resp, err := c.get(context.TODO(), "/users/"+userID+"/contact_methods/"+contactMethodID)
return c.GetUserContactMethodWithContext(context.Background(), userID, contactMethodID)
}

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

// DeleteUserContactMethod deletes a user.
// DeleteUserContactMethod deletes a user. It's recommended to use
// DeleteUserContactMethodWithContext instead.
func (c *Client) DeleteUserContactMethod(userID, contactMethodID string) error {
_, err := c.delete(context.TODO(), "/users/"+userID+"/contact_methods/"+contactMethodID)
return c.DeleteUserContactMethodWithContext(context.Background(), userID, contactMethodID)
}

// DeleteUserContactMethodWithContext deletes a user.
func (c *Client) DeleteUserContactMethodWithContext(ctx context.Context, userID, contactMethodID string) error {
_, err := c.delete(ctx, "/users/"+userID+"/contact_methods/"+contactMethodID)
return err
}

// CreateUserContactMethod creates a new contact method for user.
// CreateUserContactMethod creates a new contact method for user. It's
// recommended to use CreateUserContactMethodWithContext instead.
func (c *Client) CreateUserContactMethod(userID string, cm ContactMethod) (*ContactMethod, error) {
data := make(map[string]ContactMethod)
data["contact_method"] = cm
resp, err := c.post(context.TODO(), "/users/"+userID+"/contact_methods", data, nil)
return c.CreateUserContactMethodWithContext(context.Background(), userID, cm)
}

// CreateUserContactMethodWithContext creates a new contact method for user.
func (c *Client) CreateUserContactMethodWithContext(ctx context.Context, userID string, cm ContactMethod) (*ContactMethod, error) {
d := map[string]ContactMethod{
"contact_method": cm,
}

resp, err := c.post(ctx, "/users/"+userID+"/contact_methods", d, nil)
return getContactMethodFromResponse(c, resp, err)
}

// UpdateUserContactMethod updates an existing user.
// UpdateUserContactMethod updates an existing user. It's recommended to use
// UpdateUserContactMethodWithContext instead.
func (c *Client) UpdateUserContactMethod(userID string, cm ContactMethod) (*ContactMethod, error) {
v := make(map[string]ContactMethod)
v["contact_method"] = cm
resp, err := c.put(context.TODO(), "/users/"+userID+"/contact_methods/"+cm.ID, v, nil)
return c.UpdateUserContactMethodWthContext(context.Background(), userID, cm)
}

// UpdateUserContactMethodWthContext updates an existing user.
func (c *Client) UpdateUserContactMethodWthContext(ctx context.Context, userID string, cm ContactMethod) (*ContactMethod, error) {
d := map[string]ContactMethod{
"contact_method": cm,
}

resp, err := c.put(ctx, "/users/"+userID+"/contact_methods/"+cm.ID, d, nil)
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"

const rootNode = "contact_method"

t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}

return &t, nil
}

// GetUserNotificationRule gets details about a notification rule.
// GetUserNotificationRule gets details about a notification rule. It's
// recommended to use GetUserNotificationRuleWithContext instead.
func (c *Client) GetUserNotificationRule(userID, ruleID string) (*NotificationRule, error) {
resp, err := c.get(context.TODO(), "/users/"+userID+"/notification_rules/"+ruleID)
return c.GetUserNotificationRuleWithContext(context.Background(), userID, ruleID)
}

// GetUserNotificationRuleWithContext gets details about a notification rule.
func (c *Client) GetUserNotificationRuleWithContext(ctx context.Context, userID, ruleID string) (*NotificationRule, error) {
resp, err := c.get(ctx, "/users/"+userID+"/notification_rules/"+ruleID)
return getUserNotificationRuleFromResponse(c, resp, err)
}

// CreateUserNotificationRule creates a new notification rule for a user.
// CreateUserNotificationRule creates a new notification rule for a user. It's
// recommended to use CreateUserNotificationRuleWithContext instead.
func (c *Client) CreateUserNotificationRule(userID string, rule NotificationRule) (*NotificationRule, error) {
data := make(map[string]NotificationRule)
data["notification_rule"] = rule
resp, err := c.post(context.TODO(), "/users/"+userID+"/notification_rules", data, nil)
return c.CreateUserNotificationRuleWithContext(context.Background(), userID, rule)
}

// CreateUserNotificationRuleWithContext creates a new notification rule for a user.
func (c *Client) CreateUserNotificationRuleWithContext(ctx context.Context, userID string, rule NotificationRule) (*NotificationRule, error) {
d := map[string]NotificationRule{
"notification_rule": rule,
}

resp, err := c.post(ctx, "/users/"+userID+"/notification_rules", d, nil)
return getUserNotificationRuleFromResponse(c, resp, err)
}

// UpdateUserNotificationRule updates a notification rule for a user.
// UpdateUserNotificationRule updates a notification rule for a user. It's
// recommended to use UpdateUserNotificationRuleWithContext instead.
func (c *Client) UpdateUserNotificationRule(userID string, rule NotificationRule) (*NotificationRule, error) {
data := make(map[string]NotificationRule)
data["notification_rule"] = rule
resp, err := c.put(context.TODO(), "/users/"+userID+"/notification_rules/"+rule.ID, data, nil)
return c.UpdateUserNotificationRuleWithContext(context.Background(), userID, rule)
}

// UpdateUserNotificationRuleWithContext updates a notification rule for a user.
func (c *Client) UpdateUserNotificationRuleWithContext(ctx context.Context, userID string, rule NotificationRule) (*NotificationRule, error) {
d := map[string]NotificationRule{
"notification_rule": rule,
}

resp, err := c.put(ctx, "/users/"+userID+"/notification_rules/"+rule.ID, d, nil)
return getUserNotificationRuleFromResponse(c, resp, err)
}

// DeleteUserNotificationRule deletes a notification rule for a user.
// DeleteUserNotificationRule deletes a notification rule for a user. It's
// recommended to use DeleteUserNotificationRuleWithContext instead.
func (c *Client) DeleteUserNotificationRule(userID, ruleID string) error {
_, err := c.delete(context.TODO(), "/users/"+userID+"/notification_rules/"+ruleID)
return c.DeleteUserNotificationRuleWithContext(context.Background(), userID, ruleID)
}

// DeleteUserNotificationRuleWithContext deletes a notification rule for a user.
func (c *Client) DeleteUserNotificationRuleWithContext(ctx context.Context, userID, ruleID string) error {
_, err := c.delete(ctx, "/users/"+userID+"/notification_rules/"+ruleID)
return err
}

// ListUserNotificationRules fetches notification rules of the existing user.
func (c *Client) ListUserNotificationRules(userID string) (*ListUserNotificationRulesResponse, error) {
resp, err := c.get(context.TODO(), "/users/"+userID+"/notification_rules")
return c.ListUserNotificationRulesWithContext(context.Background(), userID)
}

// ListUserNotificationRulesWithContext fetches notification rules of the existing user.
func (c *Client) ListUserNotificationRulesWithContext(ctx context.Context, userID string) (*ListUserNotificationRulesResponse, error) {
resp, err := c.get(ctx, "/users/"+userID+"/notification_rules")
if err != nil {
return nil, err
}

var result ListUserNotificationRulesResponse
return &result, c.decodeJSON(resp, &result)
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}

return &result, nil
}

func getUserNotificationRuleFromResponse(c *Client, resp *http.Response, err error) (*NotificationRule, error) {
if err != nil {
return nil, err
}

var target map[string]NotificationRule
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
rootNode := "notification_rule"

const rootNode = "notification_rule"

t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}

return &t, nil
}

0 comments on commit 26db9f9

Please sign in to comment.