forked from grafana-tools/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-request.go
110 lines (94 loc) · 3.36 KB
/
rest-request.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
package sdk
/*
Copyright 2016-2017 Alexander I.Grafov <grafov@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
ॐ तारे तुत्तारे तुरे स्व
*/
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"strings"
)
// DefaultHTTPClient initialized Grafana with appropriate conditions.
// It allows you globally redefine HTTP client.
var DefaultHTTPClient = http.DefaultClient
// Client uses Grafana REST API for interacting with Grafana server.
type Client struct {
baseURL string
key string
basicAuth bool
client *http.Client
}
// StatusMessage reflects status message as it returned by Grafana REST API.
type StatusMessage struct {
ID *uint `json:"id"`
OrgID *uint `json:"orgId"`
Message *string `json:"message"`
Slug *string `json:"slug"`
Version *int `json:"version"`
Status *string `json:"resp"`
}
// NewClient initializes client for interacting with an instance of Grafana server;
// apiKeyOrBasicAuth accepts either 'username:password' basic authentication credentials,
// or a Grafana API key
func NewClient(apiURL, apiKeyOrBasicAuth string, client *http.Client) *Client {
key := ""
basicAuth := strings.Contains(apiKeyOrBasicAuth, ":")
baseURL, _ := url.Parse(apiURL)
if !basicAuth {
key = fmt.Sprintf("Bearer %s", apiKeyOrBasicAuth)
} else {
parts := strings.Split(apiKeyOrBasicAuth, ":")
baseURL.User = url.UserPassword(parts[0], parts[1])
}
return &Client{baseURL: baseURL.String(), basicAuth: basicAuth, key: key, client: client}
}
func (r *Client) get(query string, params url.Values) ([]byte, int, error) {
return r.doRequest("GET", query, params, nil)
}
func (r *Client) patch(query string, params url.Values, body []byte) ([]byte, int, error) {
return r.doRequest("PATCH", query, params, bytes.NewBuffer(body))
}
func (r *Client) put(query string, params url.Values, body []byte) ([]byte, int, error) {
return r.doRequest("PUT", query, params, bytes.NewBuffer(body))
}
func (r *Client) post(query string, params url.Values, body []byte) ([]byte, int, error) {
return r.doRequest("POST", query, params, bytes.NewBuffer(body))
}
func (r *Client) delete(query string) ([]byte, int, error) {
return r.doRequest("DELETE", query, nil, nil)
}
func (r *Client) doRequest(method, query string, params url.Values, buf io.Reader) ([]byte, int, error) {
u, _ := url.Parse(r.baseURL)
u.Path = path.Join(u.Path, query)
if params != nil {
u.RawQuery = params.Encode()
}
req, err := http.NewRequest(method, u.String(), buf)
if !r.basicAuth {
req.Header.Set("Authorization", r.key)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "autograf")
resp, err := r.client.Do(req)
if err != nil {
return nil, 0, err
}
data, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
return data, resp.StatusCode, err
}