/
queries_gist.go
52 lines (42 loc) · 1.18 KB
/
queries_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
package api
import (
"bytes"
"encoding/json"
)
// Gist represents a GitHub's gist.
type Gist struct {
Description string `json:"description,omitempty"`
Public bool `json:"public,omitempty"`
Files map[GistFilename]GistFile `json:"files,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
}
type GistFilename string
type GistFile struct {
Content string `json:"content,omitempty"`
}
// Create a gist for authenticated user.
//
// GitHub API docs: https://developer.github.com/v3/gists/#create-a-gist
func GistCreate(client *Client, description string, public bool, files map[string]string) (*Gist, error) {
gistFiles := map[GistFilename]GistFile{}
for filename, content := range files {
gistFiles[GistFilename(filename)] = GistFile{content}
}
path := "gists"
body := &Gist{
Description: description,
Public: public,
Files: gistFiles,
}
result := Gist{}
requestByte, err := json.Marshal(body)
if err != nil {
return nil, err
}
requestBody := bytes.NewReader(requestByte)
err = client.REST("POST", path, requestBody, &result)
if err != nil {
return nil, err
}
return &result, nil
}