-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
241 lines (173 loc) · 4.03 KB
/
api.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package github
import (
"context"
"errors"
"github.com/google/go-github/github"
"github.com/whosonfirst/go-ioutil"
"github.com/whosonfirst/go-whosonfirst-iterate/v2/emitter"
"github.com/whosonfirst/go-whosonfirst-iterate/v2/filters"
"golang.org/x/oauth2"
_ "log"
"net/url"
"path/filepath"
"strings"
"strconv"
"time"
)
func init() {
ctx := context.Background()
emitter.RegisterEmitter(ctx, "githubapi", NewGitHubAPIEmitter)
}
type GitHubAPIEmitter struct {
emitter.Emitter
owner string
repo string
branch string
concurrent bool
client *github.Client
throttle <-chan time.Time
filters filters.Filters
}
func NewGitHubAPIEmitter(ctx context.Context, uri string) (emitter.Emitter, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
rate := time.Second / 10
throttle := time.Tick(rate)
em := &GitHubAPIEmitter{
throttle: throttle,
}
em.owner = u.Host
path := strings.TrimLeft(u.Path, "/")
parts := strings.Split(path, "/")
if len(parts) != 1 {
return nil, errors.New("Invalid path")
}
em.repo = parts[0]
em.branch = DEFAULT_BRANCH
q := u.Query()
token := q.Get("access_token")
branch := q.Get("branch")
concurrent := q.Get("concurrent")
if token == "" {
return nil, errors.New("Missing access token")
}
if branch != "" {
em.branch = branch
}
if concurrent != "" {
c, err := strconv.ParseBool(concurrent)
if err != nil {
return nil, err
}
em.concurrent = c
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
em.client = client
f, err := filters.NewQueryFiltersFromQuery(ctx, q)
if err != nil {
return nil, err
}
em.filters = f
return em, nil
}
func (em *GitHubAPIEmitter) WalkURI(ctx context.Context, index_cb emitter.EmitterCallbackFunc, uri string) error {
// log.Printf("Walk %s/%s/%s", em.owner, em.repo, uri)
select {
case <-ctx.Done():
return nil
default:
// pass
}
file_contents, dir_contents, _, err := em.client.Repositories.GetContents(ctx, em.owner, em.repo, uri, nil)
if err != nil {
return err
}
if file_contents != nil {
return em.walkFileContents(ctx, index_cb, file_contents)
}
if dir_contents != nil {
if em.concurrent {
return em.walkDirectoryContentsConcurrently(ctx, index_cb, dir_contents)
} else {
return em.walkDirectoryContents(ctx, index_cb, dir_contents)
}
}
return nil
}
func (em *GitHubAPIEmitter) walkDirectoryContents(ctx context.Context, index_cb emitter.EmitterCallbackFunc, contents []*github.RepositoryContent) error {
for _, e := range contents {
err := em.WalkURI(ctx, index_cb, *e.Path)
if err != nil {
return err
}
}
return nil
}
func (em *GitHubAPIEmitter) walkDirectoryContentsConcurrently(ctx context.Context, index_cb emitter.EmitterCallbackFunc, contents []*github.RepositoryContent) error {
remaining := len(contents)
done_ch := make(chan bool)
err_ch := make(chan error)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
for _, e := range contents {
go func(e *github.RepositoryContent) {
defer func() {
done_ch <- true
}()
err := em.WalkURI(ctx, index_cb, *e.Path)
if err != nil {
err_ch <- err
}
}(e)
}
for remaining > 0 {
select {
case <-done_ch:
remaining -= 1
case err := <-err_ch:
return err
default:
// pass
}
}
return nil
}
func (em *GitHubAPIEmitter) walkFileContents(ctx context.Context, index_cb emitter.EmitterCallbackFunc, contents *github.RepositoryContent) error {
path := *contents.Path
name := *contents.Name
switch filepath.Ext(name) {
case ".geojson":
// continue
default:
return nil
}
body, err := contents.GetContent()
if err != nil {
return err
}
r := strings.NewReader(body)
fh, err := ioutil.NewReadSeekCloser(r)
if err != nil {
return err
}
if em.filters != nil {
ok, err := em.filters.Apply(ctx, fh)
if err != nil {
return err
}
if !ok {
return nil
}
_, err = fh.Seek(0, 0)
if err != nil {
return err
}
}
return index_cb(ctx, path, fh)
}