aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--marshall_test.go11
-rw-r--r--modern.go19
2 files changed, 19 insertions, 11 deletions
diff --git a/marshall_test.go b/marshall_test.go
index b5e9fae..ad4d6fd 100644
--- a/marshall_test.go
+++ b/marshall_test.go
@@ -15,13 +15,15 @@ func TestMarshallXML(t *testing.T) {
Time: 1479682785,
Album: Correctable{Name: "Mellow Gold"}},
}
- fmt.Printf("%v\n", s)
scrobbles := Scrobbles{Scrobbles: s,
Attrs: Attrs{1, 0}}
- output, err := xml.MarshalIndent(scrobbles, " ", " ")
+ response := LFMResponse{Status: "ok",
+ Response: scrobbles}
+ output, err := xml.MarshalIndent(response, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
+ fmt.Printf(xml.Header)
os.Stdout.Write(output)
}
@@ -34,8 +36,9 @@ func TestMarshallJSON(t *testing.T) {
}
scrobbles := Scrobbles{Scrobbles: s,
Attrs: Attrs{1, 0}}
- jsonscrobble := map[string]Scrobbles{"scrobbles": scrobbles}
- output, err := json.MarshalIndent(jsonscrobble, " ", " ")
+ response := LFMResponse{Status: "ok",
+ Response: scrobbles}
+ output, err := json.MarshalIndent(response, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
diff --git a/modern.go b/modern.go
index 946335d..9106123 100644
--- a/modern.go
+++ b/modern.go
@@ -27,6 +27,12 @@ type Token struct {
string
}
+type LFMResponse struct {
+ XMLName xml.Name `xml:"lfm" json:"-"`
+ Status string `xml:"status,attr" json:"-"`
+ Response ApiResponse `json:"scrobbles"`
+}
+
type Attrs struct {
Accepted int `xml:"accepted,attr" json:"accepted"`
Ignored int `xml:"ignored,attr" json:"ignored"`
@@ -49,8 +55,7 @@ func (field Correctable) Value() (driver.Value, error) {
func NewCorrectable(name string) Correctable {
return Correctable{
- Corrected: 0,
- Name: name,
+ Name: name,
}
}
@@ -98,20 +103,19 @@ func (store *SqlStore) TrackScrobble(r *http.Request) (ApiResponse, error) {
func ApiHandler(ds DataStore, w http.ResponseWriter, r *http.Request) {
method := r.FormValue("method")
- var response ApiResponse
+ response := LFMResponse{Status: "ok"}
switch method {
case "auth.getToken":
- response = ds.AuthGetToken(r)
+ response.Response = ds.AuthGetToken(r)
case "auth.getSession":
- response = ds.AuthGetSession(r)
+ response.Response = ds.AuthGetSession(r)
case "track.scrobble":
if r, err := ds.TrackScrobble(r); err != nil {
fmt.Printf("%v\n", err)
} else {
- response = r
+ response.Response = r
}
}
-
var text []byte
switch r.FormValue("format") {
case "json":
@@ -120,5 +124,6 @@ func ApiHandler(ds DataStore, w http.ResponseWriter, r *http.Request) {
text, _ = xml.Marshal(response)
}
fmt.Printf("%s\n", text)
+ fmt.Fprint(w, xml.Header)
w.Write(text)
}