aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2017-06-24 15:27:22 -0400
committerGuillaume Horel <guillaume.horel@gmail.com>2017-06-24 15:27:22 -0400
commit0f836c3c0a0ad3addf943282199a73f1e92d9be9 (patch)
treecf47137ffe8f0d0b2ab292494c8b4cc052abe868
parent05c00b03c4e1503ed7e90b21d3caecc04b35e8d4 (diff)
downloadlastfm-api-0f836c3c0a0ad3addf943282199a73f1e92d9be9.tar.gz
AuthGetToken working
-rw-r--r--marshall_test.go25
-rw-r--r--modern.go52
2 files changed, 55 insertions, 22 deletions
diff --git a/marshall_test.go b/marshall_test.go
index bb57cfd..a916556 100644
--- a/marshall_test.go
+++ b/marshall_test.go
@@ -28,6 +28,27 @@ func TestMarshallXML(t *testing.T) {
os.Stdout.Write(output)
}
+func TestMarshallXMLToken(t *testing.T) {
+ token := Token{Val: randomToken(16)}
+ response := LFMResponse{Status: "ok",
+ Response: token}
+ output, err := xml.MarshalIndent(response, " ", " ")
+ if err != nil {
+ fmt.Printf("error: %v\n", err)
+ }
+ fmt.Printf(xml.Header)
+ os.Stdout.Write(output)
+}
+
+func TestMarshallJSONToken(t *testing.T) {
+ token := Token{Val: randomToken(16)}
+ output, err := json.MarshalIndent(token, " ", " ")
+ if err != nil {
+ fmt.Printf("error: %v\n", err)
+ }
+ os.Stdout.Write(output)
+}
+
func TestMarshallJSON(t *testing.T) {
s := []Scrobble{
Scrobble{Artist: Correctable{Name: "Mumford & Sons"},
@@ -37,9 +58,7 @@ func TestMarshallJSON(t *testing.T) {
}
scrobbles := Scrobbles{Scrobbles: s,
Attrs: Attrs{1, 0}}
- response := LFMResponse{Status: "ok",
- Response: scrobbles}
- output, err := json.MarshalIndent(response, " ", " ")
+ output, err := json.MarshalIndent(scrobbles, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
diff --git a/modern.go b/modern.go
index 14261e3..cbfbcd3 100644
--- a/modern.go
+++ b/modern.go
@@ -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)
}