|
1 | | -// HTTP Client |
| 1 | +// Client for working with the server |
2 | 2 | package client |
3 | 3 |
|
4 | 4 | import ( |
5 | | - "bytes" |
6 | | - "compress/flate" |
7 | | - "compress/gzip" |
8 | 5 | "encoding/json" |
9 | 6 | "errors" |
10 | | - "fmt" |
11 | | - "reflect" |
12 | 7 | "strings" |
13 | | - "time" |
14 | 8 |
|
15 | 9 | "github.com/go-resty/resty/v2" |
16 | 10 | "github.com/srg-bnd/observator/internal/agent/models" |
17 | 11 | "github.com/srg-bnd/observator/internal/server/logger" |
| 12 | + "github.com/srg-bnd/observator/internal/shared/services" |
18 | 13 | "go.uber.org/zap" |
19 | 14 | ) |
20 | 15 |
|
21 | | -var ErrBadHashSum = errors.New("bad hashSHA256") |
| 16 | +var ErrBadHashSum = errors.New("bad hash") |
22 | 17 |
|
| 18 | +// TODO: uses `ChecksumBehaviour` instead of `services.Checksum` |
23 | 19 | type ChecksumBehaviour interface { |
24 | 20 | Sum(string) (string, error) |
25 | 21 | } |
26 | 22 |
|
27 | 23 | // Client |
28 | 24 | type Client struct { |
29 | | - client *resty.Client |
30 | | - baseURL string |
31 | | - checksumService ChecksumBehaviour |
| 25 | + httpClient *resty.Client |
| 26 | + checksumService *services.Checksum |
32 | 27 | } |
33 | 28 |
|
34 | 29 | // Returns a new client |
35 | | -func NewClient(baseURL string, checksumService ChecksumBehaviour) *Client { |
| 30 | +func NewClient(baseURL string, checksumService *services.Checksum) *Client { |
36 | 31 | return &Client{ |
37 | | - client: resty.New(), |
38 | | - // HACK |
39 | | - baseURL: "http://" + baseURL, |
| 32 | + httpClient: newHttpClient(baseURL), |
40 | 33 | checksumService: checksumService, |
41 | 34 | } |
42 | 35 | } |
43 | 36 |
|
44 | | -// Sends batch of metrics to the server |
| 37 | +// Sends batch of metrics |
45 | 38 | func (c *Client) SendMetrics(metrics []models.Metrics) error { |
46 | 39 | data, err := json.Marshal(&metrics) |
47 | 40 | if err != nil { |
48 | 41 | return err |
49 | 42 | } |
50 | 43 |
|
51 | | - // Compress data |
52 | 44 | compressedData, err := compress(data) |
53 | 45 | if err != nil { |
54 | 46 | return err |
55 | 47 | } |
56 | 48 |
|
57 | | - // Retriable errors |
58 | | - repetitionIntervals := [3]int{1, 3, 5} |
59 | | - currentRepetitionInterval := -1 |
60 | | - |
61 | | - // Init client |
62 | | - c.client. |
63 | | - SetRetryCount(len(repetitionIntervals)). |
64 | | - SetRetryAfter(func(c *resty.Client, r *resty.Response) (time.Duration, error) { |
65 | | - currentRepetitionInterval++ |
66 | | - return time.Duration(repetitionIntervals[currentRepetitionInterval]) * time.Second, nil |
67 | | - }). |
68 | | - SetRetryMaxWaitTime(5 * time.Second) |
69 | | - |
70 | | - // Execute a request |
71 | | - request := c.client.R().SetBody(compressedData). |
72 | | - SetHeader("Content-Type", "application/json"). |
73 | | - SetHeader("Accept-Encoding", "gzip"). |
74 | | - SetHeader("Content-Encoding", "gzip") |
75 | | - |
76 | | - if reflect.ValueOf(c.checksumService).Kind() == reflect.Ptr && !reflect.ValueOf(c.checksumService).IsNil() { |
77 | | - hash, err := c.checksumService.Sum(string(data)) |
78 | | - if err != nil { |
79 | | - logger.Log.Warn("bad hash (SHA256)", zap.Error(ErrBadHashSum)) |
80 | | - return err |
81 | | - } else { |
82 | | - request = request.SetHeader("HashSHA256", string(hash)) |
83 | | - } |
| 49 | + request := c.httpClient.R().SetBody(compressedData) |
| 50 | + request, err = c.withChecksum(request, data) |
| 51 | + if err != nil { |
| 52 | + return err |
84 | 53 | } |
85 | 54 |
|
86 | | - res, err := request.Post(c.baseURL + "/updates") |
| 55 | + response, err := request.Post("/updates") |
87 | 56 | if err != nil { |
88 | 57 | return err |
89 | 58 | } |
90 | 59 |
|
91 | | - // Decompress response |
92 | | - if strings.Contains(res.Header().Get("Accept-Encoding"), "gzip") { |
93 | | - decompress(res.Body()) |
| 60 | + if strings.Contains(response.Header().Get("Accept-Encoding"), "gzip") { |
| 61 | + // TODO: use the results |
| 62 | + decompress(response.Body()) |
94 | 63 | } |
95 | 64 |
|
96 | 65 | return nil |
97 | 66 | } |
98 | 67 |
|
99 | | -// Helpers |
100 | | - |
101 | | -// Compress data |
102 | | -func compress(data []byte) ([]byte, error) { |
103 | | - var b bytes.Buffer |
104 | | - |
105 | | - w := gzip.NewWriter(&b) |
106 | | - |
107 | | - _, err := w.Write(data) |
108 | | - if err != nil { |
109 | | - return nil, fmt.Errorf("failed write data to compress temporary buffer: %v", err) |
110 | | - } |
111 | | - |
112 | | - err = w.Close() |
113 | | - if err != nil { |
114 | | - return nil, fmt.Errorf("failed compress data: %v", err) |
| 68 | +// Sets a checksum if need |
| 69 | +func (c *Client) withChecksum(request *resty.Request, data []byte) (*resty.Request, error) { |
| 70 | + if c.checksumService != nil { |
| 71 | + return request, nil |
115 | 72 | } |
116 | 73 |
|
117 | | - return b.Bytes(), nil |
118 | | -} |
119 | | - |
120 | | -// Decompress data |
121 | | -func decompress(compressedData []byte) ([]byte, error) { |
122 | | - r := flate.NewReader(bytes.NewReader(compressedData)) |
123 | | - defer r.Close() |
124 | | - |
125 | | - var b bytes.Buffer |
126 | | - _, err := b.ReadFrom(r) |
| 74 | + hash, err := c.checksumService.Sum(string(data)) |
127 | 75 | if err != nil { |
128 | | - return nil, fmt.Errorf("failed decompress data: %v", err) |
| 76 | + logger.Log.Warn(ErrBadHashSum.Error(), zap.Error(ErrBadHashSum)) |
| 77 | + return request, nil |
129 | 78 | } |
130 | 79 |
|
131 | | - return b.Bytes(), nil |
| 80 | + return request.SetHeader("HashSHA256", string(hash)), nil |
132 | 81 | } |
0 commit comments