-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.append.go
201 lines (176 loc) · 5.13 KB
/
api.append.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2022 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webhdfs
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"github.com/searKing/golang/go/exp/types"
http_ "github.com/searKing/golang/go/net/http"
strings_ "github.com/searKing/golang/go/strings"
"github.com/searKing/golang/go/errors"
)
type AppendRequest struct {
Authentication
ProxyUser
CSRF
HttpRequest
// Path of the object to get.
//
// Path is a required field
Path *string `validate:"required"`
// Object data.
Body io.Reader
// ContentLength records the length of the associated content.
// The value -1 indicates that the length is unknown.
// Values >= 0 indicate that the given number of bytes may
// be read from Body.
//
// For client requests, a value of 0 with a non-nil Body is
// also treated as unknown.
// For HttpFs, ContentType and ContentLength is needed with a non-nil Body
// ContentLength will be computed inner when Body is one of type [*bytes.Buffer, *bytes.Reader, *strings.Reader]
// See https://issues.cloudera.org/browse/HUE-679
// Missing or unknown request method
// Missing URL
// Missing HTTP Identifier (HTTP/1.0)
// Request is too large
// Content-Length missing for POST or PUT requests
// Illegal character in hostname; underscores are not allowed
ContentLength *int64
// Name buffersize
// Description The size of the buffer used in transferring data.
// Type int
// Default Value Specified in the configuration.
// Valid Values > 0
// Syntax Any integer.
BufferSize *int
// Name nodirect
// Description Disable automatically redirected.
// Type bool
// Default Value false
// Valid Values true|false
// Syntax Any Bool.
NoDirect *bool
}
type AppendResponse struct {
NameNode string `json:"-"`
ErrorResponse
HttpResponse `json:"-"`
NoDirect bool `json:"-"`
Location *string `json:"Location"`
}
func (req *AppendRequest) RawPath() string {
return types.Value(req.Path)
}
func (req *AppendRequest) RawQuery() string {
v := url.Values{}
v.Set("op", OpAppend)
if req.Authentication.Delegation != nil {
v.Set("delegation", types.Value(req.Authentication.Delegation))
}
if req.ProxyUser.Username != nil {
v.Set("user.name", types.Value(req.ProxyUser.Username))
}
if req.ProxyUser.DoAs != nil {
v.Set("doas", types.Value(req.ProxyUser.DoAs))
}
if req.BufferSize != nil {
v.Set("buffersize", fmt.Sprintf("%d", types.Value(req.BufferSize)))
}
if req.NoDirect != nil {
v.Set("noredirect", fmt.Sprintf("%t", types.Value(req.NoDirect)))
}
return v.Encode()
}
func (resp *AppendResponse) UnmarshalHTTP(httpResp *http.Response) error {
resp.HttpResponse.UnmarshalHTTP(httpResp)
if isSuccessHttpCode(httpResp.StatusCode) && !resp.NoDirect {
return nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if len(body) == 0 {
return ErrorFromHttpResponse(httpResp)
}
err = json.Unmarshal(body, &resp)
if err != nil {
return fmt.Errorf("parse %s: %w", strings_.Truncate(string(body), MaxHTTPBodyLengthDumped), err)
}
if err := resp.Exception(); err != nil {
return err
}
return nil
}
// Append to a File
// See: https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Append_to_a_File
func (c *Client) Append(req *AppendRequest) (*AppendResponse, error) {
return c.append(nil, req)
}
func (c *Client) AppendWithContext(ctx context.Context, req *AppendRequest) (*AppendResponse, error) {
if ctx == nil {
panic("nil context")
}
return c.append(ctx, req)
}
func (c *Client) append(ctx context.Context, req *AppendRequest) (*AppendResponse, error) {
err := c.opts.Validator.Struct(req)
if err != nil {
return nil, err
}
nameNodes := c.opts.Addresses
if nameNodes == nil {
return nil, fmt.Errorf("missing namenode addresses")
}
var u = c.HttpUrl(req)
var errs []error
for _, addr := range nameNodes {
u.Host = addr
httpReq, err := http.NewRequest(http.MethodPost, u.String(), req.Body)
if err != nil {
return nil, err
}
httpReq.Close = req.HttpRequest.Close
_ = http_.RequestWithBodyRewindable(httpReq)
httpReq.Close = req.HttpRequest.Close
if req.CSRF.XXsrfHeader != nil {
httpReq.Header.Set("X-XSRF-HEADER", types.Value(req.CSRF.XXsrfHeader))
}
// See :https://issues.cloudera.org/browse/HUE-679
httpReq.Header.Set("Content-Type", "application/octet-stream")
if req.ContentLength != nil {
httpReq.ContentLength = types.Value(req.ContentLength)
}
if ctx != nil {
httpReq = httpReq.WithContext(ctx)
}
if req.HttpRequest.PreSendHandler != nil {
httpReq, err = req.HttpRequest.PreSendHandler(httpReq)
if err != nil {
return nil, fmt.Errorf("pre send handled: %w", err)
}
}
httpResp, err := c.httpClient().Do(httpReq)
if err != nil {
errs = append(errs, err)
continue
}
var resp AppendResponse
resp.NameNode = addr
resp.NoDirect = types.Value(req.NoDirect)
if err := resp.UnmarshalHTTP(httpResp); err != nil {
errs = append(errs, err)
continue
}
return &resp, nil
}
return nil, errors.Multi(errs...)
}