forked from kortschak/arrgh
/
arrgh_example_test.go
99 lines (87 loc) · 1.97 KB
/
arrgh_example_test.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
// Copyright ©2014 Dan Kortschak. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package arrgh_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/url"
"os"
"regexp"
"strings"
"time"
"github.com/kortschak/arrgh"
)
func Example_1() {
r, err := arrgh.NewLocalSession("", "", 3000, 10*time.Second)
if err != nil {
log.Fatal(err)
}
defer r.Close()
// Send a query to get a JSON representation of results
// from rnorm(n=10, mean=10, sd=10).
resp, err := r.Post(
"library/stats/R/rnorm/json",
"application/json",
url.Values{"digits": []string{"10"}},
strings.NewReader(`{"n":10, "mean": 10, "sd":10}`),
)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Decode the results in to a slice of float64.
var rnorm []float64
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&rnorm)
fmt.Println(rnorm, err)
}
func mask(r io.Reader) io.Reader {
re := regexp.MustCompile("x[0-9a-f]{10}")
var buf bytes.Buffer
io.Copy(&buf, r)
return bytes.NewReader(re.ReplaceAll(buf.Bytes(), []byte("xXXXXXXXXXX")))
}
func Example_3() {
r, err := arrgh.NewRemoteSession("http://public.opencpu.org", "", 10*time.Second)
if err != nil {
log.Fatal(err)
}
defer r.Close()
// Upload the contents of the file "mydata.csv" and send
// it to the read.csv function.
f, err := os.Open("mydata.csv")
if err != nil {
log.Fatal(err)
}
content, body, err := arrgh.Multipart(
arrgh.Params{"header": "FALSE"},
arrgh.Files{"file": f},
)
f.Close()
if err != nil {
log.Fatal(err)
}
resp, err := r.Post(
"library/utils/R/read.csv",
content,
nil,
body,
)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
io.Copy(os.Stdout, mask(resp.Body))
// Output:
//
// /ocpu/tmp/xXXXXXXXXXX/R/.val
// /ocpu/tmp/xXXXXXXXXXX/stdout
// /ocpu/tmp/xXXXXXXXXXX/source
// /ocpu/tmp/xXXXXXXXXXX/console
// /ocpu/tmp/xXXXXXXXXXX/info
// /ocpu/tmp/xXXXXXXXXXX/files/mydata.csv
}