diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2016-12-12 22:22:41 -0500 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2016-12-12 22:22:41 -0500 |
| commit | 6bb509f48f20a20e16b447ddcecbf3d55a4aa0a3 (patch) | |
| tree | 8c4e9a6f94a88053695e5359ac0f2d9561b025fe | |
| parent | 7034036c50f857456fb6c4cb45c39d6ac911bbe9 (diff) | |
| download | lastfm-api-6bb509f48f20a20e16b447ddcecbf3d55a4aa0a3.tar.gz | |
tweak marshalling
| -rw-r--r-- | marshall_test.go | 11 | ||||
| -rw-r--r-- | modern.go | 19 |
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) } @@ -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) } |
