/
http_connection.go
58 lines (48 loc) · 1.28 KB
/
http_connection.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
package sse
import (
"context"
"errors"
"net/http"
"strconv"
)
// HttpConnection is responsible to establish connection with SSE server.
// Create Request, handle http error and provide a response.
type HttpConnection struct {
httpClient *http.Client
Headers map[string]string
URL string
}
func NewHttpConnection(httpClient *http.Client, sourceUrl string) *HttpConnection {
return &HttpConnection{
httpClient: httpClient,
URL: sourceUrl,
Headers: make(map[string]string),
}
}
func (c *HttpConnection) Request(ctx context.Context, lastEventID int) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, c.URL, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Connection", "keep-alive")
if lastEventID >= 0 {
query := req.URL.Query()
query.Add("start_from", strconv.Itoa(lastEventID))
req.URL.RawQuery = query.Encode()
}
// Add user specified headers
for k, v := range c.Headers {
req.Header.Set(k, v)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New("error invalid connect response code")
}
return resp, err
}