-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproblemset.go
110 lines (96 loc) · 2.59 KB
/
problemset.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
package goforces
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
)
//Problems represents the response from /problemset.problems
type Problems struct {
Problems []Problem `json:"problems"`
ProblemStatistics []ProblemStatistics `json:"problemStatistics"`
}
// ProblemSetProblemsOptions specifies the optional parameters of the problemset.problems
type ProblemSetProblemsOptions struct {
Tags []string
}
func (o *ProblemSetProblemsOptions) options() interface{} {
if o == nil {
return nil
}
type option struct {
Tags string `url:"tags,omitempty"`
}
return &option{Tags: strings.Join(o.Tags, ";")}
}
// ProblemSetRecentStatus specifies the optional parameters of the problemset.recentStatus
type ProblemSetRecentStatus struct {
Count int
}
func (o *ProblemSetRecentStatus) options() interface{} {
if o == nil {
return nil
}
type option struct {
Count int `url:"count,omitempty"`
}
return &option{Count: o.Count}
}
//GetProblemSetProblems implements /problemset.problems
func (c *Client) GetProblemSetProblems(ctx context.Context, options *ProblemSetProblemsOptions) (*Problems, error) {
c.Logger.Println("GetProblems : ", options)
spath := "/problemset.problems" + "?"
req, err := c.newRequest(ctx, "GET", spath, options.options(), nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result Problems `json:"result"`
}
var resp Response
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error: %s", res.Status)
}
return &resp.Result, nil
}
//GetProblemSetRecentStatus implements /problemset.recentStatus
func (c *Client) GetProblemSetRecentStatus(ctx context.Context, count int) ([]Submission, error) {
c.Logger.Println("GetRecentStatus count: ", count)
if count <= 0 || count > 1000 {
return nil, fmt.Errorf("count value must be between 1 and 1000: %d", count)
}
v := url.Values{}
v.Add("count", strconv.Itoa(count))
spath := "/problemset.recentStatus" + "?" + v.Encode()
req, err := c.newRequest(ctx, "GET", spath, nil, nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result []Submission `json:"result"`
}
var resp Response
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error: %s", res.Status)
}
return resp.Result, nil
}