-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
69 lines (60 loc) · 1.52 KB
/
client.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
59
60
61
62
63
64
65
66
67
68
69
package logtail
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type Client struct {
server string
authToken string
defaultContentType ContentType
defaultMarshaler Marshaler
httpClient *http.Client
}
var defaultServerUrl = "https://in.logtail.com"
func NewClient(authToken string, options ...Option) *Client {
client := &Client{
authToken: authToken,
server: defaultServerUrl,
httpClient: http.DefaultClient,
defaultContentType: Json,
defaultMarshaler: json.Marshal,
}
for _, option := range options {
option.apply(client)
}
return client
}
func (c Client) Send(payload any) (n int, err error) {
body, err := c.defaultMarshaler(payload)
if err != nil {
return 0, err
}
return c.Write(body)
}
func (c Client) Write(body []byte) (n int, err error) {
request, _ := http.NewRequest(http.MethodPost, c.server, bytes.NewBuffer(body))
request.Header.Set("Content-Type", string(c.defaultContentType))
request.Header.Set("Authorization", c.authToken)
response, err := c.httpClient.Do(request)
if err != nil {
if response != nil {
response.Body.Close()
}
return 0, fmt.Errorf("log send: %w", err)
}
defer response.Body.Close()
// Check response status, statusCode 202 means success
if response.StatusCode != 202 {
switch response.StatusCode {
case 504:
return 0, InvalidSourceToken
case 406:
return 0, InvalidBodyFormat
default:
return 0, fmt.Errorf("log send failed: %s", response.Status)
}
}
return len(body), nil
}