-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeforces.go
191 lines (167 loc) · 6.05 KB
/
codeforces.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
185
186
187
188
189
190
191
// Utility package that allows access to the codeforces API.
// Some functions require authentication. For more informations on how to
// get your API key and secret, please refer here: https://codeforces.com/apiHelp
package codeforces
import (
"crypto/sha512"
"fmt"
"net/http"
"net/url"
"time"
)
const (
Version = "v.0.0.2"
defaultbaseURLString = "https://codeforces.com/api/"
)
// holds a shared httpclient (could change) and the services
// responsible for communicating with the various parts of the api
type Client struct {
Blog *blogService
User *userService
Contest *contestService
Problems *problemService
Actions *actionsService
}
func NewClient(apiKey, apiSecret string) *Client {
c := newDefaultClientWrapper(defaultbaseURLString, apiKey, apiSecret)
return NewCustomClient(apiKey, apiSecret, c)
}
func NewCustomClient(apiKey, apiSecret string, c *httpClientWrapper) *Client {
return &Client{
Blog: &blogService{c},
User: &userService{c},
Contest: &contestService{c},
Actions: &actionsService{c},
}
}
type httpClientWrapper struct {
client *http.Client
baseUrlString string
apiKey string
apiSecret string
}
func newDefaultClientWrapper(baseUrlString, apiKey, apiSecret string) *httpClientWrapper {
return &httpClientWrapper{
baseUrlString: baseUrlString,
apiKey: apiKey,
client: http.DefaultClient,
apiSecret: apiSecret,
}
}
func (c *httpClientWrapper) Get(suffix string, userParams map[string]string) (*http.Response, error) {
base, err := url.Parse(c.baseUrlString + suffix)
if err != nil {
return nil, err
}
params := url.Values{}
for k, v := range userParams {
params.Add(k, v)
}
params.Add("apiKey", c.apiKey)
t := fmt.Sprint(time.Now().UTC().UnixMilli() / 1000)
params.Add("time", t)
oldParams := params.Encode()
randomPrefix := fmt.Sprint(randomInRange(1e5, 1e6))
text := (randomPrefix + "/" + suffix + "?" + oldParams + "#" + c.apiSecret)
hash := sha512.Sum512([]byte(text))
params.Add("apiSig", randomPrefix+fmt.Sprintf("%x", hash))
base.RawQuery = params.Encode()
resp, err := c.client.Get(base.String())
return resp, err
}
type service struct {
client *httpClientWrapper
}
type (
blogService service
userService service
contestService service
problemService service
actionsService service
)
func (s *blogService) Comments(id uint) (*[]Comment, error) {
params := map[string]string{"blogEntryId": fmt.Sprint(id)}
resp, err := s.client.Get("blogEntry.comments", params)
return serializeResponse[[]Comment](resp, err)
}
func (s *blogService) EntryById(id uint) (*BlogEntry, error) {
params := map[string]string{"blogEntryId": fmt.Sprint(id)}
resp, err := s.client.Get("blogEntry.view", params)
return serializeResponse[BlogEntry](resp, err)
}
func (s *contestService) Hacks(id uint) (*ContestHack, error) {
params := map[string]string{"contestId": fmt.Sprint(id)}
resp, err := s.client.Get("contest.hacks", params)
return serializeResponse[ContestHack](resp, err)
}
func (s *contestService) RatingChange(id uint) (*[]RatingChange, error) {
params := map[string]string{"contestId": fmt.Sprint(id)}
resp, err := s.client.Get("contest.ratingChanges", params)
return serializeResponse[[]RatingChange](resp, err)
}
func (s *contestService) List(gym bool) (*[]Contest, error) {
params := map[string]string{"gym": fmt.Sprint(gym)}
resp, err := s.client.Get("contest.list", params)
return serializeResponse[[]Contest](resp, err)
}
func (s *userService) Info(users []string) (*[]User, error) {
params := map[string]string{"handles": encodeToParameter(users)}
resp, err := s.client.Get("user.info", params)
err = handleResponseStatusCode(resp, err)
return serializeResponse[[]User](resp, err)
}
func (s *userService) Rating(user string) (*[]RatingChange, error) {
params := map[string]string{"handle": user}
resp, err := s.client.Get("user.rating", params)
return serializeResponse[[]RatingChange](resp, err)
}
func (s *contestService) Standings(contestId, from, count uint, handles []string, unofficial bool) (*ContestStandings, error) {
params := map[string]string{
"contestId": fmt.Sprint(contestId),
"from": fmt.Sprint(from),
"count": fmt.Sprint(count),
"handles": encodeToParameter(handles),
"showUnofficial": fmt.Sprint(unofficial),
}
resp, err := s.client.Get("contest.standings", params)
return serializeResponse[ContestStandings](resp, err)
}
func statusDefaultParams(contestId, from, count uint) *map[string]string {
params := map[string]string{
"contestId": fmt.Sprint(contestId),
"from": fmt.Sprint(from),
"count": fmt.Sprint(count),
}
return ¶ms
}
func (s *contestService) StatusWithHandle(contestId, from, count uint, handle string) (*[]ContestStatus, error) {
params := statusDefaultParams(contestId, from, count)
(*params)["handle"] = handle
resp, err := s.client.Get("contest.status", *params)
return serializeResponse[[]ContestStatus](resp, err)
}
func (s *contestService) Status(contestId, from, count uint) (*[]ContestStatus, error) {
resp, err := s.client.Get("contest.status", *statusDefaultParams(contestId, from, count))
return serializeResponse[[]ContestStatus](resp, err)
}
// tags can also be empty (will return every problem in the problemset)
func (s *problemService) Problemset(tags []string) (*Problemset, error) {
params := map[string]string{"tags": encodeToParameter(tags)}
resp, err := s.client.Get("problemset.problems", params)
return serializeResponse[Problemset](resp, err)
}
// Maximum count can be up to 100
func (s *actionsService) RecentActions(count uint) (*[]RecentAction, error) {
if count > 100 {
return nil, fmt.Errorf("Count is greater than 100")
}
params := map[string]string{"maxCount": fmt.Sprint(count)}
resp, err := s.client.Get("recentActions", params)
return serializeResponse[[]RecentAction](resp, err)
}
// Requires authentication
func (s *userService) Friends(onlyOnline bool) (*[]string, error) {
params := map[string]string{"onlyOnline": fmt.Sprint(onlyOnline)}
resp, err := s.client.Get("user.friends", params)
return serializeResponse[[]string](resp, err)
}