feat: added repo ListContents and changed GetContents doc to talk about a single file (#485)

fix #484

Co-authored-by: Cameron Braid <cameron@drivenow.com.au>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/485
Reviewed-by: 6543 <6543@obermui.de>
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: cameronbraid <cameronbraid@noreply.gitea.io>
Co-committed-by: cameronbraid <cameronbraid@noreply.gitea.io>
This commit is contained in:
cameronbraid 2021-02-15 14:51:38 +08:00 committed by 6543
parent 95ed973c8d
commit 757f8bdb90

View file

@ -9,6 +9,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
// FileOptions options for all file APIs
@ -120,14 +121,24 @@ func (c *Client) GetFile(user, repo, ref, tree string) ([]byte, *Response, error
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/raw/%s/%s", user, repo, ref, tree), nil, nil)
}
// GetContents get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
// GetContents get the metadata and contents of a file in a repository
// ref is optional
func (c *Client) GetContents(owner, repo, ref, filepath string) (*ContentsResponse, *Response, error) {
cr := new(ContentsResponse)
filepath = strings.TrimPrefix(filepath, "/")
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", owner, repo, filepath, ref), jsonHeader, nil, cr)
return cr, resp, err
}
// ListContents gets a list of entries in a dir
// ref is optional
func (c *Client) ListContents(owner, repo, ref, filepath string) ([]*ContentsResponse, *Response, error) {
cr := make([]*ContentsResponse, 0)
filepath = strings.TrimPrefix(filepath, "/")
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", owner, repo, filepath, ref), jsonHeader, nil, &cr)
return cr, resp, err
}
// CreateFile create a file in a repository
func (c *Client) CreateFile(owner, repo, filepath string, opt CreateFileOptions) (*FileResponse, *Response, error) {
var err error