-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgist.go
170 lines (137 loc) · 3.22 KB
/
gist.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"regexp"
)
const (
DefaultGistName = "a"
GistURI = "gist.github.com"
GitHubAPIURL = "https://api.github.com"
)
type File struct {
Name string `json:"-"`
Content string `json:"content"`
// Response:
Size int `json:"size"`
Type string `json:"type"`
Language string `json:"language"`
}
type Gist struct {
Files map[string]*File `json:"files"`
Description string `json:"description"`
Public bool `json:"public"`
// Response:
HtmlUrl string `json:"html_url"`
Id string `json:"id"`
User string `json:"user"`
}
func (gist *Gist) Create(anonymous bool) (string, error) {
buf := bytes.NewBuffer(nil)
e := json.NewEncoder(buf)
if err := e.Encode(gist); err != nil {
return "", err
}
req, err := http.NewRequest("POST", GitHubAPIURL+"/gists", buf)
if err != nil {
return "", err
}
if config.Token != "" && !anonymous {
req.Header.Add("Authorization", "token "+config.Token)
}
body, err := doRequest(req)
if err != nil {
return "", err
}
if err := json.Unmarshal(body, &gist); err != nil {
return "", err
}
return gist.HtmlUrl, nil
}
func (gist *Gist) Update(uid string) (string, error) {
re := regexp.MustCompile("http(?:s?)://" + GistURI + "/(.+)")
if matched := re.MatchString(uid); matched {
uid = re.ReplaceAllString(uid, "$1")
}
buf := bytes.NewBuffer(nil)
e := json.NewEncoder(buf)
if err := e.Encode(gist); err != nil {
return "", err
}
req, err := http.NewRequest("PATCH", GitHubAPIURL+"/gists/"+uid, buf)
if err != nil {
return "", err
}
if config.Token != "" {
req.Header.Add("Authorization", "token "+config.Token)
}
body, err := doRequest(req)
if err != nil {
return "", err
}
var resp Gist
if err := json.Unmarshal(body, &resp); err != nil {
return "", err
}
return resp.HtmlUrl, nil
}
func GistList() ([]*Gist, error) {
req, err := http.NewRequest("GET", GitHubAPIURL+"/gists", nil)
if err != nil {
return nil, err
}
if config.Token != "" {
req.Header.Add("Authorization", "token "+config.Token)
}
body, err := doRequest(req)
if err != nil {
return nil, err
}
var resp []*Gist
if err := json.Unmarshal(body, &resp); err != nil {
return nil, err
}
return resp, nil
}
func (gist *Gist) Rawify() (string, error) {
if gist.HtmlUrl == "" {
return "", errors.New("HTML URL is empty.")
}
req, _ := http.NewRequest("GET", gist.HtmlUrl, nil)
client := &http.Transport{}
resp, err := client.RoundTrip(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
var url string
if resp.StatusCode == 200 {
url = gist.HtmlUrl + "/raw"
} else if resp.StatusCode == 302 {
gist.HtmlUrl = resp.Header.Get("location")
if url, err = gist.Rawify(); err != nil {
return "", err
}
}
return url, nil
}
func doRequest(req *http.Request) ([]byte, error) {
req.Header.Add("Content-type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 && resp.StatusCode != 201 {
return nil, GistParseError(body)
}
return body, nil
}