func (cli *OpsGenieHeartbeatClient) Add(req heartbeat.AddHeartbeatRequest) (*heartbeat.AddHeartbeatResponse, error) { req.ApiKey = cli.apiKey // validate mandatory fields: apiKey, name if req.ApiKey == "" { return nil, errors.New("Api Key is a mandatory field and can not be empty") } if req.Name == "" { return nil, errors.New("Heart beat name is a mandatory field and can not be empty") } // send the request resp, err := cli.buildRequest("POST", ADD_HEARTBEAT_URL, req).Do() // resp, err := goreq.Request{ Method: "POST", Uri: ADD_HEARTBEAT_URL, Body: req, }.Do() if err != nil { return nil, errors.New("Can not add a new heart beat, unable to send the request") } // check for the returning http status, 4xx: client errors, 5xx: server errors statusCode := resp.StatusCode if statusCode >= 400 && statusCode < 500 { return nil, errors.New(fmt.Sprintf("Client error %d occured", statusCode)) } if statusCode >= 500 { return nil, errors.New(fmt.Sprintf("Server error %d occured", statusCode)) } // try to parse the returning JSON into the response var addHeartbeatResp heartbeat.AddHeartbeatResponse if err = resp.Body.FromJsonTo(&addHeartbeatResp); err != nil { return nil, errors.New("Server response can not be parsed") } // parsed successfuly with no errors return &addHeartbeatResp, nil }
// Add method creates a heartbeat at OpsGenie. func (cli *OpsGenieHeartbeatClient) Add(req heartbeat.AddHeartbeatRequest) (*heartbeat.AddHeartbeatResponse, error) { req.APIKey = cli.apiKey resp, err := cli.sendRequest(cli.buildPostRequest(addHeartbeatURL, req)) if resp == nil { return nil, err } defer resp.Body.Close() var addHeartbeatResp heartbeat.AddHeartbeatResponse if err = resp.Body.FromJsonTo(&addHeartbeatResp); err != nil { message := "Server response can not be parsed, " + err.Error() logging.Logger().Warn(message) return nil, errors.New(message) } return &addHeartbeatResp, nil }