-
Notifications
You must be signed in to change notification settings - Fork 2
/
menu.go
49 lines (40 loc) · 765 Bytes
/
menu.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
package pb
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
type MenuCreateOpResp struct {
Errcode int
Errmsg string
}
func CreateMenu(requestLine string, menuLayout []byte) error {
req, err := http.NewRequest("POST",
requestLine,
bytes.NewReader(menuLayout))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json; encoding=utf-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
opResp := &MenuCreateOpResp{}
err = json.Unmarshal(body, opResp)
if err != nil {
return err
}
if opResp.Errcode != 0 {
return errors.New(opResp.Errmsg)
}
return nil
}