-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_public.go
184 lines (160 loc) · 4.14 KB
/
api_public.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package webapi
import (
"encoding/json"
"errors"
"net/url"
"time"
model "github.com/raceresult/go-model"
"github.com/raceresult/go-model/vbdate"
"golang.org/x/oauth2"
)
// Public contains all api endpoints regarding functions on public servers
type Public struct {
api *API
sessionID string
}
// newPublic creates a new Public api endpoint group
func newPublic(api *API) *Public {
return &Public{
api: api,
sessionID: "0",
}
}
// Login creates a new session
func (q *Public) Login(user, pw string) error {
values := url.Values{}
values.Set("user", user)
values.Set("pw", pw)
resp, err := q.api.post("", "public/login", nil, "application/x-www-form-urlencoded", values.Encode())
if err != nil {
return err
}
q.sessionID = string(resp)
return nil
}
// Logout terminates the session
func (q *Public) Logout() error {
if q.sessionID == "" {
return errors.New("not logged in")
}
_, err := q.api.get("", "public/logout", nil)
return err
}
type EventListItem struct {
ID string
UserID int
UserName string
CheckedOut bool
Participants int
NotActivated int
EventName string
EventDate vbdate.VBDate
EventDate2 vbdate.VBDate
EventLocation string
EventCountry int
}
// EventList returns a list of events
func (q *Public) EventList(year int, filter string) ([]EventListItem, error) {
values := urlValues{
"year": year,
"filter": filter,
"addsettings": "EventName,EventDate,EventDate2,EventLocation,EventCountry",
}
bts, err := q.api.get("", "public/eventlist", values)
if err != nil {
return nil, err
}
var dest []EventListItem
if err := json.Unmarshal(bts, &dest); err != nil {
return nil, err
}
return dest, nil
}
// CreateEvent creates a new event (online server only) and returns the new eventID
func (q *Public) CreateEvent(eventName string, eventDate time.Time, eventCountry int, copyOf int, templateID int,
mode int, laps int) (*EventAPI, error) {
values := urlValues{
"name": eventName,
"date": eventDate,
"country": eventCountry,
"copyOf": copyOf,
"templateID": templateID,
"mode": mode,
"laps": laps,
}
resp, err := q.api.get("", "public/createevent", values)
if err != nil {
return nil, err
}
return NewEventAPI(string(resp), q.api), nil
}
// DeleteEvent deletes an event, use with care!
func (q *Public) DeleteEvent(eventID string) error {
values := urlValues{
"eventID": eventID,
}
_, err := q.api.get("", "public/deleteevent", values)
return err
}
// TokenFromSession returns an auth token for other rr services
func (q *Public) TokenFromSession() (*oauth2.Token, error) {
bts, err := q.api.get("", "public/tokenfromsession", nil)
if err != nil {
return nil, err
}
var dest *oauth2.Token
if err := json.Unmarshal(bts, &dest); err != nil {
return nil, err
}
return dest, nil
}
// UserInfo returns ID + name of current user
func (q *Public) UserInfo() (*model.UserInfo, error) {
bts, err := q.api.get("", "public/userinfo", nil)
if err != nil {
return nil, err
}
var dest model.UserInfo
if err := json.Unmarshal(bts, &dest); err != nil {
return nil, err
}
return &dest, nil
}
// UserRightsGet retrieves the list of users with access rights
func (q *Public) UserRightsGet(eventID string) ([]model.UserRight, error) {
values := urlValues{
"eventID": eventID,
}
bts, err := q.api.get("", "userrights/get", values)
if err != nil {
return nil, err
}
var dest []model.UserRight
if err := json.Unmarshal(bts, &dest); err != nil {
return nil, err
}
return dest, nil
}
// UserRightsSave saves users rights
func (q *Public) UserRightsSave(eventID string, user string, rights string) error {
values := urlValues{
"eventID": eventID,
"user": user,
"rights": rights,
}
_, err := q.api.get("", "userrights/save", values)
return err
}
// UserRightsDelete deletes users rights
func (q *Public) UserRightsDelete(eventID string, userID int) error {
values := urlValues{
"eventID": eventID,
"userID": userID,
}
_, err := q.api.get("", "userrights/delete", values)
return err
}
// SessionID returns the sessionID after a login
func (q *Public) SessionID() string {
return q.sessionID
}