Refactored responses

This commit is contained in:
Deluan 2016-03-02 11:11:46 -05:00
parent 77e3aa7620
commit 01c68d6802
9 changed files with 64 additions and 69 deletions

View file

@ -30,7 +30,7 @@ func (c *GetIndexesController) Get() {
ifModifiedSince = "0" ifModifiedSince = "0"
} }
res := &responses.ArtistIndex{} res := responses.ArtistIndex{}
res.IgnoredArticles = beego.AppConfig.String("ignoredArticles") res.IgnoredArticles = beego.AppConfig.String("ignoredArticles")
res.LastModified, err = c.properties.DefaultGet(consts.LastScan, "-1") res.LastModified, err = c.properties.DefaultGet(consts.LastScan, "-1")
@ -61,5 +61,7 @@ func (c *GetIndexesController) Get() {
} }
c.Ctx.Output.Body(responses.NewXML(res)) response := responses.NewEmpty()
response.ArtistIndex = res
c.Ctx.Output.Body(responses.ToXML(response))
} }

View file

@ -8,6 +8,8 @@ import (
type GetLicenseController struct{ beego.Controller } type GetLicenseController struct{ beego.Controller }
func (c *GetLicenseController) Get() { func (c *GetLicenseController) Get() {
response := responses.NewXML(&responses.License{Valid: true}) response := responses.NewEmpty()
c.Ctx.Output.Body(response) response.License = responses.License{Valid: true}
c.Ctx.Output.Body(responses.ToXML(response))
} }

View file

@ -24,7 +24,7 @@ func (c *GetMusicFoldersController) Get() {
folders[i].Id = f.Id folders[i].Id = f.Id
folders[i].Name = f.Name folders[i].Name = f.Name
} }
musicFolders := &responses.MusicFolders{Folders: folders} response := responses.NewEmpty()
response := responses.NewXML(musicFolders) response.MusicFolders = responses.MusicFolders{Folders: folders}
c.Ctx.Output.Body(response) c.Ctx.Output.Body(responses.ToXML(response))
} }

View file

@ -1,7 +1,6 @@
package api package api
import ( import (
"encoding/xml"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/deluan/gosonic/api/responses" "github.com/deluan/gosonic/api/responses"
) )
@ -9,7 +8,5 @@ import (
type PingController struct{ beego.Controller } type PingController struct{ beego.Controller }
func (c *PingController) Get() { func (c *PingController) Get() {
response := responses.NewEmpty() c.Ctx.Output.Body(responses.ToXML(responses.NewEmpty()))
xmlBody, _ := xml.Marshal(response)
c.Ctx.Output.Body([]byte(xml.Header + string(xmlBody)))
} }

View file

@ -1,23 +0,0 @@
package responses
import "encoding/xml"
type IdxArtist struct {
XMLName xml.Name `xml:"artist"`
Id string `xml:"id,attr"`
Name string `xml:"name,attr"`
}
type IdxIndex struct {
XMLName xml.Name `xml:"index"`
Name string `xml:"name,attr"`
Artists []IdxArtist `xml:"index"`
}
type ArtistIndex struct {
XMLName xml.Name `xml:"indexes"`
Index []IdxIndex `xml:"indexes"`
LastModified string `xml:"lastModified,attr"`
IgnoredArticles string `xml:"ignoredArticles,attr"`
}

View file

@ -1,8 +0,0 @@
package responses
import "encoding/xml"
type License struct {
XMLName xml.Name `xml:"license"`
Valid bool `xml:"valid,attr"`
}

View file

@ -1,14 +0,0 @@
package responses
import "encoding/xml"
type MusicFolder struct {
XMLName xml.Name `xml:"musicFolder"`
Id string `xml:"id,attr"`
Name string `xml:"name,attr"`
}
type MusicFolders struct {
XMLName xml.Name `xml:"musicFolders"`
Folders []MusicFolder `xml:"musicFolders"`
}

View file

@ -0,0 +1,49 @@
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"`
}
type License struct {
XMLName xml.Name `xml:"license"`
Valid bool `xml:"valid,attr"`
}
type MusicFolder struct {
XMLName xml.Name `xml:"musicFolder"`
Id string `xml:"id,attr"`
Name string `xml:"name,attr"`
}
type MusicFolders struct {
XMLName xml.Name `xml:"musicFolders"`
Folders []MusicFolder `xml:"musicFolders"`
}
type IdxArtist struct {
XMLName xml.Name `xml:"artist"`
Id string `xml:"id,attr"`
Name string `xml:"name,attr"`
}
type IdxIndex struct {
XMLName xml.Name `xml:"index"`
Name string `xml:"name,attr"`
Artists []IdxArtist `xml:"index"`
}
type ArtistIndex struct {
XMLName xml.Name `xml:"indexes"`
Index []IdxIndex `xml:"indexes"`
LastModified string `xml:"lastModified,attr"`
IgnoredArticles string `xml:"ignoredArticles,attr"`
}

View file

@ -5,21 +5,11 @@ import (
"github.com/astaxie/beego" "github.com/astaxie/beego"
) )
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"`
}
func NewEmpty() Subsonic { func NewEmpty() Subsonic {
return Subsonic{Status: "ok", Version: beego.AppConfig.String("apiVersion")} return Subsonic{Status: "ok", Version: beego.AppConfig.String("apiVersion")}
} }
func NewXML(body interface{}) []byte { func ToXML(response Subsonic) []byte {
response := NewEmpty() xmlBody, _ := xml.Marshal(response)
xmlBody, _ := xml.Marshal(body) return []byte(xml.Header + string(xmlBody))
response.Body = xmlBody
xmlResponse, _ := xml.Marshal(response)
return []byte(xml.Header + string(xmlResponse))
} }