-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
181 lines (157 loc) · 3.38 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
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
package fiddle
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"strings"
)
const baseURL = "https://fiddle.fastly.dev"
type Client struct {
client *http.Client
}
func NewClient() *Client {
return &Client{
client: &http.Client{},
}
}
func (c *Client) Get(id string) (*Fiddle, error) {
createURL := baseURL + "/fiddle/" + id
req, err := http.NewRequest(http.MethodGet, createURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
j := Resp{}
err = json.Unmarshal(body, &j)
if err != nil {
return nil, err
}
return &j.Fiddle, nil
}
func (c *Client) Update(f *Fiddle) (*Fiddle, error) {
path := "/fiddle"
if f.ID != "" {
path += "/" + f.ID
}
b, err := json.Marshal(f)
if err != nil {
return nil, err
}
resp, err := c.post(path, b)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
j := Resp{}
err = json.Unmarshal(body, &j)
if err != nil {
return nil, err
}
return &j.Fiddle, nil
}
func (c *Client) Clone(f *Fiddle) (*Fiddle, error) {
// TODO: proper deep copy
b, err := json.Marshal(f)
if err != nil {
return nil, err
}
o := Fiddle{}
json.Unmarshal(b, &o)
o.ID = ""
return c.Update(&o)
}
func (c *Client) Lock(f *Fiddle) error {
return fmt.Errorf("not implemented")
}
func (c *Client) Freeze(f *Fiddle) error {
return fmt.Errorf("not implemented")
}
func (c *Client) PurgeKey(f *Fiddle, key string) error {
return fmt.Errorf("not implemented")
}
func (c *Client) post(path string, data []byte) (*http.Response, error) {
url := fmt.Sprintf("%s%s", baseURL, path)
var buf io.Reader
if data != nil {
buf = bytes.NewBuffer(data)
}
req, err := http.NewRequest(http.MethodPost, url, buf)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
if data != nil {
req.Header.Set("Content-Type", "application/json")
}
return c.client.Do(req)
}
type ExecuteOptions struct {
CacheID int
}
func (o *ExecuteOptions) setDefaults() {
if o.CacheID == 0 {
o.CacheID = rand.Intn(100000)
}
}
func (c *Client) Execute(f *Fiddle, opts ExecuteOptions) (*ExecResults, error) {
opts.setDefaults()
path := fmt.Sprintf("/fiddle/%s/execute?cacheID=%d", f.ID, opts.CacheID)
resp, err := c.post(path, nil)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
j := ExecuteResp{}
err = json.Unmarshal(body, &j)
if err != nil {
return nil, err
}
resultsURL := baseURL + "/results/" + j.SessionID + "/stream"
req, err := http.NewRequest(http.MethodGet, resultsURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "text/event-stream")
req.Header.Set("Connection", "keep-alive")
resp, err = c.client.Do(req)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(resp.Body)
event := ExecResults{}
// Simplistic handling of Fastly's Server-Sent Events stream
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "data:") {
continue
}
_, d, _ := strings.Cut(line, ": ")
if d != "" {
if err := json.Unmarshal([]byte(d), &event); err != nil {
return nil, err
}
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return &event, nil
}