-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsession.go
104 lines (86 loc) · 2.88 KB
/
session.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
102
103
package myradio
import (
"bytes"
"encoding/json"
"net/url"
"github.com/UniversityRadioYork/myradio-go/api"
)
// Session represents an open API session.
type Session struct {
requester api.Requester
}
// NewSession constructs a new Session with the given API key.
func NewSession(apikey string) (*Session, error) {
url, err := url.Parse(`https://ury.org.uk/api/v2`)
if err != nil {
return nil, err
}
return &Session{requester: api.NewRequester(apikey, *url)}, nil
}
// NewSessionForServer constructs a new Session with the given API key for a non-standard server URL.
func NewSessionForServer(apikey, server string) (*Session, error) {
url, err := url.Parse(server)
if err != nil {
return nil, err
}
return &Session{requester: api.NewRequester(apikey, *url)}, nil
}
// MockSession creates a new mocked API session returning the JSON message stored in message.
func MockSession(message []byte) (*Session, error) {
rm := json.RawMessage{}
err := rm.UnmarshalJSON(message)
if err != nil {
return nil, err
}
return &Session{requester: api.MockRequester(&rm)}, nil
}
// do fulfils, a request for the given endpoint.
func (s *Session) do(r *api.Request) *api.Response {
return s.requester.Do(r)
}
// get creates, and fulfils, a GET request for the given endpoint.
func (s *Session) get(endpoint string) *api.Response {
return s.do(api.NewRequest(endpoint))
}
// getf creates, and fulfils, a GET request for the endpoint created by
// the given format string and parameters.
func (s *Session) getf(format string, params ...interface{}) *api.Response {
return s.do(api.NewRequestf(format, params...))
}
func (s *Session) getWithQueryParams(format string, queryParams map[string][]string) *api.Response {
r := api.NewRequest(format)
r.Params = queryParams
return s.do(r)
}
// putf creates, and fulfils, a PUT request for the endpoint created by
// the given format string and parameters.
func (s *Session) putf(format string, body bytes.Buffer, params ...interface{}) *api.Response {
r := api.NewRequestf(format, params...)
r.ReqType = api.PutReq
r.Body = body
return s.do(r)
}
// post creates, and fulfils, a POST request for the given endpoint,
// using the given form parameters
func (s *Session) post(endpoint string, formParams map[string][]string) *api.Response {
r := api.NewRequest(endpoint)
r.ReqType = api.PostReq
r.Params = formParams
return s.do(r)
}
// NewSessionFromKeyFile tries to open a Session with the key from an API key file.
func NewSessionFromKeyFile() (*Session, error) {
apikey, err := api.GetAPIKey()
if err != nil {
return nil, err
}
return NewSession(apikey)
}
// NewSessionFromKeyFileForServer tries to open a Session with the key from an API key file, with a non-standard server.
func NewSessionFromKeyFileForServer(server string) (*Session, error) {
apikey, err := api.GetAPIKey()
if err != nil {
return nil, err
}
return NewSessionForServer(apikey, server)
}