Add Makefile
[vaultmon/.git] / http.go
CommitLineData
9a72579f
SB
1package main
2
3import (
4 "bytes"
5 "crypto/tls"
6 "net/http"
7 "time"
8)
9
10const (
11 connectionTimeout = time.Second * 10
12)
13
14var (
15 transport = &http.Transport{
16 TLSClientConfig: &tls.Config{
17 InsecureSkipVerify: true,
18 },
19 }
20 client = &http.Client{
21 Timeout: connectionTimeout,
22 Transport: transport,
23 }
24)
25
26type HeaderMap = map[string]string
27
28type Connection struct {
29 url string
30 headers HeaderMap
31}
32
33func (c *Connection) Post(message string) (*http.Response, error) {
34 req, err := http.NewRequest("POST", c.url, bytes.NewBufferString(message))
35 if err != nil {
36 return nil, err
37 }
38
39 for k, v := range c.headers {
40 req.Header.Set(k, v)
41 }
42
43 resp, err := client.Do(req)
44 return resp, err
45}
46
47func (c *Connection) Get() (*http.Response, error) {
48 req, err := http.NewRequest("GET", c.url, nil)
49 if err != nil {
50 return nil, err
51 }
52
53 resp, err := client.Do(req)
54 return resp, err
55}
56
57func (c *Connection) Head() (*http.Response, error) {
58 req, err := http.NewRequest("HEAD", c.url, nil)
59 if err != nil {
60 return nil, err
61 }
62
63 resp, err := client.Do(req)
64 return resp, err
65}
66
67func (c *Connection) SetHeaders(headers HeaderMap) {
68 c.headers = headers
69}
70
71func NewConnection(url string) *Connection {
72 return &Connection{url: url}
73}