-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgoogleapi.go
More file actions
62 lines (55 loc) · 1.12 KB
/
googleapi.go
File metadata and controls
62 lines (55 loc) · 1.12 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
package structgraph
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"time"
)
const (
googleApiUrl = "https://chart.googleapis.com/chart"
quickChartApiUrl = "https://quickchart.io/graphviz"
timeout = time.Second * 60
)
var cli = &http.Client{
Timeout: timeout,
}
func GenPngFromQuickChartApi(str, filename string) (err error) {
b, err := json.Marshal(map[string]string{
"graph": str,
"format": "png",
})
if err != nil {
return
}
resp, err := cli.Post(quickChartApiUrl, "application/json", bytes.NewBuffer(b))
if err != nil {
return
}
return write(resp, filename)
}
func write(resp *http.Response, filename string) (err error) {
defer resp.Body.Close()
ret, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
if resp.StatusCode != http.StatusOK {
err = errors.New(string(ret))
return
}
err = ioutil.WriteFile(filename, ret, 0664)
return
}
func GenPngFromApi(str, filename string) (err error) {
u := url.Values{}
u.Add("cht", "gv")
u.Add("chl", str)
resp, err := cli.PostForm(googleApiUrl, u)
if err != nil {
return
}
return write(resp, filename)
}