-
Notifications
You must be signed in to change notification settings - Fork 5
/
methods.go
82 lines (69 loc) · 1.64 KB
/
methods.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
package gogi
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type ListJsonItem struct {
Contents string `json:"contents"`
FileName string `json:"fileName"`
Key string `json:"key"`
Name string `json:"name"`
}
// List lists all defined gitignore types with lines format
func (c *Client) List() (string, error) {
path := fmt.Sprintf("%s/list", typePath)
req, err := c.NewRequest("GET", path, nil)
if err != nil {
return "", err
}
resp, err := c.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
return getResponseBody(resp)
}
// ListJson lists all defined gitignore types with json format.
func (c *Client) ListJson() (map[string]*ListJsonItem, error) {
path := fmt.Sprintf("%s/list", typePath)
req, err := c.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("format", "json")
req.URL.RawQuery = q.Encode()
resp, err := c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var res map[string]*ListJsonItem
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return res, nil
}
// Create creates .gitignore file content for given input type.
func (c *Client) Create(typeName string) (string, error) {
path := fmt.Sprintf("%s/%s", typePath, typeName)
req, err := c.NewRequest("GET", path, nil)
if err != nil {
return "", err
}
resp, err := c.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
return getResponseBody(resp)
}
func getResponseBody(resp *http.Response) (string, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}