-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun.go
56 lines (48 loc) · 1.26 KB
/
run.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
package goplayground
import (
"encoding/json"
"net/http"
"net/url"
"time"
)
// RunResult is result of Client.Run.
type RunResult struct {
// Errors is compile or runtime error on Go Playground.
Errors string
// Events has output events on Go Playground.
Events []*RunEvent
}
// RunEvent represents output events to stdout or stderr of Client.Run.
type RunEvent struct {
// Message is a message which is outputed to stdout or stderr.
Message string
// Kind has stdout or stderr value.
Kind string
// Delay represents delay time to print the message to stdout or stderr.
Delay time.Duration
}
// Run compiles and runs the given src.
// src can be set string, []byte and io.Reader value.
func (cli *Client) Run(src interface{}) (*RunResult, error) {
values := url.Values{}
values.Set("version", Version)
body, err := srcToString(src)
if err != nil {
return nil, err
}
values.Set("body", body)
req, err := http.NewRequest(http.MethodPost, cli.baseURL()+"/compile?"+values.Encode(), nil)
if err != nil {
return nil, err
}
resp, err := cli.httpClient().Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result RunResult
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}