-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
161 lines (112 loc) · 2.86 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
package reader
import (
"context"
"fmt"
"io"
_ "log"
"net/http"
"net/url"
"path/filepath"
"strings"
"time"
"github.com/google/go-github/v48/github"
"github.com/whosonfirst/go-ioutil"
wof_reader "github.com/whosonfirst/go-reader"
"golang.org/x/oauth2"
)
type GitHubAPIReader struct {
wof_reader.Reader
owner string
repo string
prefix string
branch string
client *github.Client
throttle <-chan time.Time
}
func init() {
ctx := context.Background()
err := wof_reader.RegisterReader(ctx, "githubapi", NewGitHubAPIReader)
if err != nil {
panic(err)
}
}
func NewGitHubAPIReader(ctx context.Context, uri string) (wof_reader.Reader, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
rate := time.Second / 3
throttle := time.Tick(rate)
r := &GitHubAPIReader{
throttle: throttle,
}
r.owner = u.Host
path := strings.TrimLeft(u.Path, "/")
parts := strings.Split(path, "/")
if len(parts) != 1 {
return nil, fmt.Errorf("Invalid path")
}
r.repo = parts[0]
r.branch = DEFAULT_BRANCH
q := u.Query()
token := q.Get("access_token")
branch := q.Get("branch")
if token == "" {
return nil, fmt.Errorf("Missing access token")
}
if branch != "" {
r.branch = branch
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
r.client = client
prefix := q.Get("prefix")
r.prefix = prefix
return r, nil
}
func (r *GitHubAPIReader) Read(ctx context.Context, uri string) (io.ReadSeekCloser, error) {
<-r.throttle
url := r.ReaderURI(ctx, uri)
ref := fmt.Sprintf("refs/heads/%s", r.branch)
opts := &github.RepositoryContentGetOptions{
Ref: ref,
}
rsp, _, _, err := r.client.Repositories.GetContents(ctx, r.owner, r.repo, url, opts)
if err != nil {
return nil, fmt.Errorf("Failed to get contents for %s, %w", url, err)
}
body, err := rsp.GetContent()
var rsp_r io.Reader
if err != nil {
// START OF I have no idea why I need to do this, but only sometimes...
if *rsp.Content != "" {
return nil, fmt.Errorf("Failed to read contents for %s, %w", url, err)
}
if *rsp.DownloadURL == "" {
return nil, fmt.Errorf("Failed to read contents for %s and response is missing download URL, %w", url, err)
}
raw_rsp, err := http.Get(*rsp.DownloadURL)
if err != nil {
return nil, fmt.Errorf("Failed to fetch contents from download URL, %w", err)
}
rsp_r = raw_rsp.Body
// END OF I have no idea why I need to do this, but only sometimes...
} else {
rsp_r = strings.NewReader(body)
}
fh, err := ioutil.NewReadSeekCloser(rsp_r)
if err != nil {
return nil, fmt.Errorf("Failed to create ReadSeekCloser for %s, %w", url, err)
}
return fh, nil
}
func (r *GitHubAPIReader) ReaderURI(ctx context.Context, key string) string {
uri := key
if r.prefix != "" {
uri = filepath.Join(r.prefix, key)
}
return uri
}