-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
185 lines (128 loc) · 3.39 KB
/
api.go
File metadata and controls
185 lines (128 loc) · 3.39 KB
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
package reader
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"path/filepath"
"strings"
"time"
"github.com/google/go-github/v74/github"
"github.com/whosonfirst/go-ioutil"
wof_reader "github.com/whosonfirst/go-reader/v2"
"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) Exists(ctx context.Context, uri string) (bool, error) {
<-r.throttle
url := r.ReaderURI(ctx, uri)
ref := fmt.Sprintf("refs/heads/%s", r.branch)
opts := &github.RepositoryContentGetOptions{
Ref: ref,
}
_, _, http_rsp, _ := r.client.Repositories.GetContents(ctx, r.owner, r.repo, url, opts)
switch http_rsp.StatusCode {
case http.StatusOK:
return true, nil
case http.StatusNotFound:
return false, nil
default:
return false, fmt.Errorf("Failed to get contents for %s, %s", url, http_rsp.Status)
}
}
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
}