Commit | Line | Data |
---|---|---|
9a72579f SB |
1 | package main |
2 | ||
3 | import ( | |
4 | "bytes" | |
5 | "crypto/tls" | |
6 | "net/http" | |
7 | "time" | |
8 | ) | |
9 | ||
10 | const ( | |
11 | connectionTimeout = time.Second * 10 | |
12 | ) | |
13 | ||
14 | var ( | |
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 | ||
26 | type HeaderMap = map[string]string | |
27 | ||
28 | type Connection struct { | |
29 | url string | |
30 | headers HeaderMap | |
31 | } | |
32 | ||
33 | func (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 | ||
47 | func (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 | ||
57 | func (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 | ||
67 | func (c *Connection) SetHeaders(headers HeaderMap) { | |
68 | c.headers = headers | |
69 | } | |
70 | ||
71 | func NewConnection(url string) *Connection { | |
72 | return &Connection{url: url} | |
73 | } |