This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
list.go
143 lines (128 loc) · 2.95 KB
/
list.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
package w3s
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/tomnomnom/linkheader"
)
const maxPageSize = 100
type UploadIterator struct {
paginator *pageIterator
max int
count int
page []*Status
}
// Next retrieves status information for the next upload in the list.
func (li *UploadIterator) Next() (*Status, error) {
li.count++
if li.max > 0 && li.count > li.max {
return nil, io.EOF
}
if len(li.page) > 0 {
item := li.page[0]
li.page = li.page[1:]
return item, nil
}
res, err := li.paginator.Next()
if err != nil {
return nil, err
}
var page []*Status
d := json.NewDecoder(res.Body)
err = d.Decode(&page)
if err != nil {
return nil, err
}
li.page = page
if len(li.page) > 0 {
item := li.page[0]
li.page = li.page[1:]
return item, nil
}
return nil, io.EOF
}
type listConfig struct {
before time.Time
maxResults int
}
// List retrieves the list of uploads to Web3.Storage.
func (c *client) List(ctx context.Context, options ...ListOption) (*UploadIterator, error) {
var cfg listConfig
for _, opt := range options {
if err := opt(&cfg); err != nil {
return nil, err
}
}
fetchNextPage := func(url string) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s%s", c.cfg.endpoint, url), nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.cfg.token))
req.Header.Add("Access-Control-Request-Headers", "Link")
req.Header.Add("X-Client", clientName)
res, err := c.cfg.hc.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected response status: %d", res.StatusCode)
}
return res, nil
}
var before string
if cfg.before.IsZero() {
before = time.Now().Format(iso8601)
} else {
before = cfg.before.Format(iso8601)
}
size := cfg.maxResults
if size > maxPageSize {
size = maxPageSize
}
var urlPath string
if size <= 0 {
urlPath = fmt.Sprintf("/user/uploads?before=%s", url.QueryEscape(before))
} else {
urlPath = fmt.Sprintf("/user/uploads?before=%s&size=%d", url.QueryEscape(before), size)
}
return &UploadIterator{
paginator: newPageIterator(urlPath, fetchNextPage),
max: cfg.maxResults,
}, nil
}
type pageIterator struct {
nextURL string
fetchNextPage func(string) (*http.Response, error)
}
func newPageIterator(url string, fetchNextPage func(string) (*http.Response, error)) *pageIterator {
return &pageIterator{
nextURL: url,
fetchNextPage: fetchNextPage,
}
}
func (pi *pageIterator) Next() (*http.Response, error) {
if pi.nextURL == "" {
return nil, io.EOF
}
res, err := pi.fetchNextPage(pi.nextURL)
if err != nil {
return nil, err
}
linkHdrs := res.Header["Link"]
pi.nextURL = ""
if len(linkHdrs) > 0 {
links := linkheader.Parse(linkHdrs[0])
for _, l := range links {
if l.Rel == "next" {
pi.nextURL = l.URL
break
}
}
}
return res, nil
}