diff options
Diffstat (limited to 'modern.go')
| -rw-r--r-- | modern.go | 52 |
1 files changed, 33 insertions, 19 deletions
@@ -6,6 +6,7 @@ import ( "encoding/xml" "fmt" "net/http" + "strings" ) type Api interface { @@ -18,19 +19,31 @@ type Api interface { type ApiResponse interface { } +type SuspendScrobbles Scrobbles + +func (s Scrobbles) MarshalJSON() ([]byte, error) { + return json.Marshal(&struct { + Tag SuspendScrobbles `json:"scrobbles"` + }{Tag: SuspendScrobbles(s)}) +} + +func (s *Session) MarshalJSON() ([]byte, error) { + return json.Marshal(s) +} + type Name struct { XMLName xml.Name } type Token struct { - Name - string + XMLName xml.Name `xml:"token" json:"-"` + Val string `xml:",innerxml" json:"token"` } type LFMResponse struct { - XMLName xml.Name `xml:"lfm" json:"-"` - Status string `xml:"status,attr" json:"-"` - Response ApiResponse `json:"scrobbles"` + XMLName xml.Name `xml:"lfm" json:"-"` + Status string `xml:"status,attr" json:"-"` + Response ApiResponse } type Attrs struct { @@ -68,11 +81,8 @@ func (n Name) getName() string { } func (store *SqlStore) AuthGetToken(r *http.Request) ApiResponse { - token := randomToken(16) - response := struct { - Token string `xml:"token" json:"token"` - }{Token: token} - return response + token := Token{Val: randomToken(16)} + return token } func (store *SqlStore) AuthGetMobileSession(r *http.Request) ApiResponse { @@ -108,17 +118,19 @@ func (store *SqlStore) TrackScrobble(r *http.Request) (ApiResponse, error) { func (app *App) ApiHandler(w http.ResponseWriter, r *http.Request) { method := r.FormValue("method") - response := LFMResponse{Status: "ok"} - switch method { - case "auth.getToken": - response.Response = app.DataStore.AuthGetToken(r) - case "auth.getSession": - response.Response = app.DataStore.AuthGetSession(r) + var response ApiResponse + switch strings.ToLower(method) { + case "auth.gettoken": + response = app.AuthGetToken(r) + case "auth.getsession": + response = app.AuthGetSession(r) + case "auth.getmobilesession": + response = app.AuthGetSession(r) case "track.scrobble": if r, err := app.DataStore.TrackScrobble(r); err != nil { fmt.Printf("%v\n", err) } else { - response.Response = r + response = r } } var text []byte @@ -126,9 +138,11 @@ func (app *App) ApiHandler(w http.ResponseWriter, r *http.Request) { case "json": text, _ = json.Marshal(response) default: - text, _ = xml.Marshal(response) + xmlresponse := LFMResponse{Status: "ok", + Response: response} + text, _ = xml.Marshal(xmlresponse) + fmt.Fprint(w, xml.Header) } fmt.Printf("%s\n", text) - fmt.Fprint(w, xml.Header) w.Write(text) } |
