Ejemplo n.º 1
0
func (cli *OpsGenieAlertClient) List(req alerts.ListAlertsRequest) (*alerts.ListAlertsResponse, error) {
	req.ApiKey = cli.apiKey
	// validate the mandatory parameter: apiKey
	if req.ApiKey == "" {
		return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
	}
	v, _ := goquery.Values(req)
	var resp *goreq.Response
	var err error
	for i := 0; i < cli.retries; i++ {
		resp, err = cli.buildRequest("GET", LIST_ALERTS_URL+"?"+v.Encode(), nil).Do()
		if err == nil {
			break
		}
		time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
	}
	if err != nil {
		return nil, errors.New("Could not retrieve the alert: a problem occured while sending 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 listAlertsResp alerts.ListAlertsResponse
	// check if the response can be unmarshalled
	if err = resp.Body.FromJsonTo(&listAlertsResp); err != nil {
		return nil, errors.New("Server response can not be parsed.")
	}
	return &listAlertsResp, nil
}
Ejemplo n.º 2
0
// List method retrieves alerts from OpsGenie.
func (cli *OpsGenieAlertClient) List(req alerts.ListAlertsRequest) (*alerts.ListAlertsResponse, error) {
	req.APIKey = cli.apiKey
	resp, err := cli.sendRequest(cli.buildGetRequest(listAlertsURL, req))

	if resp == nil {
		return nil, errors.New(err.Error())
	}
	defer resp.Body.Close()

	var listAlertsResp alerts.ListAlertsResponse

	if err = resp.Body.FromJsonTo(&listAlertsResp); err != nil {
		message := "Server response can not be parsed, " + err.Error()
		logging.Logger().Warn(message)
		return nil, errors.New(message)
	}
	return &listAlertsResp, nil
}