This repository has been archived by the owner on Sep 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
api.go
131 lines (112 loc) · 2.5 KB
/
api.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
)
var cmdApi = &Command{
Usage: "api <command>",
Short: "show API for command",
Long: `Opens browser with API details for the specified command.`,
ApiUrl: "",
}
func init() {
cmdApi.Run = runApi
}
func runApi(cmd *Command, args []string) {
if len(args) == 0 {
printUsage()
return
}
if len(args) != 1 {
log.Fatal("too many arguments")
}
for _, cmd := range commands {
if cmd.Name() == args[0] && len(cmd.ApiUrl) > 0 {
// open browser
openBrowser := "open"
if _, err := exec.LookPath("xdg-open"); err == nil {
openBrowser = "xdg-open"
}
exec.Command(openBrowser, cmd.ApiUrl).Start()
return
}
}
fmt.Fprintf(os.Stderr, "No API information for command: %s\n", args[0])
os.Exit(2)
}
// Generic API for accessing ElasticSearch server starts here.
type Request http.Request
func ESReq(method, path string) *Request {
req, err := http.NewRequest(method, esUrl+path, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add("User-Agent", "es/"+Version+" ("+runtime.GOOS+"-"+runtime.GOARCH+")")
req.Header.Add("Accept", "application/json")
return (*Request)(req)
}
func (r *Request) SetBodyJson(data interface{}) {
body, err := json.Marshal(data)
if err != nil {
log.Fatal(err)
}
r.SetBody(bytes.NewReader(body))
r.Header.Set("Content-Type", "application/json")
}
func (r *Request) SetBodyString(body string) {
r.SetBody(strings.NewReader(body))
}
func (r *Request) SetBody(body io.Reader) {
rc, ok := body.(io.ReadCloser)
if !ok && body != nil {
rc = ioutil.NopCloser(body)
}
r.Body = rc
if body != nil {
switch v := body.(type) {
case *strings.Reader:
r.ContentLength = int64(v.Len())
case *bytes.Buffer:
r.ContentLength = int64(v.Len())
}
}
}
func (r *Request) Do(v interface{}) string {
res := checkResponse(http.DefaultClient.Do((*http.Request)(r)))
defer res.Body.Close()
bodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
if v != nil {
jsonErr := json.Unmarshal(bodyBytes, v)
if jsonErr != nil {
log.Fatal(jsonErr)
}
}
return string(bodyBytes)
}
func checkResponse(res *http.Response, err error) *http.Response {
if err != nil {
log.Fatal(err)
}
if res.StatusCode == 401 {
log.Fatal("Unauthorized")
}
if res.StatusCode == 403 {
log.Fatal("Unauthorized")
}
if res.StatusCode < 200 && res.StatusCode > 299 {
log.Fatal("Unexpected error: ", res.Status)
}
return res
}