-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
59 lines (50 loc) · 1.2 KB
/
Copy pathclient.go
File metadata and controls
59 lines (50 loc) · 1.2 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
package plugins
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/docker/docker/utils"
)
const (
versionMimetype = "appplication/vnd.docker.plugins.v1+json"
)
func NewClient(addr string) *Client {
// No TLS. Hopefully this discourages non-local plugins
tr := &http.Transport{}
protoAndAddr := strings.Split(addr, "://")
utils.ConfigureTCPTransport(tr, protoAndAddr[0], protoAndAddr[1])
return &Client{&http.Client{Transport: tr}, addr}
}
type Client struct {
http *http.Client
addr string
}
func (c *Client) Call(serviceMethod string, args interface{}, ret interface{}) error {
u, _ := url.Parse(c.addr)
u.Path = serviceMethod
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(args); err != nil {
return err
}
req, err := http.NewRequest("POST", u.String(), &buf)
req.Header.Add("Accept", versionMimetype)
resp, err := c.http.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
remoteErr, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
return fmt.Errorf("Plugin Error: %v", remoteErr)
}
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
return err
}
return nil
}