Add go sdk

This commit is contained in:
Oliver
2026-01-06 14:55:21 +01:00
parent 32bcd6db9e
commit e018bedca3
21 changed files with 1090 additions and 0 deletions

46
client/records.go Normal file
View File

@@ -0,0 +1,46 @@
package client
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
func (c *Client) SendRecord(records []RecordData) error {
dto := createMetricRecordDto{
SenderID: c.senderID,
ProjectID: c.projectID,
Timestamp: time.Now().Unix(),
WriteKey: c.writeKey,
Data: records,
}
body, err := json.Marshal(dto)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, c.baseURL+"/collector/api/v1/records", bytes.NewBuffer(body))
if err != nil {
return fmt.Errorf("could not create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
if c.authToken != "" {
req.Header.Set("Authorization", "Bearer "+c.authToken)
}
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("could not send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return ErrUnexpectedStatusCode
}
return nil
}