From 01c68d68028082b0dc5c8fffb6848ecb5b6bd29e Mon Sep 17 00:00:00 2001 From: Deluan Date: Wed, 2 Mar 2016 11:11:46 -0500 Subject: [PATCH] Refactored responses --- api/get_indexes.go | 6 +++-- api/get_license.go | 6 +++-- api/get_music_folders.go | 6 ++--- api/ping.go | 5 +--- api/responses/indexes.go | 23 ---------------- api/responses/license.go | 8 ------ api/responses/music_folders.go | 14 ---------- api/responses/responses.go | 49 ++++++++++++++++++++++++++++++++++ api/responses/subsonic.go | 16 +++-------- 9 files changed, 64 insertions(+), 69 deletions(-) delete mode 100644 api/responses/indexes.go delete mode 100644 api/responses/license.go delete mode 100644 api/responses/music_folders.go create mode 100644 api/responses/responses.go diff --git a/api/get_indexes.go b/api/get_indexes.go index 2b58db427..02b062c5a 100644 --- a/api/get_indexes.go +++ b/api/get_indexes.go @@ -30,7 +30,7 @@ func (c *GetIndexesController) Get() { ifModifiedSince = "0" } - res := &responses.ArtistIndex{} + res := responses.ArtistIndex{} res.IgnoredArticles = beego.AppConfig.String("ignoredArticles") 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)) } diff --git a/api/get_license.go b/api/get_license.go index 6db79dd56..088cf2640 100644 --- a/api/get_license.go +++ b/api/get_license.go @@ -8,6 +8,8 @@ import ( type GetLicenseController struct{ beego.Controller } func (c *GetLicenseController) Get() { - response := responses.NewXML(&responses.License{Valid: true}) - c.Ctx.Output.Body(response) + response := responses.NewEmpty() + response.License = responses.License{Valid: true} + + c.Ctx.Output.Body(responses.ToXML(response)) } diff --git a/api/get_music_folders.go b/api/get_music_folders.go index ee9e101f4..e976ca643 100644 --- a/api/get_music_folders.go +++ b/api/get_music_folders.go @@ -24,7 +24,7 @@ func (c *GetMusicFoldersController) Get() { folders[i].Id = f.Id folders[i].Name = f.Name } - musicFolders := &responses.MusicFolders{Folders: folders} - response := responses.NewXML(musicFolders) - c.Ctx.Output.Body(response) + response := responses.NewEmpty() + response.MusicFolders = responses.MusicFolders{Folders: folders} + c.Ctx.Output.Body(responses.ToXML(response)) } diff --git a/api/ping.go b/api/ping.go index 3207deb75..772a9da2e 100644 --- a/api/ping.go +++ b/api/ping.go @@ -1,7 +1,6 @@ package api import ( - "encoding/xml" "github.com/astaxie/beego" "github.com/deluan/gosonic/api/responses" ) @@ -9,7 +8,5 @@ import ( type PingController struct{ beego.Controller } func (c *PingController) Get() { - response := responses.NewEmpty() - xmlBody, _ := xml.Marshal(response) - c.Ctx.Output.Body([]byte(xml.Header + string(xmlBody))) + c.Ctx.Output.Body(responses.ToXML(responses.NewEmpty())) } diff --git a/api/responses/indexes.go b/api/responses/indexes.go deleted file mode 100644 index bfefa8ab6..000000000 --- a/api/responses/indexes.go +++ /dev/null @@ -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"` -} - diff --git a/api/responses/license.go b/api/responses/license.go deleted file mode 100644 index 4920c1da1..000000000 --- a/api/responses/license.go +++ /dev/null @@ -1,8 +0,0 @@ -package responses - -import "encoding/xml" - -type License struct { - XMLName xml.Name `xml:"license"` - Valid bool `xml:"valid,attr"` -} diff --git a/api/responses/music_folders.go b/api/responses/music_folders.go deleted file mode 100644 index 2c7bbf63a..000000000 --- a/api/responses/music_folders.go +++ /dev/null @@ -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"` -} diff --git a/api/responses/responses.go b/api/responses/responses.go new file mode 100644 index 000000000..82cfd841c --- /dev/null +++ b/api/responses/responses.go @@ -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"` +} + diff --git a/api/responses/subsonic.go b/api/responses/subsonic.go index 1a03b42be..2d651a515 100644 --- a/api/responses/subsonic.go +++ b/api/responses/subsonic.go @@ -5,21 +5,11 @@ import ( "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 { return Subsonic{Status: "ok", Version: beego.AppConfig.String("apiVersion")} } -func NewXML(body interface{}) []byte { - response := NewEmpty() - xmlBody, _ := xml.Marshal(body) - response.Body = xmlBody - xmlResponse, _ := xml.Marshal(response) - return []byte(xml.Header + string(xmlResponse)) +func ToXML(response Subsonic) []byte { + xmlBody, _ := xml.Marshal(response) + return []byte(xml.Header + string(xmlBody)) }