Supporting json output (except for errors)
This commit is contained in:
@@ -1,10 +1,5 @@
|
||||
package responses
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
ERROR_GENERIC = iota * 10
|
||||
ERROR_MISSING_PARAMETER
|
||||
@@ -32,26 +27,9 @@ func init() {
|
||||
errors[ERROR_DATA_NOT_FOUND] = "The requested data was not found"
|
||||
}
|
||||
|
||||
type error struct {
|
||||
XMLName xml.Name `xml:"error"`
|
||||
Code int `xml:"code,attr"`
|
||||
Message string `xml:"message,attr"`
|
||||
}
|
||||
|
||||
func NewError(errorCode int, message ...interface{}) []byte {
|
||||
response := NewEmpty()
|
||||
response.Status = "fail"
|
||||
if errors[errorCode] == "" {
|
||||
errorCode = ERROR_GENERIC
|
||||
func ErrorMsg(code int) string {
|
||||
if v, found := errors[code]; found {
|
||||
return v
|
||||
}
|
||||
var msg string
|
||||
if (len(message) == 0) {
|
||||
msg = errors[errorCode]
|
||||
} else {
|
||||
msg = fmt.Sprintf(message[0].(string), message[1:len(message)]...)
|
||||
}
|
||||
xmlBody, _ := xml.Marshal(&error{Code: errorCode, Message: msg})
|
||||
response.Body = xmlBody
|
||||
xmlResponse, _ := xml.Marshal(response)
|
||||
return []byte(xml.Header + string(xmlResponse))
|
||||
return errors[ERROR_GENERIC]
|
||||
}
|
||||
+33
-27
@@ -3,47 +3,53 @@ package responses
|
||||
import "encoding/xml"
|
||||
|
||||
type Subsonic struct {
|
||||
XMLName xml.Name `xml:"http://subsonic.org/restapi subsonic-response"`
|
||||
Status string `xml:"status,attr"`
|
||||
Version string `xml:"version,attr"`
|
||||
Body []byte `xml:",innerxml"`
|
||||
License *License `xml:",omitempty"`
|
||||
MusicFolders *MusicFolders `xml:",omitempty"`
|
||||
ArtistIndex *ArtistIndex `xml:",omitempty"`
|
||||
XMLName xml.Name `xml:"http://subsonic.org/restapi subsonic-response" json:"-"`
|
||||
Status string `xml:"status,attr" json:"status"`
|
||||
Version string `xml:"version,attr" json:"version"`
|
||||
Error *Error `xml:",omitempty" json:"error,omitempty"`
|
||||
License *License `xml:",omitempty" json:"license,omitempty"`
|
||||
MusicFolders *MusicFolders `xml:",omitempty" json:"musicFolders,omitempty"`
|
||||
ArtistIndex *Indexes `xml:",omitempty" json:"indexes,omitempty"`
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
XMLName xml.Name `xml:"error" json:"-"`
|
||||
Code int `xml:"code,attr"`
|
||||
Message string `xml:"message,attr"`
|
||||
}
|
||||
|
||||
type License struct {
|
||||
XMLName xml.Name `xml:"license"`
|
||||
Valid bool `xml:"valid,attr"`
|
||||
XMLName xml.Name `xml:"license" json:"-" json:"-"`
|
||||
Valid bool `xml:"valid,attr" json:"valid"`
|
||||
}
|
||||
|
||||
type MusicFolder struct {
|
||||
XMLName xml.Name `xml:"musicFolder"`
|
||||
Id string `xml:"id,attr"`
|
||||
Name string `xml:"name,attr"`
|
||||
XMLName xml.Name `xml:"musicFolder" json:"-"`
|
||||
Id string `xml:"id,attr" json:"id"`
|
||||
Name string `xml:"name,attr" json:"name"`
|
||||
}
|
||||
|
||||
type MusicFolders struct {
|
||||
XMLName xml.Name `xml:"musicFolders"`
|
||||
Folders []MusicFolder `xml:"musicFolders"`
|
||||
XMLName xml.Name `xml:"musicFolders" json:"-"`
|
||||
Folders []MusicFolder `xml:"musicFolders" json:"musicFolder"`
|
||||
}
|
||||
|
||||
type IdxArtist struct {
|
||||
XMLName xml.Name `xml:"artist"`
|
||||
Id string `xml:"id,attr"`
|
||||
Name string `xml:"name,attr"`
|
||||
type Artist struct {
|
||||
XMLName xml.Name `xml:"artist" json:"-"`
|
||||
Id string `xml:"id,attr" json:"id"`
|
||||
Name string `xml:"name,attr" json:"name"`
|
||||
}
|
||||
|
||||
type IdxIndex struct {
|
||||
XMLName xml.Name `xml:"index"`
|
||||
Name string `xml:"name,attr"`
|
||||
Artists []IdxArtist `xml:"index"`
|
||||
type Index struct {
|
||||
XMLName xml.Name `xml:"index" json:"-"`
|
||||
Name string `xml:"name,attr" json:"name"`
|
||||
Artists []Artist `xml:"index" json:"artist"`
|
||||
}
|
||||
|
||||
type ArtistIndex struct {
|
||||
XMLName xml.Name `xml:"indexes"`
|
||||
Index []IdxIndex `xml:"indexes"`
|
||||
LastModified string `xml:"lastModified,attr"`
|
||||
IgnoredArticles string `xml:"ignoredArticles,attr"`
|
||||
type Indexes struct {
|
||||
XMLName xml.Name `xml:"indexes" json:"-"`
|
||||
Index []Index `xml:"indexes" json:"index"`
|
||||
LastModified string `xml:"lastModified,attr" json:"lastModified"`
|
||||
IgnoredArticles string `xml:"ignoredArticles,attr" json:"ignoredArticles"`
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package responses
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
func NewEmpty() Subsonic {
|
||||
return Subsonic{Status: "ok", Version: beego.AppConfig.String("apiVersion")}
|
||||
}
|
||||
|
||||
func ToXML(response Subsonic) []byte {
|
||||
xmlBody, _ := xml.Marshal(response)
|
||||
return []byte(xml.Header + string(xmlBody))
|
||||
}
|
||||
Reference in New Issue
Block a user