-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameters.go
48 lines (42 loc) · 1.16 KB
/
parameters.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
type Parameters struct {
ContestTitle string
ContestLocked bool
}
func fetchParameters(ctx context.Context, cfg Config) (params Parameters, err error) {
b := bytes.Buffer{}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/printd/contests/%s/parameters", cfg.Toph.BaseURL, cfg.Toph.ContestID), nil)
if err != nil {
return Parameters{}, err
}
req.Header.Add("Authorization", "Printd "+cfg.Toph.Token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return Parameters{}, retryableError{tophError{"Could not reach Toph", err}}
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusNotFound:
return Parameters{}, nil
case http.StatusForbidden:
return Parameters{}, tophError{"Could not retrieve parameters", errInvalidToken}
}
b.Reset()
_, err = io.Copy(&b, resp.Body)
if err != nil {
return Parameters{}, retryableError{tophError{"Could not retrieve parameters", err}}
}
err = json.NewDecoder(&b).Decode(¶ms)
if err != nil {
return Parameters{}, retryableError{tophError{"Could not parse response", err}}
}
return params, nil
}