forked from broderickhyman/albiondata-client
/
uploader_http.go
55 lines (42 loc) · 1.16 KB
/
uploader_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
package client
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"github.com/ao-data/albiondata-client/log"
)
type httpUploader struct {
baseURL string
transport *http.Transport
}
// newHTTPUploader creates a new HTTP uploader
func newHTTPUploader(url string) uploader {
return &httpUploader{
baseURL: url,
transport: &http.Transport{},
}
}
func (u *httpUploader) sendToIngest(body []byte, topic string, state *albionState) {
client := &http.Client{Transport: u.transport}
fullURL := u.baseURL + "/" + topic
req, err := http.NewRequest("POST", fullURL, bytes.NewBuffer([]byte(body)))
if err != nil {
log.Errorf("Error while create new request: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Errorf("Error while sending ingest with data: %v", err)
return
}
if resp.StatusCode != 200 {
log.Errorf("Got bad response code: %v", resp.StatusCode)
return
}
// See: https://stackoverflow.com/questions/17948827/reusing-http-connections-in-golang
io.Copy(ioutil.Discard, resp.Body)
log.Infof("Successfully sent ingest request to %v", u.baseURL)
defer resp.Body.Close()
}