-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathclient.go
More file actions
113 lines (102 loc) · 2.68 KB
/
client.go
File metadata and controls
113 lines (102 loc) · 2.68 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package alluxio
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
const (
apiPrefix = "api/v1"
pathsPrefix = "paths"
streamsPrefix = "streams"
// Path endpoints
createDirectory = "create-directory"
createFile = "create-file"
delete = "delete"
exists = "exists"
free = "free"
getStatus = "get-status"
listStatus = "list-status"
mount = "mount"
openFile = "open-file"
rename = "rename"
setAttribute = "set-attribute"
unmount = "unmount"
// Stream endpoints
close = "close"
read = "read"
write = "write"
)
type Client struct {
host string
port int
prefix string
http http.Client
}
// NewClient is the Alluxio file system client factory.
func NewClient(host string, port int, timeout time.Duration) *Client {
return &Client{
host: host,
port: port,
prefix: apiPrefix,
http: http.Client{
Timeout: timeout,
},
}
}
func join(components ...string) string {
return strings.Join(components, "/")
}
func (client *Client) endpointURL(resource string, params map[string]string) string {
paramPairs := []string{}
for key, value := range params {
paramPairs = append(paramPairs, key+"="+url.QueryEscape(value))
}
return fmt.Sprintf("http://%v:%v/%v/%v?%v", client.host, client.port, client.prefix, resource, strings.Join(paramPairs, "&"))
}
func (client *Client) post(resource string, params map[string]string, input interface{}, output interface{}) error {
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(input); err != nil {
return fmt.Errorf("Encode() failed: %v", err)
}
resp, err := client.http.Post(client.endpointURL(resource, params), "application/json", &b)
if err != nil {
return err
}
if err := check(resp); err != nil {
return err
}
if err := process(resp, output); err != nil {
return err
}
return nil
}
func check(resp *http.Response) error {
if resp.StatusCode != http.StatusOK {
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Response status: %v (%v):\nFailed to ready response body: %v", resp.Status, resp.StatusCode, err)
}
return fmt.Errorf("Response status: %v (%v):\nResponse body:\n%s", resp.Status, resp.StatusCode, bytes)
}
return nil
}
func process(resp *http.Response, output interface{}) error {
defer resp.Body.Close()
if output != nil {
contentType := resp.Header.Get("Content-Type")
switch contentType {
case "application/json":
if err := json.NewDecoder(resp.Body).Decode(output); err != nil {
return fmt.Errorf("Decode() failed: %v", err)
}
default:
return fmt.Errorf("Unsupported response type: %v", contentType)
}
}
return nil
}