forked from maddyblue/moggio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
101 lines (87 loc) · 2.27 KB
/
protocol.go
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
package protocol
import (
"encoding/gob"
"fmt"
"io"
"reflect"
"github.com/mjibson/moggio/codec"
"golang.org/x/oauth2"
)
type Protocol struct {
*Params
OAuth *oauth2.Config
newInstance func([]string, *oauth2.Token) (Instance, error)
instType reflect.Type
}
type Params struct {
Params []string `json:",omitempty"`
OAuthURL string `json:",omitempty"`
}
type Instance interface {
// Key returns a unique identifier for the instance.
Key() string
// List returns the list of available songs, possibly cached.
List() (SongList, error)
// Refresh forces an update of the song list.
Refresh() (SongList, error)
// Info returns information about one song.
Info(codec.ID) (*codec.SongInfo, error)
// GetSong returns a playable song.
GetSong(codec.ID) (codec.Song, error)
}
type SongList map[codec.ID]*codec.SongInfo
func (p *Protocol) NewInstance(params []string, token *oauth2.Token) (Instance, error) {
return p.newInstance(params, token)
}
func (p *Protocol) Decode(r io.Reader) (Instance, error) {
if p.instType == nil {
panic("NIL")
}
v := reflect.New(p.instType)
i := v.Interface()
if err := gob.NewDecoder(r).Decode(i); err != nil {
return nil, err
}
return reflect.Indirect(v).Interface().(Instance), nil
}
var protocols = make(map[string]*Protocol)
func Register(name string, params []string, newInstance func([]string, *oauth2.Token) (Instance, error), instType reflect.Type) {
protocols[name] = &Protocol{
Params: &Params{
Params: params,
},
newInstance: newInstance,
instType: instType,
}
}
func RegisterOAuth(name string, config *oauth2.Config, newInstance func([]string, *oauth2.Token) (Instance, error), instType reflect.Type) {
protocols[name] = &Protocol{
Params: &Params{
OAuthURL: config.AuthCodeURL(""),
},
OAuth: config,
newInstance: newInstance,
instType: instType,
}
}
func ByName(name string) (*Protocol, error) {
p, ok := protocols[name]
if !ok {
return nil, fmt.Errorf("unknown protocol")
}
return p, nil
}
func Get() map[string]Params {
m := make(map[string]Params)
for n, p := range protocols {
m[n] = *p.Params
}
return m
}
func Map() map[string]map[string]Instance {
p := make(map[string]map[string]Instance)
for name := range Get() {
p[name] = make(map[string]Instance)
}
return p
}