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

124
client/client.go Normal file
View File

@@ -0,0 +1,124 @@
// Package client provides a client for the FancyAnalytics API.
package client
import (
"net/http"
"time"
"github.com/google/uuid"
)
const (
DefaultBaseURL = "https://fancyanalytics.net"
DefaultMaxIdleConnections = 100
DefaultIdleTimeout = 90 * time.Second
DefaultTimeout = 30 * time.Second
)
var DefaultClient *Client
// Client is a client for the FancyAnalytics API.
type Client struct {
baseURL string
authToken string
senderID string
projectID string
writeKey string
client *http.Client
}
// Configuration holds the configuration for the FancyAnalytics client.
type Configuration struct {
// BaseURL is the base URL of the FancyAnalytics API.
// If empty, DefaultBaseURL is used.
BaseURL string
// SenderID is the sender ID to use for requests.
// If empty a random UUID will be used.
SenderID string
// AuthToken is the authentication token to use for requests. Optional.
AuthToken string
// ProjectID is the project ID to use for requests.
ProjectID string
// WriteKey is the write key for the project. Optional.
WriteKey string
// MaxIdleConnections sets the maximum number of idle (keep-alive) connections across all hosts.
// If zero, DefaultMaxIdleConnections is used.
MaxIdleConnections int
// IdleConnectionTimeout is the maximum amount of time an idle (keep-alive) connection will remain idle before closing itself.
// If zero, DefaultIdleTimeout is used.
IdleConnectionTimeout time.Duration
// RequestTimeout is the maximum amount of time a request can take before timing out.
// If zero, DefaultTimeout is used.
RequestTimeout time.Duration
}
// New creates a new FancyAnalytics client with the given configuration.
func New(cfg Configuration) (*Client, error) {
if cfg.ProjectID == "" {
return nil, ErrNoProjectID
}
if cfg.BaseURL == "" {
cfg.BaseURL = DefaultBaseURL
}
if cfg.SenderID == "" {
cfg.SenderID = uuid.New().String()
}
if cfg.MaxIdleConnections == 0 {
cfg.MaxIdleConnections = DefaultMaxIdleConnections
}
if cfg.IdleConnectionTimeout == 0 {
cfg.IdleConnectionTimeout = DefaultIdleTimeout
}
if cfg.RequestTimeout == 0 {
cfg.RequestTimeout = DefaultTimeout
}
transport := http.Transport{
MaxIdleConns: cfg.MaxIdleConnections,
IdleConnTimeout: cfg.IdleConnectionTimeout,
DisableKeepAlives: false,
DisableCompression: false,
}
c := &http.Client{
Transport: &transport,
Timeout: cfg.RequestTimeout,
}
return &Client{
baseURL: cfg.BaseURL,
authToken: cfg.AuthToken,
projectID: cfg.ProjectID,
writeKey: cfg.WriteKey,
senderID: cfg.SenderID,
client: c,
}, nil
}
// Ping checks if the FancyAnalytics API is reachable.
func (c *Client) Ping() error {
resp, err := c.client.Get(c.baseURL + "/collector/api/v1/ping")
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return ErrUnexpectedStatusCode
}
return nil
}

151
client/client_test.go Normal file
View File

@@ -0,0 +1,151 @@
package client
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestNew(t *testing.T) {
for _, tc := range []struct {
name string
cfg Configuration
exp *Client
expErr error
}{
{
name: "default values",
cfg: Configuration{
AuthToken: "my-token",
ProjectID: "my-project",
},
expErr: nil,
},
{
name: "all values set",
cfg: Configuration{
BaseURL: "https://custom-url.com/",
AuthToken: "my-token",
ProjectID: "my-project",
SenderID: "custom-sender-id",
MaxIdleConnections: 50,
IdleConnectionTimeout: 60 * time.Second,
RequestTimeout: 15 * time.Second,
},
expErr: nil,
},
{
name: "missing project ID",
cfg: Configuration{
AuthToken: "my-token",
},
expErr: ErrNoProjectID,
},
} {
t.Run(tc.name, func(t *testing.T) {
got, err := New(tc.cfg)
if !errors.Is(err, tc.expErr) {
t.Errorf("expected error %v, got %v", tc.expErr, err)
}
// If an error was expected, no need to check further.
if tc.expErr != nil {
return
}
if tc.cfg.BaseURL == "" {
if got.baseURL != DefaultBaseURL {
t.Errorf("expected baseURL %s, got %s", DefaultBaseURL, got.baseURL)
}
} else if got.baseURL != tc.cfg.BaseURL {
t.Errorf("expected baseURL %s, got %s", tc.cfg.BaseURL, got.baseURL)
}
if got.authToken != tc.cfg.AuthToken {
t.Errorf("expected authToken %s, got %s", tc.cfg.AuthToken, got.authToken)
}
if got.projectID != tc.cfg.ProjectID {
t.Errorf("expected projectID %s, got %s", tc.cfg.ProjectID, got.projectID)
}
if tc.cfg.SenderID == "" {
if got.senderID == "" {
t.Error("expected non-empty senderID, got empty")
}
} else if got.senderID != tc.cfg.SenderID {
t.Errorf("expected senderID %s, got %s", tc.cfg.SenderID, got.senderID)
}
if tc.cfg.MaxIdleConnections == 0 {
if got.client.Transport.(*http.Transport).MaxIdleConns != DefaultMaxIdleConnections {
t.Errorf("expected MaxIdleConnections %d, got %d", DefaultMaxIdleConnections, got.client.Transport.(*http.Transport).MaxIdleConns)
}
} else if got.client.Transport.(*http.Transport).MaxIdleConns != tc.cfg.MaxIdleConnections {
t.Errorf("expected MaxIdleConnections %d, got %d", tc.cfg.MaxIdleConnections, got.client.Transport.(*http.Transport).MaxIdleConns)
}
})
}
}
func TestClient_Ping(t *testing.T) {
for _, tc := range []struct {
name string
statusCode int
serverError bool
expErr error
}{
{
name: "successful ping",
statusCode: http.StatusOK,
expErr: nil,
},
{
name: "unexpected status code",
statusCode: http.StatusInternalServerError,
expErr: ErrUnexpectedStatusCode,
},
{
name: "network error",
serverError: true,
expErr: errors.New("network error"),
},
} {
t.Run(tc.name, func(t *testing.T) {
var server *httptest.Server
if tc.serverError {
// Simulate network error by using a closed server
server = httptest.NewServer(http.NotFoundHandler())
server.Close()
} else {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tc.statusCode)
}))
}
client, err := New(Configuration{
BaseURL: server.URL,
AuthToken: "test-token",
ProjectID: "test-project",
})
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
err = client.Ping()
if tc.expErr == nil {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
} else if tc.serverError {
if err == nil {
t.Errorf("expected network error, got nil")
}
} else if !errors.Is(err, tc.expErr) {
t.Errorf("expected error %v, got %v", tc.expErr, err)
}
})
}
}

8
client/errors.go Normal file
View File

@@ -0,0 +1,8 @@
package client
import "errors"
var (
ErrNoProjectID = errors.New("no project ID provided")
ErrUnexpectedStatusCode = errors.New("unexpected status code")
)

46
client/events.go Normal file
View File

@@ -0,0 +1,46 @@
package client
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
func (c *Client) SendEvent(evt *Event) error {
dto := createEventDTO{
ProjectID: c.projectID,
Name: evt.Name,
Timestamp: time.Now(),
Properties: evt.Properties,
WriteKey: c.writeKey,
}
body, err := json.Marshal(dto)
if err != nil {
return fmt.Errorf("could not marshal event: %w", err)
}
req, err := http.NewRequest(http.MethodPost, c.baseURL+"/collector/api/v1/events", 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
}

74
client/events_test.go Normal file
View File

@@ -0,0 +1,74 @@
package client
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestClient_SendEvent(t *testing.T) {
for _, tc := range []struct {
name string
statusCode int
serverError bool
expErr error
}{
{
name: "successful event send",
statusCode: http.StatusOK,
expErr: nil,
},
{
name: "unexpected status code",
statusCode: http.StatusBadRequest,
expErr: ErrUnexpectedStatusCode,
},
{
name: "network error",
serverError: true,
expErr: errors.New("network error"),
},
} {
t.Run(tc.name, func(t *testing.T) {
var server *httptest.Server
if tc.serverError {
server = httptest.NewServer(http.NotFoundHandler())
server.Close()
} else {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tc.statusCode)
}))
}
c, err := New(Configuration{
BaseURL: server.URL,
AuthToken: "token",
ProjectID: "project",
})
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
e := &Event{
Name: "test_event",
Properties: map[string]string{
"key": "value",
},
}
err = c.SendEvent(e)
if tc.expErr == nil {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
} else if tc.serverError {
if err == nil {
t.Errorf("expected network error, got nil")
}
} else if !errors.Is(err, tc.expErr) {
t.Errorf("expected error %v, got %v", tc.expErr, err)
}
})
}
}

38
client/model.go Normal file
View File

@@ -0,0 +1,38 @@
package client
import "time"
type Event struct {
Name string `json:"name"`
Properties map[string]string `json:"properties"`
}
func (e *Event) WithProperty(key, value string) {
if e.Properties == nil {
e.Properties = make(map[string]string)
}
e.Properties[key] = value
}
type createEventDTO struct {
ProjectID string `json:"project_id"`
Name string `json:"name"`
Timestamp time.Time `json:"timestamp,omitempty"`
Properties map[string]string `json:"properties"`
WriteKey string `json:"write_key,omitempty"`
}
type RecordData struct {
Metric string `json:"metric"`
Label string `json:"label,omitempty"`
Value float64 `json:"value"`
}
type createMetricRecordDto struct {
SenderID string `json:"sender_id"`
ProjectID string `json:"project_id"`
Timestamp int64 `json:"timestamp,omitempty"`
WriteKey string `json:"write_key,omitempty"`
Data []RecordData `json:"data"`
}

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
}