From 7b5877326df52dafa7f62efdc2c10c7952ccfd04 Mon Sep 17 00:00:00 2001 From: Tim Heckman Date: Wed, 3 Mar 2021 22:54:08 -0800 Subject: [PATCH] Update escalation_policy.go to accept a context.Context Updates #267 --- escalation_policy.go | 157 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 124 insertions(+), 33 deletions(-) diff --git a/escalation_policy.go b/escalation_policy.go index b094c0a4..3f2386dc 100644 --- a/escalation_policy.go +++ b/escalation_policy.go @@ -37,6 +37,8 @@ type ListEscalationPoliciesResponse struct { EscalationPolicies []EscalationPolicy `json:"escalation_policies"` } +// ListEscalationRulesResponse represents the data structure returned when +// calling the ListEscalationRules API endpoint. type ListEscalationRulesResponse struct { APIListObject EscalationRules []EscalationRule `json:"escalation_rules"` @@ -57,31 +59,57 @@ type GetEscalationRuleOptions struct { Includes []string `url:"include,omitempty,brackets"` } -// ListEscalationPolicies lists all of the existing escalation policies. +// ListEscalationPolicies lists all of the existing escalation policies. It's +// recommended to use ListEscalationPoliciesWithContext instead. func (c *Client) ListEscalationPolicies(o ListEscalationPoliciesOptions) (*ListEscalationPoliciesResponse, error) { + return c.ListEscalationPoliciesWithContext(context.Background(), o) +} + +// ListEscalationPoliciesWithContext lists all of the existing escalation policies. +func (c *Client) ListEscalationPoliciesWithContext(ctx context.Context, o ListEscalationPoliciesOptions) (*ListEscalationPoliciesResponse, error) { v, err := query.Values(o) if err != nil { return nil, err } - resp, err := c.get(context.TODO(), escPath+"?"+v.Encode()) + + resp, err := c.get(ctx, escPath+"?"+v.Encode()) if err != nil { return nil, err } + var result ListEscalationPoliciesResponse - return &result, c.decodeJSON(resp, &result) + if err = c.decodeJSON(resp, &result); err != nil { + return nil, err + } + + return &result, nil } -// CreateEscalationPolicy creates a new escalation policy. +// CreateEscalationPolicy creates a new escalation policy. It's recommended to +// use CreateEscalationPolicyWithContext instead. func (c *Client) CreateEscalationPolicy(e EscalationPolicy) (*EscalationPolicy, error) { - data := make(map[string]EscalationPolicy) - data["escalation_policy"] = e - resp, err := c.post(context.TODO(), escPath, data, nil) + return c.CreateEscalationPolicyWithContext(context.Background(), e) +} + +// CreateEscalationPolicyWithContext creates a new escalation policy. +func (c *Client) CreateEscalationPolicyWithContext(ctx context.Context, e EscalationPolicy) (*EscalationPolicy, error) { + d := map[string]EscalationPolicy{ + "escalation_policy": e, + } + + resp, err := c.post(ctx, escPath, d, nil) return getEscalationPolicyFromResponse(c, resp, err) } -// DeleteEscalationPolicy deletes an existing escalation policy and rules. +// DeleteEscalationPolicy deletes an existing escalation policy and rules. It's +// recommended to use DeleteEscalationPolicyWithContext instead. func (c *Client) DeleteEscalationPolicy(id string) error { - _, err := c.delete(context.TODO(), escPath+"/"+id) + return c.DeleteEscalationPolicyWithContext(context.Background(), id) +} + +// DeleteEscalationPolicyWithContext deletes an existing escalation policy and rules. +func (c *Client) DeleteEscalationPolicyWithContext(ctx context.Context, id string) error { + _, err := c.delete(ctx, escPath+"/"+id) return err } @@ -90,98 +118,161 @@ type GetEscalationPolicyOptions struct { Includes []string `url:"include,omitempty,brackets"` } -// GetEscalationPolicy gets information about an existing escalation policy and its rules. +// GetEscalationPolicy gets information about an existing escalation policy and +// its rules. It's recommended to use GetEscalationPolicyWithContext instead. func (c *Client) GetEscalationPolicy(id string, o *GetEscalationPolicyOptions) (*EscalationPolicy, error) { + return c.GetEscalationPolicyWithContext(context.Background(), id, o) +} + +// GetEscalationPolicyWithContext gets information about an existing escalation +// policy and its rules. +func (c *Client) GetEscalationPolicyWithContext(ctx context.Context, id string, o *GetEscalationPolicyOptions) (*EscalationPolicy, error) { v, err := query.Values(o) if err != nil { return nil, err } - resp, err := c.get(context.TODO(), escPath+"/"+id+"?"+v.Encode()) + + resp, err := c.get(ctx, escPath+"/"+id+"?"+v.Encode()) return getEscalationPolicyFromResponse(c, resp, err) } // UpdateEscalationPolicy updates an existing escalation policy and its rules. +// It's recommended to use UpdateEscalationPolicyWithContext instead. func (c *Client) UpdateEscalationPolicy(id string, e *EscalationPolicy) (*EscalationPolicy, error) { - data := make(map[string]EscalationPolicy) - data["escalation_policy"] = *e - resp, err := c.put(context.TODO(), escPath+"/"+id, data, nil) + return c.UpdateEscalationPolicyWithContext(context.Background(), id, *e) +} + +// UpdateEscalationPolicyWithContext updates an existing escalation policy and its rules. +func (c *Client) UpdateEscalationPolicyWithContext(ctx context.Context, id string, e EscalationPolicy) (*EscalationPolicy, error) { + d := map[string]EscalationPolicy{ + "escalation_policy": e, + } + + resp, err := c.put(ctx, escPath+"/"+id, d, nil) return getEscalationPolicyFromResponse(c, resp, err) } // CreateEscalationRule creates a new escalation rule for an escalation policy -// and appends it to the end of the existing escalation rules. +// and appends it to the end of the existing escalation rules. It's recommended +// to use CreateEscalationRuleWithContext instead. func (c *Client) CreateEscalationRule(escID string, e EscalationRule) (*EscalationRule, error) { - data := make(map[string]EscalationRule) - data["escalation_rule"] = e - resp, err := c.post(context.TODO(), escPath+"/"+escID+"/escalation_rules", data, nil) + return c.CreateEscalationRuleWithContext(context.Background(), escID, e) +} + +// CreateEscalationRuleWithContext creates a new escalation rule for an escalation policy +// and appends it to the end of the existing escalation rules. +func (c *Client) CreateEscalationRuleWithContext(ctx context.Context, escID string, e EscalationRule) (*EscalationRule, error) { + d := map[string]EscalationRule{ + "escalation_rule": e, + } + + resp, err := c.post(ctx, escPath+"/"+escID+"/escalation_rules", d, nil) return getEscalationRuleFromResponse(c, resp, err) } -// GetEscalationRule gets information about an existing escalation rule. +// GetEscalationRule gets information about an existing escalation rule. It's +// recommended to use GetEscalationRuleWithContext instead. func (c *Client) GetEscalationRule(escID string, id string, o *GetEscalationRuleOptions) (*EscalationRule, error) { + return c.GetEscalationRuleWithContext(context.Background(), escID, id, o) +} + +// GetEscalationRuleWithContext gets information about an existing escalation rule. +func (c *Client) GetEscalationRuleWithContext(ctx context.Context, escID string, id string, o *GetEscalationRuleOptions) (*EscalationRule, error) { v, err := query.Values(o) if err != nil { return nil, err } - resp, err := c.get(context.TODO(), escPath+"/"+escID+"/escalation_rules/"+id+"?"+v.Encode()) + + resp, err := c.get(ctx, escPath+"/"+escID+"/escalation_rules/"+id+"?"+v.Encode()) return getEscalationRuleFromResponse(c, resp, err) } -// DeleteEscalationRule deletes an existing escalation rule. +// DeleteEscalationRule deletes an existing escalation rule. It's recommended to +// use DeleteEscalationRuleWithContext instead. func (c *Client) DeleteEscalationRule(escID string, id string) error { - _, err := c.delete(context.TODO(), escPath+"/"+escID+"/escalation_rules/"+id) + return c.DeleteEscalationRuleWithContext(context.Background(), escID, id) +} + +// DeleteEscalationRuleWithContext deletes an existing escalation rule. +func (c *Client) DeleteEscalationRuleWithContext(ctx context.Context, escID string, id string) error { + _, err := c.delete(ctx, escPath+"/"+escID+"/escalation_rules/"+id) return err } -// UpdateEscalationRule updates an existing escalation rule. +// UpdateEscalationRule updates an existing escalation rule. It's recommended to +// use UpdateEscalationRuleWithContext instead. func (c *Client) UpdateEscalationRule(escID string, id string, e *EscalationRule) (*EscalationRule, error) { - data := make(map[string]EscalationRule) - data["escalation_rule"] = *e - resp, err := c.put(context.TODO(), escPath+"/"+escID+"/escalation_rules/"+id, data, nil) + return c.UpdateEscalationRuleWithContext(context.Background(), escID, id, *e) +} + +// UpdateEscalationRuleWithContext updates an existing escalation rule. +func (c *Client) UpdateEscalationRuleWithContext(ctx context.Context, escID string, id string, e EscalationRule) (*EscalationRule, error) { + d := map[string]EscalationRule{ + "escalation_rule": e, + } + + resp, err := c.put(ctx, escPath+"/"+escID+"/escalation_rules/"+id, d, nil) return getEscalationRuleFromResponse(c, resp, err) } -// ListEscalationRules lists all of the escalation rules for an existing escalation policy. +// ListEscalationRules lists all of the escalation rules for an existing +// escalation policy. It's recommended to use ListEscalationRulesWithContext +// instead. func (c *Client) ListEscalationRules(escID string) (*ListEscalationRulesResponse, error) { - resp, err := c.get(context.TODO(), escPath+"/"+escID+"/escalation_rules") + return c.ListEscalationRulesWithContext(context.Background(), escID) +} + +// ListEscalationRulesWithContext lists all of the escalation rules for an existing escalation policy. +func (c *Client) ListEscalationRulesWithContext(ctx context.Context, escID string) (*ListEscalationRulesResponse, error) { + resp, err := c.get(ctx, escPath+"/"+escID+"/escalation_rules") if err != nil { return nil, err } var result ListEscalationRulesResponse - return &result, c.decodeJSON(resp, &result) + if err = c.decodeJSON(resp, &result); err != nil { + return nil, err + } + + return &result, nil } func getEscalationRuleFromResponse(c *Client, resp *http.Response, err error) (*EscalationRule, error) { - defer resp.Body.Close() if err != nil { return nil, err } + var target map[string]EscalationRule if dErr := c.decodeJSON(resp, &target); dErr != nil { return nil, fmt.Errorf("Could not decode JSON response: %v", dErr) } - rootNode := "escalation_rule" + + const rootNode = "escalation_rule" + t, nodeOK := target[rootNode] if !nodeOK { return nil, fmt.Errorf("JSON response does not have %s field", rootNode) } + return &t, nil } func getEscalationPolicyFromResponse(c *Client, resp *http.Response, err error) (*EscalationPolicy, error) { - defer resp.Body.Close() if err != nil { return nil, err } + var target map[string]EscalationPolicy if dErr := c.decodeJSON(resp, &target); dErr != nil { return nil, fmt.Errorf("Could not decode JSON response: %v", dErr) } - rootNode := "escalation_policy" + + const rootNode = "escalation_policy" + t, nodeOK := target[rootNode] if !nodeOK { return nil, fmt.Errorf("JSON response does not have %s field", rootNode) } + return &t, nil }