// Update Heartbeat is used to change configuration of existing heartbeats.
// Mandatory Parameters:
// 	- id: 		Id of the heartbeat
// 	- apiKey: 	API key is used for authenticating API requests
// Optional Parameters
// 	- name: 			Name of the heartbeat
// 	- interval: 		Specifies how often a heartbeat message should be expected.
// 	- intervalUnit: 	interval specified as minutes, hours or days
// 	- description:	 	An optional description of the heartbeat
// 	- enabled: 			Enable/disable heartbeat monitoring
func (cli *OpsGenieHeartbeatClient) Update(req heartbeat.UpdateHeartbeatRequest) (*heartbeat.UpdateHeartbeatResponse, error) {
	req.ApiKey = cli.apiKey
	// validate mandatory fields: apiKey, id
	if req.ApiKey == "" {
		return nil, errors.New("Api Key is a mandatory field and can not be empty")
	}
	if req.Id == "" {
		return nil, errors.New("Id 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", UPDATE_HEARTBEAT_URL, req).Do()
		if err == nil {
			break
		}
		time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
	}
	if err != nil {
		return nil, errors.New("Can not update the heartbeat, 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 updateHeartbeatResp heartbeat.UpdateHeartbeatResponse
	if err = resp.Body.FromJsonTo(&updateHeartbeatResp); err != nil {
		return nil, errors.New("Server response can not be parsed")
	}
	// parsed successfuly with no errors
	return &updateHeartbeatResp, nil
}