navidrome/api/base_api_controller.go

52 lines
1.4 KiB
Go
Raw Normal View History

package api
import (
"github.com/astaxie/beego"
"github.com/deluan/gosonic/api/responses"
"fmt"
"encoding/xml"
)
type BaseAPIController struct{ beego.Controller }
func (c *BaseAPIController) NewEmpty() responses.Subsonic {
return responses.Subsonic{Status: "ok", Version: beego.AppConfig.String("apiVersion")}
}
func (c *BaseAPIController) SendError(errorCode int, message ...interface{}) {
response := responses.Subsonic{Version: beego.AppConfig.String("apiVersion"), Status: "fail"}
var msg string
if (len(message) == 0) {
msg = responses.ErrorMsg(errorCode)
} else {
msg = fmt.Sprintf(message[0].(string), message[1:len(message)]...)
}
response.Error = &responses.Error{Code: errorCode, Message: msg}
xmlBody, _ := xml.Marshal(&response)
c.CustomAbort(200, xml.Header + string(xmlBody))
}
func (c *BaseAPIController) prepResponse(response responses.Subsonic) interface{} {
f := c.GetString("f")
if f == "json" {
type jsonWrapper struct{ Subsonic responses.Subsonic `json:"subsonic-response"` }
return jsonWrapper{Subsonic: response}
}
return response
}
func (c *BaseAPIController) SendResponse(response responses.Subsonic) {
f := c.GetString("f")
if f == "json" {
type jsonWrapper struct{ Subsonic responses.Subsonic `json:"subsonic-response"` }
w := &jsonWrapper{Subsonic: response}
c.Data["json"] = &w
c.ServeJSON()
} else {
c.Data["xml"] = &response
c.ServeXML()
}
}