Skip to content

Commit 2067d4b

Browse files
committed
Refactored DownloadContentsWithMeta method, used bcombined approach, updated tests
1 parent a795cf3 commit 2067d4b

2 files changed

Lines changed: 40 additions & 13 deletions

File tree

github/repos_contents.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,21 +191,30 @@ func (s *RepositoriesService) DownloadContentsWithMeta(ctx context.Context, owne
191191
if err == nil && content != "" {
192192
return io.NopCloser(strings.NewReader(content)), fileContent, resp, nil
193193
}
194+
}
194195

195-
if fileContent.DownloadURL == nil || *fileContent.DownloadURL == "" {
196-
return nil, fileContent, resp, fmt.Errorf("no download link found for %s", filepath)
197-
}
196+
_, dirContents, resp, err := s.GetContents(ctx, owner, repo, dir, opts)
197+
if err != nil {
198+
return nil, nil, resp, err
199+
}
198200

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
206-
}
201+
for _, contents := range dirContents {
202+
if contents.GetName() == filename {
203+
if contents.GetDownloadURL() == "" {
204+
return nil, contents, resp, fmt.Errorf("no download link found for %s", filepath)
205+
}
207206

208-
return dlResp.Body, fileContent, &Response{Response: dlResp}, nil
207+
dlReq, err := http.NewRequestWithContext(ctx, http.MethodGet, *contents.DownloadURL, nil)
208+
if err != nil {
209+
return nil, contents, resp, err
210+
}
211+
dlResp, err := s.client.client.Do(dlReq)
212+
if err != nil {
213+
return nil, contents, &Response{Response: dlResp}, err
214+
}
215+
216+
return dlResp.Body, contents, &Response{Response: dlResp}, nil
217+
}
209218
}
210219

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

github/repos_contents_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,29 @@ func TestRepositoriesService_DownloadContentsWithMeta_FailedResponse(t *testing.
329329
t.Parallel()
330330
client, mux, serverURL := setup(t)
331331

332+
downloadURL := fmt.Sprintf("%s%s/download/f", serverURL, baseURLPath)
333+
332334
mux.HandleFunc("/repos/o/r/contents/d/f", func(w http.ResponseWriter, r *http.Request) {
333335
testMethod(t, r, "GET")
334336
fmt.Fprint(w, `{
335337
"type": "file",
336338
"name": "f",
337-
"download_url": "`+serverURL+baseURLPath+`/download/f"
339+
"download_url": "`+downloadURL+`"
338340
}`)
339341
})
340342
mux.HandleFunc("/download/f", func(w http.ResponseWriter, r *http.Request) {
341343
testMethod(t, r, "GET")
342344
w.WriteHeader(http.StatusInternalServerError)
343345
fmt.Fprint(w, "foo error")
344346
})
347+
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
348+
testMethod(t, r, "GET")
349+
fmt.Fprint(w, `[{
350+
"type": "file",
351+
"name": "f",
352+
"download_url": "`+downloadURL+`"
353+
}]`)
354+
})
345355

346356
ctx := context.Background()
347357
r, c, resp, err := client.Repositories.DownloadContentsWithMeta(ctx, "o", "r", "d/f", nil)
@@ -376,6 +386,14 @@ func TestRepositoriesService_DownloadContentsWithMeta_NoDownloadURL(t *testing.T
376386
t.Parallel()
377387
client, mux, _ := setup(t)
378388

389+
mux.HandleFunc("/repos/o/r/contents/d/f", func(w http.ResponseWriter, r *http.Request) {
390+
testMethod(t, r, "GET")
391+
fmt.Fprint(w, `{
392+
"type": "file",
393+
"name": "f",
394+
}`)
395+
})
396+
379397
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
380398
testMethod(t, r, "GET")
381399
fmt.Fprint(w, `[{

0 commit comments

Comments
 (0)