aboutsummaryrefslogtreecommitdiffstats
path: root/modern.go
blob: 8a384c381fa2bdc6d9f5eff39e4011e92a4cf5a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 Attrs struct {
	Accepted int `xml:"accepted,attr" json:"accepted"`
	Ignored  int `xml:"ignored,attr" json:"ignored"`
}

type Scrobbles struct {
	XMLName   xml.Name   `xml:"scrobbles" json:scrobbles"`
	Scrobbles []Scrobble `xml:"scrobble" json:scrobble`
	Attrs     `json:"@attr"`
}

type Correctable struct {
	Corrected int    `xml:"corrected,attr" json:"corrected"`
	Name      string `xml:",chardata" json:"#text"`
}

func NewCorrectable(name string) Correctable {
	return Correctable{
		Corrected: 0,
		Name:      name,
	}
}

func (field *Correctable) String() string {
	return field.Name
}

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