// ListRecipients method retrieves recipients of an alert from OpsGenie. func (cli *OpsGenieAlertClient) ListRecipients(req alerts.ListAlertRecipientsRequest) (*alerts.ListAlertRecipientsResponse, error) { req.APIKey = cli.apiKey resp, err := cli.sendRequest(cli.buildGetRequest(listAlertRecipientsURL, req)) if resp == nil { return nil, err } defer resp.Body.Close() var listAlertRecipientsResp alerts.ListAlertRecipientsResponse if err = resp.Body.FromJsonTo(&listAlertRecipientsResp); err != nil { message := "Server response can not be parsed, " + err.Error() logging.Logger().Warn(message) return nil, errors.New(message) } return &listAlertRecipientsResp, nil }
func (cli *OpsGenieAlertClient) ListRecipients(req alerts.ListAlertRecipientsRequest) (*alerts.ListAlertRecipientsResponse, error) { req.ApiKey = cli.apiKey // validate the mandatory parameters: apiKey, id/alias if req.ApiKey == "" { return nil, errors.New("ApiKey is a mandatory field and can not be empty.") } if req.Id == "" && req.Alias == "" { return nil, errors.New("At least either Id or Alias should be set in the request.") } if req.Id != "" && req.Alias != "" { return nil, errors.New("Either Id or Alias should be set in the request not both.") } v, _ := goquery.Values(req) var resp *goreq.Response var err error for i := 0; i < cli.retries; i++ { resp, err = cli.buildRequest("GET", LIST_ALERT_RECIPIENTS_URL+"?"+v.Encode(), nil).Do() if err == nil { break } time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS) } if err != nil { return nil, errors.New("Can not list the recipient list, unable to send the request") } // check the returning HTTP status code httpStatusCode := resp.StatusCode if httpStatusCode >= 400 && httpStatusCode < 500 { return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode)) } if httpStatusCode >= 500 { return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode)) } var listAlertRecipientsResp alerts.ListAlertRecipientsResponse // check if the response can be unmarshalled if err = resp.Body.FromJsonTo(&listAlertRecipientsResp); err != nil { return nil, errors.New("Server response can not be parsed.") } return &listAlertRecipientsResp, nil }