/
directclient.go
163 lines (138 loc) · 6.13 KB
/
directclient.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package client
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"github.com/troubling/hummingbird/common"
"github.com/troubling/hummingbird/common/conf"
"github.com/troubling/hummingbird/common/srv"
"github.com/troubling/nectar"
"github.com/troubling/nectar/nectarutil"
)
type directClient struct {
pc RequestClient
account string
}
var _ nectar.Client = &directClient{}
func NewDirectClient(account string, cnf srv.ConfigLoader, certFile, keyFile string, logger srv.LowLevelLogger) (nectar.Client, error) {
policies, err := cnf.GetPolicies()
if err != nil {
return nil, err
}
pdc, err := NewProxyClient(policies, cnf, logger, certFile, keyFile, "", "", "", conf.Config{})
if err != nil {
return nil, fmt.Errorf("Could not make client: %v", err)
}
return &directClient{pc: pdc.NewRequestClient(nil, nil, logger), account: account}, nil
}
func (c *directClient) SetUserAgent(v string) {
c.pc.SetUserAgent(v)
}
func (c *directClient) GetURL() string {
return "<direct>/" + c.account
}
func (c *directClient) PutAccount(headers map[string]string) *http.Response {
return c.pc.PutAccount(context.Background(), c.account, common.Map2Headers(headers))
}
func (c *directClient) PostAccount(headers map[string]string) *http.Response {
return c.pc.PostAccount(context.Background(), c.account, common.Map2Headers(headers))
}
func (c *directClient) GetAccount(marker string, endMarker string, limit int, prefix string, delimiter string, reverse bool, headers map[string]string) ([]*nectar.ContainerRecord, *http.Response) {
resp := c.GetAccountRaw(marker, endMarker, limit, prefix, delimiter, reverse, headers)
if resp.StatusCode/100 != 2 {
return nil, resp
}
var accountListing []*nectar.ContainerRecord
if err := json.NewDecoder(resp.Body).Decode(&accountListing); err != nil {
resp.Body.Close()
// FIXME. Log something.
return nil, nectarutil.ResponseStub(http.StatusInternalServerError, err.Error())
}
resp.Body.Close()
return accountListing, resp
}
func (c *directClient) GetAccountRaw(marker string, endMarker string, limit int, prefix string, delimiter string, reverse bool, headers map[string]string) *http.Response {
options := map[string]string{
"format": "json",
"marker": marker,
"end_marker": endMarker,
"prefix": prefix,
"delimiter": delimiter,
}
if limit != 0 {
options["limit"] = strconv.Itoa(limit)
}
if reverse {
options["reverse"] = "true"
}
return c.pc.GetAccountRaw(context.Background(), c.account, options, common.Map2Headers(headers))
}
func (c *directClient) HeadAccount(headers map[string]string) *http.Response {
return c.pc.HeadAccount(context.Background(), c.account, common.Map2Headers(headers))
}
func (c *directClient) DeleteAccount(headers map[string]string) *http.Response {
return c.pc.DeleteAccount(context.Background(), c.account, common.Map2Headers(headers))
}
func (c *directClient) PutContainer(container string, headers map[string]string) *http.Response {
return c.pc.PutContainer(context.Background(), c.account, container, common.Map2Headers(headers))
}
func (c *directClient) PostContainer(container string, headers map[string]string) *http.Response {
return c.pc.PostContainer(context.Background(), c.account, container, common.Map2Headers(headers))
}
func (c *directClient) GetContainer(container string, marker string, endMarker string, limit int, prefix string, delimiter string, reverse bool, headers map[string]string) ([]*nectar.ObjectRecord, *http.Response) {
resp := c.GetContainerRaw(container, marker, endMarker, limit, prefix, delimiter, reverse, headers)
if resp.StatusCode/100 != 2 {
return nil, resp
}
var containerListing []*nectar.ObjectRecord
if err := json.NewDecoder(resp.Body).Decode(&containerListing); err != nil {
resp.Body.Close()
// FIXME. Log something.
return nil, nectarutil.ResponseStub(http.StatusInternalServerError, err.Error())
}
resp.Body.Close()
return containerListing, resp
}
func (c *directClient) GetContainerRaw(container string, marker string, endMarker string, limit int, prefix string, delimiter string, reverse bool, headers map[string]string) *http.Response {
options := map[string]string{
"format": "json",
"marker": marker,
"end_marker": endMarker,
"prefix": prefix,
"delimiter": delimiter,
}
if limit != 0 {
options["limit"] = strconv.Itoa(limit)
}
if reverse {
options["reverse"] = "true"
}
return c.pc.GetContainerRaw(context.Background(), c.account, container, options, common.Map2Headers(headers))
}
func (c *directClient) HeadContainer(container string, headers map[string]string) *http.Response {
return c.pc.HeadContainer(context.Background(), c.account, container, common.Map2Headers(headers))
}
func (c *directClient) DeleteContainer(container string, headers map[string]string) *http.Response {
return c.pc.DeleteContainer(context.Background(), c.account, container, common.Map2Headers(headers))
}
func (c *directClient) PutObject(container string, obj string, headers map[string]string, src io.Reader) *http.Response {
return c.pc.PutObject(context.Background(), c.account, container, obj, common.Map2Headers(headers), src)
}
func (c *directClient) PostObject(container string, obj string, headers map[string]string) *http.Response {
return c.pc.PostObject(context.Background(), c.account, container, obj, common.Map2Headers(headers))
}
func (c *directClient) GetObject(container string, obj string, headers map[string]string) *http.Response {
return c.pc.GetObject(context.Background(), c.account, container, obj, common.Map2Headers(headers))
}
func (c *directClient) HeadObject(container string, obj string, headers map[string]string) *http.Response {
return c.pc.HeadObject(context.Background(), c.account, container, obj, common.Map2Headers(headers))
}
func (c *directClient) DeleteObject(container string, obj string, headers map[string]string) *http.Response {
return c.pc.DeleteObject(context.Background(), c.account, container, obj, common.Map2Headers(headers))
}
func (c *directClient) Raw(method, urlAfterAccount string, headers map[string]string, body io.Reader) *http.Response {
return nectarutil.ResponseStub(http.StatusNotImplemented, "Raw requests not implemented for direct clients")
}