Skip to content

Commit

Permalink
feat(event): Add support for ingest API
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Sep 5, 2024
1 parent eb954b2 commit e2c8182
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
14 changes: 8 additions & 6 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ func (er *EventRequest) Create(ctx context.Context, eventInput *EventInput) (*Ev
}

clientRequest := &ClientRequest{
Path: "events",
Result: &EventResult{},
Body: eventParams,
UseIngestService: true,
Path: "events",
Result: &EventResult{},
Body: eventParams,
}

result, err := er.client.Post(ctx, clientRequest)
Expand Down Expand Up @@ -138,9 +139,10 @@ func (er *EventRequest) Batch(ctx context.Context, batchInput *[]EventInput) (*[
}

clientRequest := &ClientRequest{
Path: "events/batch",
Result: &BatchEventResult{},
Body: eventParams,
UseIngestService: true,
Path: "events/batch",
Result: &BatchEventResult{},
Body: eventParams,
}

result, err := er.client.Post(ctx, clientRequest)
Expand Down
56 changes: 48 additions & 8 deletions lago.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ import (
)

const baseURL string = "https://api.getlago.com"
const baseIngestURL string = "https://ingest.getlago.com"
const apiPath string = "/api/v1/"

type Client struct {
Debug bool
HttpClient *resty.Client
BaseUrl string
BaseIngestUrl string
UseIngestService bool
Debug bool
HttpClient *resty.Client
IngestHttpClient *resty.Client
}

type ClientRequest struct {
Path string
QueryParams map[string]string
Result interface{}
Body interface{}
UseIngestService bool
Path string
QueryParams map[string]string
Result interface{}
Body interface{}
}

type Metadata struct {
Expand All @@ -38,20 +44,30 @@ func New() *Client {
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "lago-go-client github.com/getlago/lago-go-client/v1")

ingestRestyClient := resty.New().
SetBaseURL(url).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "lago-go-client github.com/getlago/lago-go-client/v1")

return &Client{
HttpClient: restyClient,
BaseUrl: url,
BaseIngestUrl: url,
HttpClient: restyClient,
IngestHttpClient: ingestRestyClient,
}
}

func (c *Client) SetApiKey(apiKey string) *Client {
c.HttpClient = c.HttpClient.SetAuthToken(apiKey)
c.IngestHttpClient = c.IngestHttpClient.SetAuthToken(apiKey)

return c
}

func (c *Client) SetBaseURL(url string) *Client {
customURL := fmt.Sprintf("%s%s", url, apiPath)
c.HttpClient = c.HttpClient.SetBaseURL(customURL)
c.IngestHttpClient = c.IngestHttpClient.SetBaseURL(customURL)

return c
}
Expand All @@ -62,6 +78,25 @@ func (c *Client) SetDebug(debug bool) *Client {
return c
}

func (c *Client) SetUseIngestService(useIngestService bool) *Client {
c.UseIngestService = useIngestService

if useIngestService {
c = c.SetIngestBaseUrl(baseIngestURL)
} else {
c.HttpClient.SetBaseURL(c.BaseUrl)
}

return c
}

func (c *Client) SetIngestBaseUrl(url string) *Client {
customURL := fmt.Sprintf("%s%s", url, apiPath)
c.IngestHttpClient = c.IngestHttpClient.SetBaseURL(customURL)

return c
}

func (c *Client) Get(ctx context.Context, cr *ClientRequest) (interface{}, *Error) {
hasResult := cr.Result != nil

Expand Down Expand Up @@ -102,7 +137,12 @@ func (c *Client) Get(ctx context.Context, cr *ClientRequest) (interface{}, *Erro
}

func (c *Client) Post(ctx context.Context, cr *ClientRequest) (interface{}, *Error) {
resp, err := c.HttpClient.R().
httpClient := c.HttpClient
if cr.UseIngestService == true {

Check failure on line 141 in lago.go

View workflow job for this annotation

GitHub Actions / lint

S1002: should omit comparison to bool constant, can be simplified to `cr.UseIngestService` (gosimple)
httpClient = c.IngestHttpClient
}

resp, err := httpClient.R().
SetContext(ctx).
SetError(&Error{}).
SetResult(cr.Result).
Expand Down

0 comments on commit e2c8182

Please sign in to comment.