func (cli *OpsGenieAlertClient) Create(req alerts.CreateAlertRequest) (*alerts.CreateAlertResponse, error) { req.ApiKey = cli.apiKey // validate the mandatory parameters: apiKey, message if req.ApiKey == "" { return nil, errors.New("ApiKey is a mandatory field and can not be empty.") } if req.Message == "" { return nil, errors.New("Message is a mandatory field and can not be empty.") } // send the request var resp *goreq.Response var err error for i := 0; i < cli.retries; i++ { resp, err = cli.buildRequest("POST", CREATE_ALERT_URL, req).Do() if err == nil { break } // sleep for a second time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS) } if err != nil { return nil, errors.New("Could not create 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 createAlertResp alerts.CreateAlertResponse // check if the response can be unmarshalled if err = resp.Body.FromJsonTo(&createAlertResp); err != nil { return nil, errors.New("Server response can not be parsed") } return &createAlertResp, nil }