-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.go
More file actions
210 lines (199 loc) · 5.9 KB
/
api.go
File metadata and controls
210 lines (199 loc) · 5.9 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"context"
"encoding/base64"
"github.com/machinebox/graphql"
"log"
"os"
)
func setHeaders(request *graphql.Request, key string) {
if key != "" {
request.Header.Set("X-Jjs-Auth", key)
}
request.Header.Set("Connection", "close")
}
func (apiClient Api) sendRun(key, toolchain string, runCode []byte, problem, contest string) (int, error) {
mutation := graphql.NewRequest(`
mutation ($toolchain: String!, $runCode: String!, $problem: String!, $contest: String!) {
submitSimple(toolchain: $toolchain, runCode: $runCode, problem: $problem, contest: $contest) {
id
}
}
`)
mutation.Var("toolchain", toolchain)
mutation.Var("runCode", base64.StdEncoding.EncodeToString(runCode))
mutation.Var("problem", problem)
mutation.Var("contest", contest)
setHeaders(mutation, key)
var response struct {
SubmitSimple struct {
Id int
}
}
err := apiClient.client.Run(context.Background(), mutation, &response)
if err != nil {
if apiClient.debug {
apiClient.logger.Println("Error while sending run: " + err.Error())
}
return -1, err
}
return response.SubmitSimple.Id, nil
}
func (apiClient Api) getApiVersion(key string) (string, error) {
query := graphql.NewRequest(`
query {
apiVersion
}
`)
setHeaders(query, key)
var response struct {
ApiVersion string
}
err := apiClient.client.Run(context.Background(), query, &response)
if err != nil { // TODO: compare this error to error when bad cookie is set
if apiClient.debug {
apiClient.logger.Println("Error while authenticating: " + err.Error())
}
return "", err
}
return response.ApiVersion, nil
}
func (apiClient Api) authorize(login, password string) (string, error) {
mutation := graphql.NewRequest(`
mutation ($login: String!, $password: String!) {
authSimple (login: $login, password: $password) {
data
}
}
`)
mutation.Var("login", login)
mutation.Var("password", password)
setHeaders(mutation, "")
var response struct {
AuthSimple struct {
Data string
}
}
err := apiClient.client.Run(context.Background(), mutation, &response)
if err != nil {
if apiClient.debug {
apiClient.logger.Println("Error while authorizing: " + err.Error())
}
return "", err
}
return response.AuthSimple.Data, nil
}
func (apiClient Api) createUser(key, login, password string, groups []string) (string, error) {
mutation := graphql.NewRequest(`
mutation ($login: String!, $password: String!, $groups: [String!]!) {
createUser(login: $login, password: $password, groups: $groups) {
id
}
}
`)
mutation.Var("login", login)
mutation.Var("password", password)
mutation.Var("groups", groups)
setHeaders(mutation, key)
var response struct {
CreateUser struct {
Id string
}
}
err := apiClient.client.Run(context.Background(), mutation, &response)
if err != nil {
if apiClient.debug {
apiClient.logger.Println("Error while creating user: " + err.Error())
}
return "", err
}
return response.CreateUser.Id, nil
}
func (apiClient* Api) listContests(key string) ([]*Contest, error) {
query := graphql.NewRequest(`
query {
contests {
title
id
problems {
title
id
}
}
}
`)
var response struct {
Contests []Contest
}
setHeaders(query, key)
err := apiClient.client.Run(context.Background(), query, &response)
if err != nil {
if apiClient.debug {
apiClient.logger.Println("Error while listing contests: " + err.Error())
}
return []*Contest{}, err
}
result := make([]*Contest, len(response.Contests))
for i := 0; i < len(response.Contests); i += 1 {
result[i] = &response.Contests[i]
}
return result, nil
}
func (apiClient* Api) findContest(key, contestID string) (*Contest, error) {
query := graphql.NewRequest(`
query ($contestID: String!) {
contest(name: $contestID) {
title
id
problems {
title
id
}
}
}
`)
query.Var("contestID", contestID)
setHeaders(query, key)
var response struct {
Contest Contest
}
err := apiClient.client.Run(context.Background(), query, &response)
if err != nil {
if apiClient.debug {
apiClient.logger.Println("Error while finding contest: " + err.Error())
}
return &Contest{}, err
}
return &response.Contest, nil
}
func (apiClient *Api) listToolChains(key string) ([]*ToolChain, error) {
query := graphql.NewRequest(`
query {
toolchains {
name
id
}
}
`)
var response struct {
ToolChains []ToolChain
}
setHeaders(query, key)
err := apiClient.client.Run(context.Background(), query, &response)
if err != nil {
if apiClient.debug {
apiClient.logger.Println("Error while listing toolchains: " + err.Error())
}
return []*ToolChain{}, err
}
result := make([]*ToolChain, len(response.ToolChains))
for i := 0; i < len(response.ToolChains); i += 1 {
result[i] = &response.ToolChains[i]
}
return result, nil
}
func initialize(apiURL string, logFile *os.File, debug bool) *Api {
client := graphql.NewClient(apiURL)
logger := log.New(logFile, "", log.LstdFlags)
return &Api{client, logger, debug}
}