/
iterator.go
169 lines (149 loc) · 4.56 KB
/
iterator.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
/******************************************************************************
*
* Copyright 2018 Stefan Majewsky <majewsky@gmx.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package schwift
import (
"encoding/json"
"net/http"
"strconv"
"strings"
)
// iteratorInterface allows iteratorBase to access public attributes of
// ContainerIterator/ObjectIterator.
type iteratorInterface interface {
getAccount() *Account
getContainerName() string
getDelimiter() string
getPrefix() string
getOptions() *RequestOptions
//putHeader initializes the AccountHeaders/ContainerHeaders field of the
//Account/Container using the response headers from the GET request.
putHeader(http.Header) error
}
func (i ContainerIterator) getAccount() *Account { return i.Account }
func (i ContainerIterator) getContainerName() string { return "" }
func (i ContainerIterator) getDelimiter() string { return "" }
func (i ContainerIterator) getPrefix() string { return i.Prefix }
func (i ContainerIterator) getOptions() *RequestOptions { return i.Options }
func (i ContainerIterator) putHeader(hdr http.Header) error {
headers := AccountHeaders{headersFromHTTP(hdr)}
if err := headers.Validate(); err != nil {
return err
}
i.Account.headers = &headers
return nil
}
func (i ObjectIterator) getAccount() *Account { return i.Container.Account() }
func (i ObjectIterator) getContainerName() string { return i.Container.Name() }
func (i ObjectIterator) getDelimiter() string { return i.Delimiter }
func (i ObjectIterator) getPrefix() string { return i.Prefix }
func (i ObjectIterator) getOptions() *RequestOptions { return i.Options }
func (i ObjectIterator) putHeader(hdr http.Header) error {
headers := ContainerHeaders{headersFromHTTP(hdr)}
if err := headers.Validate(); err != nil {
return err
}
i.Container.headers = &headers
return nil
}
// iteratorBase provides shared behavior for ContainerIterator and ObjectIterator.
type iteratorBase struct {
i iteratorInterface
marker string
eof bool
}
func (b *iteratorBase) request(limit int, detailed bool) Request {
r := Request{
Method: "GET",
ContainerName: b.i.getContainerName(),
Options: cloneRequestOptions(b.i.getOptions(), nil),
}
if delimiter := b.i.getDelimiter(); delimiter != "" {
r.Options.Values.Set("delimiter", delimiter)
}
if prefix := b.i.getPrefix(); prefix != "" {
r.Options.Values.Set("prefix", prefix)
}
if b.marker == "" {
r.Options.Values.Del("marker")
} else {
r.Options.Values.Set("marker", b.marker)
}
if limit < 0 {
r.Options.Values.Del("limit")
} else {
r.Options.Values.Set("limit", strconv.FormatUint(uint64(limit), 10))
}
if detailed {
r.Options.Headers.Set("Accept", "application/json")
r.Options.Values.Set("format", "json")
r.ExpectStatusCodes = []int{200}
} else {
r.Options.Headers.Set("Accept", "text/plain")
r.Options.Values.Set("format", "plain")
r.ExpectStatusCodes = []int{200, 204}
}
return r
}
func (b *iteratorBase) nextPage(limit int) ([]string, error) {
if b.eof {
return nil, nil
}
resp, err := b.request(limit, false).Do(b.i.getAccount().backend)
if err != nil {
return nil, err
}
buf, err := collectResponseBody(resp)
if err != nil {
return nil, err
}
bufStr := strings.TrimSuffix(string(buf), "\n")
var result []string
if bufStr != "" {
result = strings.Split(bufStr, "\n")
}
if len(result) == 0 {
b.eof = true
b.marker = ""
} else {
b.eof = false
b.marker = result[len(result)-1]
}
return result, b.i.putHeader(resp.Header)
}
func (b *iteratorBase) nextPageDetailed(limit int, data interface{}) error {
if b.eof {
return nil
}
resp, err := b.request(limit, true).Do(b.i.getAccount().backend)
if err != nil {
return err
}
err = json.NewDecoder(resp.Body).Decode(&data)
closeErr := resp.Body.Close()
if err == nil {
err = closeErr
}
if err == nil {
err = b.i.putHeader(resp.Header)
}
return err
}
func (b *iteratorBase) setMarker(marker string) {
b.marker = marker
b.eof = marker == ""
}