This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
/
api.go
81 lines (60 loc) · 1.43 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
package reader
import (
"context"
"fmt"
"github.com/google/go-github/github"
wof_reader "github.com/whosonfirst/go-whosonfirst-readwrite/reader"
"golang.org/x/oauth2"
"io"
"io/ioutil"
_ "log"
"strings"
"time"
)
type GitHubAPIReader struct {
wof_reader.Reader
owner string
repo string
branch string
client *github.Client
context context.Context
throttle <-chan time.Time
}
func NewGitHubAPIReader(ctx context.Context, owner string, repo string, branch string, token string) (wof_reader.Reader, error) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
// https://github.com/golang/go/wiki/RateLimiting
rate := time.Second / 3
throttle := time.Tick(rate)
r := GitHubAPIReader{
repo: repo,
owner: owner,
branch: branch,
throttle: throttle,
client: client,
context: ctx,
}
return &r, nil
}
func (r *GitHubAPIReader) Read(key string) (io.ReadCloser, error) {
<-r.throttle
url := r.URI(key)
opts := &github.RepositoryContentGetOptions{}
rsp, _, _, err := r.client.Repositories.GetContents(r.context, r.owner, r.repo, url, opts)
if err != nil {
return nil, err
}
body, err := rsp.GetContent()
if err != nil {
return nil, err
}
br := strings.NewReader(body)
fh := ioutil.NopCloser(br)
return fh, nil
}
func (r *GitHubAPIReader) URI(key string) string {
return fmt.Sprintf("data/%s", key)
}