This commit is contained in:
Deluan
2016-03-02 13:18:39 -05:00
parent 9d41f5a39f
commit 4843ccb46c
42 changed files with 150 additions and 160 deletions
+10 -7
View File
@@ -1,10 +1,10 @@
package api
import (
"encoding/xml"
"fmt"
"github.com/astaxie/beego"
"github.com/deluan/gosonic/api/responses"
"fmt"
"encoding/xml"
)
type BaseAPIController struct{ beego.Controller }
@@ -16,7 +16,7 @@ func (c *BaseAPIController) NewEmpty() responses.Subsonic {
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) {
if len(message) == 0 {
msg = responses.ErrorMsg(errorCode)
} else {
msg = fmt.Sprintf(message[0].(string), message[1:len(message)]...)
@@ -24,13 +24,15 @@ func (c *BaseAPIController) SendError(errorCode int, message ...interface{}) {
response.Error = &responses.Error{Code: errorCode, Message: msg}
xmlBody, _ := xml.Marshal(&response)
c.CustomAbort(200, xml.Header + string(xmlBody))
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"` }
type jsonWrapper struct {
Subsonic responses.Subsonic `json:"subsonic-response"`
}
return jsonWrapper{Subsonic: response}
}
return response
@@ -39,7 +41,9 @@ func (c *BaseAPIController) prepResponse(response responses.Subsonic) interface{
func (c *BaseAPIController) SendResponse(response responses.Subsonic) {
f := c.GetString("f")
if f == "json" {
type jsonWrapper struct{ Subsonic responses.Subsonic `json:"subsonic-response"` }
type jsonWrapper struct {
Subsonic responses.Subsonic `json:"subsonic-response"`
}
w := &jsonWrapper{Subsonic: response}
c.Data["json"] = &w
c.ServeJSON()
@@ -48,4 +52,3 @@ func (c *BaseAPIController) SendResponse(response responses.Subsonic) {
c.ServeXML()
}
}