@@ -3,7 +3,6 @@ package main
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
- "fmt"
7
6
"io/ioutil"
8
7
"net/http"
9
8
)
@@ -21,6 +20,21 @@ type Gist struct {
21
20
Public bool `json:"public"`
22
21
}
23
22
23
+ type FileResponse struct {
24
+ Size int `json:"size"`
25
+ Type string `json:"type"`
26
+ Language string `json:"laguage"`
27
+ }
28
+
29
+ type GistResponse struct {
30
+ Description string `json:"description"`
31
+ Files map [string ]* FileResponse `json:"files"`
32
+ HtmlUrl string `json:"html_url"`
33
+ Id string `json:"id"`
34
+ Public bool `json:"public"`
35
+ User string `json:"user"`
36
+ }
37
+
24
38
func (gist * Gist ) Create (anonymous bool ) (string , error ) {
25
39
buf := bytes .NewBuffer (nil )
26
40
e := json .NewEncoder (buf )
@@ -34,7 +48,7 @@ func (gist *Gist) Create(anonymous bool) (string, error) {
34
48
}
35
49
36
50
req .Header .Add ("Content-type" , "application/json" )
37
- if ! anonymous {
51
+ if config . APIKey != "" && ! anonymous {
38
52
req .Header .Add ("Authorization" , "token " + config .APIKey )
39
53
}
40
54
@@ -50,15 +64,86 @@ func (gist *Gist) Create(anonymous bool) (string, error) {
50
64
return "" , err
51
65
}
52
66
53
- fmt .Println (string (body ))
67
+ var gistResp GistResponse
68
+ err = json .Unmarshal (body , & gistResp )
69
+ if err != nil {
70
+ return "" , err
71
+ }
54
72
55
- return "" , nil
73
+ return gistResp . HtmlUrl , nil
56
74
}
57
75
58
76
func (gist * Gist ) Update (uid string ) (string , error ) {
59
- return "" , nil
77
+ buf := bytes .NewBuffer (nil )
78
+ e := json .NewEncoder (buf )
79
+ if err := e .Encode (gist ); err != nil {
80
+ return "" , err
81
+ }
82
+
83
+ req , err := http .NewRequest ("PATCH" , GitHubAPIURL + "/gists/" + uid , buf )
84
+ if err != nil {
85
+ return "" , err
86
+ }
87
+
88
+ req .Header .Add ("Content-type" , "application/json" )
89
+ if config .APIKey != "" {
90
+ req .Header .Add ("Authorization" , "token " + config .APIKey )
91
+ }
92
+
93
+ client := & http.Client {}
94
+ resp , err := client .Do (req )
95
+ if err != nil {
96
+ return "" , err
97
+ }
98
+
99
+ defer resp .Body .Close ()
100
+ body , err := ioutil .ReadAll (resp .Body )
101
+ if err != nil {
102
+ return "" , err
103
+ }
104
+
105
+ var gistResp GistResponse
106
+ err = json .Unmarshal (body , & gistResp )
107
+ if err != nil {
108
+ return "" , err
109
+ }
110
+
111
+ return gistResp .HtmlUrl , nil
60
112
}
61
113
62
- func GistList (user string ) ([]* Gist , error ) {
63
- return nil , nil
114
+ func GistList (user string ) ([]* GistResponse , error ) {
115
+ var uri string
116
+ if user != "" {
117
+ uri = "/users/" + user + "/gists"
118
+ }
119
+
120
+ req , err := http .NewRequest ("GET" , GitHubAPIURL + uri , nil )
121
+ if err != nil {
122
+ return nil , err
123
+ }
124
+
125
+ req .Header .Add ("Content-type" , "application/json" )
126
+ if config .APIKey != "" {
127
+ req .Header .Add ("Authorization" , "token " + config .APIKey )
128
+ }
129
+
130
+ client := & http.Client {}
131
+ resp , err := client .Do (req )
132
+ if err != nil {
133
+ return nil , err
134
+ }
135
+
136
+ defer resp .Body .Close ()
137
+ body , err := ioutil .ReadAll (resp .Body )
138
+ if err != nil {
139
+ return nil , err
140
+ }
141
+
142
+ var gistResp []* GistResponse
143
+ err = json .Unmarshal (body , & gistResp )
144
+ if err != nil {
145
+ return nil , err
146
+ }
147
+
148
+ return gistResp , nil
64
149
}
0 commit comments