// GetAlertAction retrieves specified alert details from OpsGenie.
func GetAlertAction(c *gcli.Context) {
	cli, err := NewAlertClient(c)
	if err != nil {
		os.Exit(1)
	}
	req := alerts.GetAlertRequest{}
	if val, success := getVal("id", c); success {
		req.ID = val
	}
	if val, success := getVal("alias", c); success {
		req.Alias = val
	}

	printVerboseMessage("Get alert request prepared from flags, sending request to OpsGenie..")

	resp, err := cli.Get(req)
	if err != nil {
		fmt.Printf("%s\n", err.Error())
		os.Exit(1)
	}

	outputFormat := strings.ToLower(c.String("output-format"))
	printVerboseMessage("Got Alert successfully, and will print as " + outputFormat)
	switch outputFormat {
	case "yaml":
		output, err := resultToYAML(resp)
		if err != nil {
			fmt.Printf("%s\n", err.Error())
			os.Exit(1)
		}
		fmt.Printf("%s\n", output)
	default:
		isPretty := c.IsSet("pretty")
		output, err := resultToJSON(resp, isPretty)
		if err != nil {
			fmt.Printf("%s\n", err.Error())
			os.Exit(1)
		}
		fmt.Printf("%s\n", output)
	}
}
// Get method retrieves specified alert details from OpsGenie.
func (cli *OpsGenieAlertClient) Get(req alerts.GetAlertRequest) (*alerts.GetAlertResponse, error) {
	req.APIKey = cli.apiKey
	resp, err := cli.sendRequest(cli.buildGetRequest(getAlertURL, req))

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

	var getAlertResp alerts.GetAlertResponse

	if err = resp.Body.FromJsonTo(&getAlertResp); err != nil {
		message := "Server response can not be parsed, " + err.Error()
		logging.Logger().Warn(message)
		return nil, errors.New(message)
	}
	return &getAlertResp, nil
}
func (cli *OpsGenieAlertClient) Get(req alerts.GetAlertRequest) (*alerts.GetAlertResponse, error) {
	req.ApiKey = cli.apiKey
	// validate the mandatory parameters: apiKey, id/alias/tinyId
	if req.ApiKey == "" {
		return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
	}
	if req.Id == "" && req.Alias == "" && req.TinyId == "" {
		return nil, errors.New("At least one of the parameters of id, alias and tiny id should be set.")
	}
	if (req.Id != "" && req.Alias != "") || (req.Id != "" && req.TinyId != "") || (req.Alias != "" && req.TinyId != "") {
		return nil, errors.New("Only one of the parameters of id, alias and tiny id should be set.")
	}
	v, _ := goquery.Values(req)
	var resp *goreq.Response
	var err error
	for i := 0; i < cli.retries; i++ {
		resp, err = cli.buildRequest("GET", GET_ALERT_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 getAlertResp alerts.GetAlertResponse
	// check if the response can be unmarshalled
	if err = resp.Body.FromJsonTo(&getAlertResp); err != nil {
		return nil, errors.New("Server response can not be parsed.")
	}
	return &getAlertResp, nil
}