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) }