-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_contents.go
58 lines (53 loc) · 1.57 KB
/
get_contents.go
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
package github
import (
"encoding/json"
"fmt"
"io/ioutil"
)
// http://developer.github.com/v3/repos/contents/#get-contents
type GetContents struct {
Owner string
Repo string
Path string
Ref string
}
func (action *GetContents) Execute(client *Client) ([]*Content, error) {
theUrl := "https://api.github.com/repos/" + action.Owner + "/" + action.Repo + "/contents"
if action.Path != "" {
theUrl += "/" + action.Path
}
if action.Ref != "" {
theUrl += "?ref=" + action.Ref
}
rsp, e := client.Get(theUrl)
if e != nil {
return nil, e
}
defer rsp.Body.Close()
if rsp.Status[0] != '2' {
return nil, fmt.Errorf("expected status 2xx, got %v", rsp.Status)
}
b, e := ioutil.ReadAll(rsp.Body)
if e != nil {
return nil, e
}
// this is a brilliant api
var c *Content
e = json.Unmarshal(b, &c)
if e == nil {
return []*Content{c}, nil
}
var cs []*Content
return cs, json.Unmarshal(b, &cs)
}
type Content struct {
Type string `json:"type"` // "file",
Size int64 `json:"size"` // 625,
Name string `json:"name"` // "octokit.rb",
Path string `json:"path"` // "lib/octokit.rb",
Sha string `json:"sha"` // "fff6fe3a23bf1c8ea0692b4a883af99bee26fd3b",
Url string `json:"url"` // "https://api.github.com/repos/pengwynn/octokit/contents/lib/octokit.rb",
GitUrl string `json:"git_url"` // "https://api.github.com/repos/pengwynn/octokit/git/blobs/fff6fe3a23bf1c8ea0692b4a883af99bee26fd3b",
HtmlUrl string `json:"html_url"` // "https://github.com/pengwynn/octokit/blob/master/lib/octokit.rb",
Links *Links `json:"_links"`
}