package utils

import (
	"encoding/xml"
	"net/http"

	"github.com/fiskerinc/cloud-services/pkg/common"
)

// ErrorXMLResult makes error result
func ErrorXMLResult(status int, message string) common.XMLError {
	return common.XMLError{
		Error:   http.StatusText(status),
		Message: message,
	}
}

// RespXML sends back XML response.
func RespXML(w http.ResponseWriter, status int, resp interface{}) {
	js, _ := xml.Marshal(resp)
	w.Header().Set("Content-Type", "application/xml")

	w.WriteHeader(status)
	w.Write(js)
}

// RespXMLError XML error response
func RespXMLError(w http.ResponseWriter, status int, message string) {
	resp := ErrorXMLResult(status, message)
	RespXML(w, status, &resp)
}
