package webutil import ( "encoding/json" "encoding/xml" "fmt" "net/http" "strings" "os" "github.rackspace.com/gorax/libs/nsiuuid" ) // generic response writter for APIs type Response map[string]interface{} // Error represents a handler error. It provides methods for a HTTP status code and embeds the built-in error interface. // Deprecated: Use in new location of github.rackspace.com/gorax/webutil type Error interface { error Status() int } // StatusError represents an error with an associated HTTP status code. // Deprecated: Use in new location of github.rackspace.com/gorax/webutil type StatusError struct { Code int Err error } // Allows StatusError to satisfy the error interface. // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func (s StatusError) Error() string { return s.Err.Error() } // Returns our HTTP status code. // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func (s StatusError) Status() int { return s.Code } //This method returns a xml marshaled response // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func (r Response) XML() string { b, err := xml.MarshalIndent(r, "", " ") if err != nil { return "" } return strings.Replace(string(b), "%", "%%", -1) } // XMLWithHeader will take a structure and xml marshal with the header prepended to the XML request. // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func XMLMarshalHead(r interface{}) string { rv := []byte(xml.Header) b, err := xml.MarshalIndent(r, "", " ") if err != nil { return "" } rv = append(rv, b...) return strings.Replace(string(rv), "\\\"", "\"", -1) } //This method returns a json marshaled response // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func (r Response) String() string { b, err := json.Marshal(r) if err != nil { return "" } return strings.Replace(string(b), "%", "%%", -1) } //returns the resp map as a json string with the error code provided // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func ErrHandler(w http.ResponseWriter, r *http.Request, resp Response, code int) { resp["api_ver"] = os.Getenv("API_VER") resp["connection_id"] = nsiuuid.FromContext(r.Context()) http.Error(w, resp.String(), code) return } // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func JSONErrHandler(w http.ResponseWriter, r *http.Request, resp Response, code int) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(code) uid := nsiuuid.FromContext(r.Context()) resp["api_ver"] = os.Getenv("API_VER") resp["connection_id"] = uid fmt.Fprintln(w, resp) } //returns the resp map as a json string with a 200 OK // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func ResHandler(w http.ResponseWriter, r *http.Request, resp Response) { w.Header().Set("Content-Type", "application/json") uid := nsiuuid.FromContext(r.Context()) resp["api_ver"] = os.Getenv("API_VER") resp["connection_id"] = uid fmt.Fprintf(w, resp.String()) } //returns the resp map as a json string with a given success code // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func ResCodeHandler(w http.ResponseWriter, r *http.Request, resp interface{}, code int) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(code) uid := nsiuuid.FromContext(r.Context()) if stringType, ok := resp.(string); ok { fmt.Fprintf(w, stringType) } else if respType, ok := resp.(Response); ok { respType["api_ver"] = os.Getenv("API_VER") respType["connection_id"] = uid fmt.Fprintf(w, respType.String()) } } //returns the resp map as a json string with a given success code // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func HealthCheckHandler(w http.ResponseWriter) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) resp := Response{"success": true, "data": "API Alive", "api_ver": os.Getenv("API_VER")} fmt.Fprintf(w, resp.String()) } //returns the resp map as a xml string with the error code provided // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func XMLErrHandler(w http.ResponseWriter, r *http.Request, resp Response, code int) { w.Header().Set("Content-Type", "application/xml") uid := nsiuuid.FromContext(r.Context()) resp["api_ver"] = os.Getenv("API_VER") resp["connection_id"] = uid http.Error(w, resp.XML(), code) return } //returns the resp map as a xml string with a 200 OK // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func XMLResHandler(w http.ResponseWriter, r *http.Request, resp Response) { w.Header().Set("Content-Type", "application/xml") uid := nsiuuid.FromContext(r.Context()) resp["api_ver"] = os.Getenv("API_VER") resp["connection_id"] = uid fmt.Fprintf(w, resp.XML()) return } var allowHeaders = []string{ "Content-Type", "apikey", "X-Auth", "X-Auth-Token", "Accept", "Cache-Control", } // WebResHandler returns the resp map as a json string with a given success code // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func WebResHandler(w http.ResponseWriter, r *http.Request, resp interface{}, code int) { w.Header().Set("Content-Type", "application/json") w.Header().Set("Access-Control-Allow-Headers", strings.Join(allowHeaders, ", ")) w.Header().Set("Access-Control-Allow-Origin", "*") w.WriteHeader(code) uid := nsiuuid.FromContext(r.Context()) if stringType, ok := resp.(string); ok { fmt.Fprintf(w, stringType) } else if respType, ok := resp.(Response); ok { respType["api_ver"] = os.Getenv("API_VER") respType["connection_id"] = uid fmt.Fprintf(w, respType.String()) } } // WebOptionsHandler returns the resp map as a json string with a given success code // Deprecated: Use in new location of github.rackspace.com/gorax/webutil func WebOptionsHandler(w http.ResponseWriter, options []string) { w.Header().Set("Content-Type", "application/json") w.Header().Set("Access-Control-Allow-Headers", strings.Join(allowHeaders, ", ")) w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Allow", strings.Join(options, ", ")) w.WriteHeader(http.StatusOK) fmt.Fprintf(w, Response{}.String()) }