-
Notifications
You must be signed in to change notification settings - Fork 2
/
log_entries_client.go
111 lines (92 loc) · 3.28 KB
/
log_entries_client.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
// Package logentries_goclient provides a logentries client which allows the interaction with logentries rest API
// via the seamless resource interfaces exposed. Examples include:
// - LogSets
// - Logs
// - Tags
// - Labels
package logentries_goclient
import (
"fmt"
httpGoClient "github.com/dikhan/http_goclient"
"io/ioutil"
"net/http"
)
const LOG_ENTRIES_API = "https://rest.logentries.com"
type LogEntriesClient struct {
LogSets LogSets
Logs Logs
Tags Tags
Labels Labels
}
// NewLogEntriesClient creates a logentries client which exposes an interface with CRUD operations for each of the
// resources provided by logentries rest API
func NewLogEntriesClient(apiKey string) (LogEntriesClient, error) {
if apiKey == "" {
return LogEntriesClient{}, fmt.Errorf("apiKey is mandatory to initialise Logentries client")
}
client := &httpGoClient.HttpClient{&http.Client{}}
return newLogEntriesClient(apiKey, client)
}
func newLogEntriesClient(apiKey string, httpClient *httpGoClient.HttpClient) (LogEntriesClient, error) {
c := &client{LOG_ENTRIES_API, apiKey, httpClient}
return LogEntriesClient{
LogSets: newLogSets(c),
Logs: newLogs(c),
Tags: newTags(c),
Labels: newLabels(c),
}, nil
}
type client struct {
logEntriesUrl string
api_key string
httpClient *httpGoClient.HttpClient
}
func (c *client) requestHeaders() map[string]string {
headers := map[string]string{}
headers["x-api-key"] = c.api_key
return headers
}
func (c *client) get(path string, in interface{}) error {
url := c.getLogEntriesUrl(path)
res, err := c.httpClient.Get(url, c.requestHeaders(), in)
return checkResponseStatusCode(res, err, http.StatusOK)
}
func (c *client) post(path string, in interface{}, out interface{}) error {
url := c.getLogEntriesUrl(path)
res, err := c.httpClient.PostJson(url, c.requestHeaders(), in, out)
return checkResponseStatusCode(res, err, http.StatusCreated)
}
func (c *client) put(path string, in interface{}, out interface{}) error {
url := c.getLogEntriesUrl(path)
res, err := c.httpClient.PutJson(url, c.requestHeaders(), in, out)
return checkResponseStatusCode(res, err, http.StatusOK)
}
func (c *client) deleteWithStatus(path string, customStatus int) error {
url := c.getLogEntriesUrl(path)
res, err := c.httpClient.Delete(url, c.requestHeaders())
return checkResponseStatusCode(res, err, customStatus)
}
func (c *client) delete(path string) error {
url := c.getLogEntriesUrl(path)
res, err := c.httpClient.Delete(url, c.requestHeaders())
return checkResponseStatusCode(res, err, http.StatusNoContent)
}
func (c *client) getLogEntriesUrl(path string) string {
return fmt.Sprintf("%s%s", c.logEntriesUrl, path)
}
func checkResponseStatusCode(res *http.Response, err error, expectedResponseStatusCode int) error {
if err != nil {
return fmt.Errorf("\nReceived unexpected error response: '%s'", err.Error())
}
defer res.Body.Close()
var bodyBytes []byte
bodyBytes, err = ioutil.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("\nReceived unexpected error response: '%s'", err.Error())
}
bodyString := string(bodyBytes)
if res.StatusCode != expectedResponseStatusCode {
return fmt.Errorf("\nReceived a non expected response status code %d, expected code was %d. Response: %s", res.StatusCode, expectedResponseStatusCode, bodyString)
}
return nil
}