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

Update event_v2.go to accept a context.Context #290

Merged
merged 1 commit into from
Mar 10, 2021
Merged
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
35 changes: 27 additions & 8 deletions event_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ type V2EventResponse struct {

const v2eventEndPoint = "https://events.pagerduty.com/v2/enqueue"

// ManageEvent handles the trigger, acknowledge, and resolve methods for an event
// ManageEvent handles the trigger, acknowledge, and resolve methods for an
// event. It's recommended to use ManageEventWithContext instead.
func ManageEvent(e V2Event) (*V2EventResponse, error) {
return ManageEventWithContext(context.Background(), e)
}

// ManageEventWithContext handles the trigger, acknowledge, and resolve methods for an event.
func ManageEventWithContext(ctx context.Context, e V2Event) (*V2EventResponse, error) {
data, err := json.Marshal(e)
if err != nil {
return nil, err
}

req, err := http.NewRequestWithContext(context.TODO(), http.MethodPost, v2eventEndPoint, bytes.NewBuffer(data))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, v2eventEndPoint, bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}
Expand All @@ -63,35 +69,48 @@ func ManageEvent(e V2Event) (*V2EventResponse, error) {
if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusAccepted {
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("HTTP Status Code: %d", resp.StatusCode)
}

return nil, fmt.Errorf("HTTP Status Code: %d, Message: %s", resp.StatusCode, string(bytes))
}

var eventResponse V2EventResponse
if err := json.NewDecoder(resp.Body).Decode(&eventResponse); err != nil {
return nil, err
}

return &eventResponse, nil
}

// ManageEvent handles the trigger, acknowledge, and resolve methods for an event
// ManageEvent handles the trigger, acknowledge, and resolve methods for an
// event. It's recommended to use ManageEventWithContext instead.
func (c *Client) ManageEvent(e *V2Event) (*V2EventResponse, error) {
headers := make(map[string]string)
return c.ManageEventWithContext(context.Background(), e)
}

// ManageEventWithContext handles the trigger, acknowledge, and resolve methods for an event.
func (c *Client) ManageEventWithContext(ctx context.Context, e *V2Event) (*V2EventResponse, error) {
data, err := json.Marshal(e)
if err != nil {
return nil, err
}

resp, err := c.doWithEndpoint(context.TODO(), c.v2EventsAPIEndpoint, http.MethodPost, "/v2/enqueue", false, bytes.NewBuffer(data), headers)
resp, err := c.doWithEndpoint(ctx, c.v2EventsAPIEndpoint, http.MethodPost, "/v2/enqueue", false, bytes.NewBuffer(data), nil)
if err != nil {
return nil, err
}
result := &V2EventResponse{}
err = json.NewDecoder(resp.Body).Decode(result)
return result, err

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

return &result, err
}