aboutsummaryrefslogtreecommitdiffstats
path: root/modern.go
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2016-11-19 17:35:07 -0500
committerThibaut Horel <thibaut.horel@gmail.com>2016-11-19 17:35:07 -0500
commitc2c39cc2756230c1de29d8065b8b320f2f084045 (patch)
treebaf3569f25b0fabeb93144d636bf61a2deae6b4a /modern.go
downloadlastfm-api-c2c39cc2756230c1de29d8065b8b320f2f084045.tar.gz
Initial commit
Diffstat (limited to 'modern.go')
-rw-r--r--modern.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/modern.go b/modern.go
new file mode 100644
index 0000000..4a95f32
--- /dev/null
+++ b/modern.go
@@ -0,0 +1,90 @@
+package main
+
+import (
+ "encoding/json"
+ "encoding/xml"
+ "fmt"
+ "net/http"
+)
+
+type Api interface {
+ AuthGetToken(*http.Request) ApiResponse
+ AuthGetSession(*http.Request) ApiResponse
+ AuthGetMobileSession(*http.Request) ApiResponse
+}
+
+type ApiResponse interface {
+}
+
+type Name struct {
+ XMLName xml.Name
+}
+
+type Token struct {
+ Name
+ string
+}
+
+type Scrobbles struct {
+ XMLName xml.Name `xml:"scrobbles"`
+ Scrobbles []Scrobble
+}
+
+type Scrobble struct {
+ Track Track `xml:"track"`
+}
+
+type Track struct {
+ Corrected int `xml:"corrected,attr"`
+ Name string `xml:",chardata"`
+}
+
+func (n Name) getName() string {
+ return n.XMLName.Local
+}
+
+func (store *SqlStore) AuthGetToken(r *http.Request) ApiResponse {
+ token := randomToken(16)
+ response := struct {
+ Token string `xml:"token" json:"token"`
+ }{Token: token}
+ return response
+}
+
+func (store *SqlStore) AuthGetMobileSession(r *http.Request) ApiResponse {
+ return struct{}{}
+}
+
+func (store *SqlStore) AuthGetSession(r *http.Request) ApiResponse {
+ var response struct {
+ Session *Session `json:"session"`
+ }
+ session := NewSession("thibauthorel", r.FormValue("api_key"), "2.0")
+ store.PutSession(session)
+ response.Session = session
+ return response
+}
+
+func (store *SqlStore) TrackScrobble(r *http.Request) ApiResponse {
+ return struct{}{}
+}
+
+func ApiHandler(ds DataStore, w http.ResponseWriter, r *http.Request) {
+ method := r.FormValue("method")
+ var response ApiResponse
+ if method == "auth.getToken" {
+ response = ds.AuthGetToken(r)
+ } else if method == "auth.getSession" {
+ response = ds.AuthGetSession(r)
+ }
+
+ var text []byte
+ switch r.FormValue("format") {
+ case "json":
+ text, _ = json.Marshal(response)
+ default:
+ text, _ = xml.Marshal(response)
+ }
+ fmt.Printf("%s\n", text)
+ w.Write(text)
+}