-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
88 lines (73 loc) · 1.52 KB
/
api.go
File metadata and controls
88 lines (73 loc) · 1.52 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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
)
type Client struct {
url string
}
func NewClient(url string) *Client {
return &Client{
url: url,
}
}
func (c *Client) Get() (*Trie, error) {
resp, err := http.Get(c.url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var res *Trie
if err := json.Unmarshal(bytes, &res); err != nil {
return nil, err
}
return res, nil
}
func (c *Client) Create(path []string) error {
joined := strings.Join(path, "+")
resp, err := http.Post(c.url+"/projects/"+joined, "", nil)
if err != nil {
return err
} else if resp.StatusCode != http.StatusCreated {
return errors.New("invalid status code")
}
return nil
}
func (c *Client) Start(path []string) error {
joined := strings.Join(path, "+")
resp, err := http.Post(c.url+"/projects/"+joined+"/start", "", nil)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusNotFound:
return errors.New("project doesn't exist")
case http.StatusBadRequest:
return errors.New("already recording")
case http.StatusCreated:
return nil
default:
return errors.New("invalid status code")
}
}
func (c *Client) Stop() error {
resp, err := http.Post(c.url+"/stop", "", nil)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusBadRequest:
return errors.New("not recording")
case http.StatusOK:
return nil
default:
return errors.New("invalid status code")
}
}