-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
85 lines (68 loc) · 2.01 KB
/
http.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
package logRushClient
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
var logRushHttpApi = httpClient{}
type httpClient struct{}
func (c httpClient) jsonPostRequest(url string, body interface{}, responseInterface interface{}) error {
jsonBody, _ := json.Marshal(body)
requestBody := bytes.NewBuffer(jsonBody)
response, err := http.Post(url, "application/json", requestBody)
if err != nil {
return err
}
defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
return json.Unmarshal(responseBody, responseInterface)
}
func (c httpClient) RegisterStream(url, name, id, key string) (ApiStreamResponse, error) {
streamResponse := ApiStreamResponse{}
body := map[string]string{
"alias": name,
"id": id,
"key": key,
}
err := c.jsonPostRequest(url+"stream/register", body, &streamResponse)
return streamResponse, err
}
func (c httpClient) UnregisterStream(url, id, key string) (ApiSuccessOrErrorResponse, error) {
streamResponse := ApiSuccessOrErrorResponse{}
body := map[string]string{
"id": id,
"key": key,
}
err := c.jsonPostRequest(url+"stream/unregister", body, &streamResponse)
return streamResponse, err
}
func (c httpClient) Log(url, stream string, log Log) (ApiSuccessOrErrorResponse, error) {
streamResponse := ApiSuccessOrErrorResponse{}
body := map[string]interface{}{
"stream": stream,
"log": log.Log,
"timestamp": log.Timestamp,
}
err := c.jsonPostRequest(url+"log", body, &streamResponse)
return streamResponse, err
}
func (c httpClient) Batch(url, stream string, logs []Log) (ApiSuccessOrErrorResponse, error) {
streamResponse := ApiSuccessOrErrorResponse{}
apiLogs := []map[string]interface{}{}
for _, log := range logs {
apiLogs = append(apiLogs, map[string]interface{}{
"log": log.Log,
"timestamp": log.Timestamp,
})
}
body := map[string]interface{}{
"stream": stream,
"logs": apiLogs,
}
err := c.jsonPostRequest(url+"batch", body, &streamResponse)
return streamResponse, err
}