-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
73 lines (62 loc) · 1.56 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
70
71
72
73
package fullstory
import (
"bytes"
"context"
"fmt"
"net/http"
"time"
)
// Config is the FullStory API config.
//
// It can be loaded via github.com/kelseyhightower/envconfig
type Config struct {
// OrgID is the fullstory organization id
OrgID string `envconfig:"FULLSTORY_ORG_ID"`
// APIKey is the fullstory api key
APIKey string `envconfig:"FULLSTORY_API_KEY"`
}
// Client is the FullStory API client interface
type Client interface {
CreateEvent(ctx context.Context, req *CreateEventRequest) error
}
type client struct {
// APIKey is the fullstory api key
APIKey string
// Host is the fullstory host
Host string
// OrgID is the fullstory organization id
OrgID string
// httpClient is the http client
httpClient *http.Client
}
// NewClient returns a new FullStory API Client
func NewClient(cfg *Config) Client {
return &client{
APIKey: cfg.APIKey,
Host: "https://api.fullstory.com",
OrgID: cfg.OrgID,
httpClient: &http.Client{
Timeout: 10 * time.Second,
},
}
}
func (c *client) CreateEvent(ctx context.Context, req *CreateEventRequest) error {
byt, err := req.MarshalJSON()
if err != nil {
return err
}
r, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/v2/events", c.Host), bytes.NewReader(byt))
if err != nil {
return err
}
r.Header.Add("Authorization", "Basic "+c.APIKey)
r.Header.Add("Content-Type", "application/json")
res, err := c.httpClient.Do(r)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("failed to create new event: %s", res.Status)
}
return nil
}