Skip to content

Commit 437eefc

Browse files
authored
Added a GetFileContent function which uses the contents endpoint (the same as ListFiles function), but it handles the response differently because the returned response is the file content when the requested path is a file and not a directory. (ktrysmt#201)
1 parent 5fc5788 commit 437eefc

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func (c *Client) doRequest(req *http.Request, emptyResponse bool) (interface{},
332332
var result interface{}
333333
if err := json.Unmarshal(responseBytes, &result); err != nil {
334334
log.Println("Could not unmarshal JSON payload, returning raw response")
335-
return resBody, err
335+
return responseBytes, nil
336336
}
337337
return result, nil
338338
}

repository.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,46 @@ func (r *Repository) Get(ro *RepositoryOptions) (*Repository, error) {
243243
return decodeRepository(response)
244244
}
245245

246-
func (r *Repository) ListFiles(ro *RepositoryFilesOptions) ([]RepositoryFile, error) {
246+
func (r *Repository) buildContentsURL(ro *RepositoryFilesOptions) (string, error) {
247247
filePath := path.Join("/repositories", ro.Owner, ro.RepoSlug, "src", ro.Ref, ro.Path) + "/"
248248

249249
urlStr := r.c.requestUrl(filePath)
250250
url, err := url.Parse(urlStr)
251251
if err != nil {
252-
return nil, err
252+
return "", err
253253
}
254254

255255
query := url.Query()
256256
r.c.addMaxDepthParam(&query, &ro.MaxDepth)
257257
url.RawQuery = query.Encode()
258258

259-
urlStr = url.String()
259+
return url.String(), nil
260+
}
261+
262+
func (r *Repository) GetFileContent(ro *RepositoryFilesOptions) ([]byte, error) {
263+
urlStr, err := r.buildContentsURL(ro)
264+
if err != nil {
265+
return nil, err
266+
}
267+
268+
response, err := r.c.execute("GET", urlStr, "")
269+
if err != nil {
270+
return nil, err
271+
}
272+
273+
content, ok := response.([]byte)
274+
if !ok {
275+
return nil, fmt.Errorf("requested path is not a file")
276+
}
277+
278+
return content, nil
279+
}
280+
281+
func (r *Repository) ListFiles(ro *RepositoryFilesOptions) ([]RepositoryFile, error) {
282+
urlStr, err := r.buildContentsURL(ro)
283+
if err != nil {
284+
return nil, err
285+
}
260286

261287
response, err := r.c.executePaginated("GET", urlStr, "")
262288
if err != nil {

0 commit comments

Comments
 (0)