Skip to content

Commit 989b582

Browse files
committed
Refactored DownloadContentsWithMeta
1 parent c9e1ad0 commit 989b582

2 files changed

Lines changed: 26 additions & 22 deletions

File tree

github/repos_contents.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,28 +181,31 @@ func (s *RepositoriesService) DownloadContents(ctx context.Context, owner, repo,
181181
func (s *RepositoriesService) DownloadContentsWithMeta(ctx context.Context, owner, repo, filepath string, opts *RepositoryContentGetOptions) (io.ReadCloser, *RepositoryContent, *Response, error) {
182182
dir := path.Dir(filepath)
183183
filename := path.Base(filepath)
184-
_, dirContents, resp, err := s.GetContents(ctx, owner, repo, dir, opts)
184+
fileContent, _, resp, err := s.GetContents(ctx, owner, repo, filepath, opts)
185185
if err != nil {
186186
return nil, nil, resp, err
187187
}
188188

189-
for _, contents := range dirContents {
190-
if *contents.Name == filename {
191-
if contents.DownloadURL == nil || *contents.DownloadURL == "" {
192-
return nil, contents, resp, fmt.Errorf("no download link found for %s", filepath)
193-
}
189+
if fileContent != nil {
190+
content, err := fileContent.GetContent()
191+
if err == nil && content != "" {
192+
return io.NopCloser(strings.NewReader(content)), fileContent, resp, nil
193+
}
194194

195-
dlReq, err := http.NewRequestWithContext(ctx, http.MethodGet, *contents.DownloadURL, nil)
196-
if err != nil {
197-
return nil, contents, resp, err
198-
}
199-
dlResp, err := s.client.client.Do(dlReq)
200-
if err != nil {
201-
return nil, contents, &Response{Response: dlResp}, err
202-
}
195+
if fileContent.DownloadURL == nil || *fileContent.DownloadURL == "" {
196+
return nil, fileContent, resp, fmt.Errorf("no download link found for %s", filepath)
197+
}
203198

204-
return dlResp.Body, contents, &Response{Response: dlResp}, nil
199+
dlReq, err := http.NewRequestWithContext(ctx, http.MethodGet, *fileContent.DownloadURL, nil)
200+
if err != nil {
201+
return nil, fileContent, resp, err
202+
}
203+
dlResp, err := s.client.client.Do(dlReq)
204+
if err != nil {
205+
return nil, fileContent, &Response{Response: dlResp}, err
205206
}
207+
208+
return dlResp.Body, fileContent, &Response{Response: dlResp}, nil
206209
}
207210

208211
return nil, nil, resp, fmt.Errorf("no file named %s found in %s", filename, dir)

github/repos_contents_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,14 @@ func TestRepositoriesService_DownloadContentsWithMeta_Success(t *testing.T) {
265265
t.Parallel()
266266
client, mux, serverURL := setup(t)
267267

268-
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
268+
mux.HandleFunc("/repos/o/r/contents/d/f", func(w http.ResponseWriter, r *http.Request) {
269269
testMethod(t, r, "GET")
270-
fmt.Fprint(w, `[{
270+
fmt.Fprint(w, `{
271271
"type": "file",
272272
"name": "f",
273-
"download_url": "`+serverURL+baseURLPath+`/download/f"
274-
}]`)
273+
"download_url": "`+serverURL+baseURLPath+`/download/f",
274+
"content": "foo"
275+
}`)
275276
})
276277
mux.HandleFunc("/download/f", func(w http.ResponseWriter, r *http.Request) {
277278
testMethod(t, r, "GET")
@@ -328,13 +329,13 @@ func TestRepositoriesService_DownloadContentsWithMeta_FailedResponse(t *testing.
328329
t.Parallel()
329330
client, mux, serverURL := setup(t)
330331

331-
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
332+
mux.HandleFunc("/repos/o/r/contents/d/f", func(w http.ResponseWriter, r *http.Request) {
332333
testMethod(t, r, "GET")
333-
fmt.Fprint(w, `[{
334+
fmt.Fprint(w, `{
334335
"type": "file",
335336
"name": "f",
336337
"download_url": "`+serverURL+baseURLPath+`/download/f"
337-
}]`)
338+
}`)
338339
})
339340
mux.HandleFunc("/download/f", func(w http.ResponseWriter, r *http.Request) {
340341
testMethod(t, r, "GET")

0 commit comments

Comments
 (0)