-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.go
More file actions
64 lines (49 loc) · 1.11 KB
/
execution.go
File metadata and controls
64 lines (49 loc) · 1.11 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
package gowandbox
import (
"bytes"
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
func NewGWBProgram() *GWBProgram {
// Returns new GWBProgram struct
return &GWBProgram{}
}
/*
Method to execute a GWBProgram
If no errors ocurred, the result is returned in the form of a GWBResult struct.
If the response code is not 200, an error is returned.
Maps to the `/compile.json` endpoint
*/
func (g *GWBProgram) Execute(ctx context.Context) (GWBResult, error) {
data, err := json.Marshal(g)
var result GWBResult
if err != nil {
return result, err
}
client := http.DefaultClient
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
WandBoxUrl+"compile.json",
bytes.NewBuffer(data),
)
if err != nil {
return result, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return result, err
}
if resp.StatusCode != http.StatusOK {
defer resp.Body.Close()
e, _ := ioutil.ReadAll(resp.Body)
return result, errors.New(string(e))
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&result)
return result, err
}