Skip to content

Commit

Permalink
Update change_events.go to accept a context.Context
Browse files Browse the repository at this point in the history
Updates #267
  • Loading branch information
theckman committed Feb 26, 2021
1 parent 1a0c44e commit addcf57
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions change_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,41 @@ type ChangeEventResponse struct {
// CreateChangeEvent Sends PagerDuty a single ChangeEvent to record
// The v2EventsAPIEndpoint parameter must be set on the client
// Documentation can be found at https://developer.pagerduty.com/docs/events-api-v2/send-change-events
//
// It's recommended to use CreateChangeEventWithContext instead.
func (c *Client) CreateChangeEvent(e ChangeEvent) (*ChangeEventResponse, error) {
return c.CreateChangeEventWithContext(context.Background(), e)
}

// CreateChangeEventWithContext sends PagerDuty a single ChangeEvent to record
// The v2EventsAPIEndpoint parameter must be set on the client Documentation can
// be found at https://developer.pagerduty.com/docs/events-api-v2/send-change-events
func (c *Client) CreateChangeEventWithContext(ctx context.Context, e ChangeEvent) (*ChangeEventResponse, error) {
if c.v2EventsAPIEndpoint == "" {
return nil, errors.New("v2EventsAPIEndpoint field must be set on Client")
}

headers := make(map[string]string)

data, err := json.Marshal(e)
if err != nil {
return nil, err
}

resp, err := c.doWithEndpoint(
context.TODO(),
ctx,
c.v2EventsAPIEndpoint,
http.MethodPost,
changeEventPath,
false,
bytes.NewBuffer(data),
headers,
nil,
)
if err != nil {
return nil, err
}

var eventResponse ChangeEventResponse
if err := json.NewDecoder(resp.Body).Decode(&eventResponse); err != nil {

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

Expand Down

0 comments on commit addcf57

Please sign in to comment.