Add go sdk
This commit is contained in:
46
client/records.go
Normal file
46
client/records.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user