forked from xalanq/cf-tool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
62 lines (53 loc) · 1.2 KB
/
client.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
package client
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"github.com/fatih/color"
"github.com/xalanq/cf-tool/cookiejar"
)
// Client codeforces client
type Client struct {
Jar *cookiejar.Jar `json:"cookies"`
Username string `json:"username"`
Ftaa string `json:"ftaa"`
Bfaa string `json:"bfaa"`
LastSubmission *SaveSubmission `json:"last_submission"`
path string
client *http.Client
}
// New client
func New(path string) *Client {
jar, _ := cookiejar.New(nil)
c := &Client{Jar: jar, LastSubmission: nil, path: path, client: nil}
if path != "" {
c.load()
}
c.client = &http.Client{Jar: c.Jar}
return c
}
// load from path
func (c *Client) load() (err error) {
file, err := os.Open(c.path)
if err != nil {
return
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
return err
}
return json.Unmarshal(bytes, c)
}
// save file to path
func (c *Client) save() (err error) {
data, err := json.MarshalIndent(c, "", " ")
if err == nil {
err = ioutil.WriteFile(c.path, data, 0644)
}
if err != nil {
color.Red("Cannot save session to %v\n%v", c.path, err.Error())
}
return
}