Go SDK
The Spartera Go SDK provides native Go methods to interact with the
Spartera REST API for automated asset management, analytics, and billing
operations.
Installation
Install using Go modules:
go get github.com/spartera/go-sdk
Requirements
- Go 1.18 or higher
- Go modules enabled
Features
The Go SDK includes:
- Native Go structs and methods for all API endpoints
- Automated asset management capabilities
- Analytics and billing operations
- Connection management for data platforms
- Marketplace publishing functionality
- Context-aware API calls
- Structured error handling
- Concurrent operation support
Import and Configuration
package main
import (
"context"
"os"
"github.com/spartera/go-sdk/spartera"
)
func main() {
// Initialize client
client := spartera.NewClient(&spartera.Config{
APIKey: os.Getenv("SPARTERA_API_KEY"),
Endpoint: "https://api.spartera.com",
Timeout: 30 * time.Second,
})
}
Quick Start
package main
import (
"context"
"fmt"
"log"
"github.com/spartera/go-sdk/spartera"
)
func main() {
client := spartera.NewClient(&spartera.Config{
APIKey: os.Getenv("SPARTERA_API_KEY"),
})
ctx := context.Background()
// Create data connection
connection, err := client.Connections.Create(ctx, &spartera.ConnectionRequest{
Name: "Postgres Production",
Type: "postgresql",
Credentials: map[string]interface{}{
"host": "db.example.com",
"database": "analytics",
"username": "api_user",
"password": "secure_password",
},
})
if err != nil {
log.Fatal(err)
}
// Create asset
asset, err := client.Assets.Create(ctx, &spartera.AssetRequest{
Name: "Revenue Analytics",
Type: "sql",
Query: "SELECT product_id, revenue FROM sales",
ConnectionID: connection.ID,
})
if err != nil {
log.Fatal(err)
}
// Configure pricing
err = client.Assets.SetPricing(ctx, asset.ID, &spartera.PricingRequest{
Strategy: "per_query",
Price: 0.75,
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Asset created: %s\n", asset.ID)
}
Connection Management
// BigQuery connection
bigqueryConn, err := client.Connections.Create(ctx, &spartera.ConnectionRequest{
Name: "BigQuery Analytics",
Type: "bigquery",
Credentials: map[string]interface{}{
"project_id": "my-gcp-project",
"credentials_json": string(credentialsJSON),
},
})
// Snowflake connection
snowflakeConn, err := client.Connections.Create(ctx, &spartera.ConnectionRequest{
Name: "Snowflake Warehouse",
Type: "snowflake",
Credentials: map[string]interface{}{
"account": "myorg-myaccount",
"username": "analytics_user",
"password": "secure_password",
"warehouse": "COMPUTE_WH",
"database": "ANALYTICS_DB",
},
})
Asset Operations
// List assets with filtering
assets, err := client.Assets.List(ctx, &spartera.AssetListOptions{
Page: 1,
PerPage: 50,
Filter: "active",
SortBy: "created_at",
Order: "desc",
})
// Get specific asset
asset, err := client.Assets.Get(ctx, "asset-id")
// Update asset
updatedAsset, err := client.Assets.Update(ctx, "asset-id", &spartera.AssetUpdateRequest{
Name: "Updated Asset Name",
Description: "New description",
Tags: []string{"analytics", "revenue", "product"},
})
// Delete asset
err = client.Assets.Delete(ctx, "asset-id")
Analytics and Metrics
// Usage analytics
usage, err := client.Analytics.GetUsage(ctx, "asset-id", &spartera.AnalyticsOptions{
StartDate: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
EndDate: time.Date(2024, 1, 31, 23, 59, 59, 0, time.UTC),
Granularity: "daily",
})
// Performance metrics
performance, err := client.Analytics.GetPerformance(ctx, "asset-id")
// Revenue reporting
revenue, err := client.Billing.GetRevenue(ctx, &spartera.RevenueOptions{
StartDate: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
EndDate: time.Date(2024, 1, 31, 23, 59, 59, 0, time.UTC),
GroupBy: "asset",
})
Error Handling
import "github.com/spartera/go-sdk/spartera/errors"
asset, err := client.Assets.Get(ctx, "invalid-id")
if err != nil {
switch e := err.(type) {
case *errors.NotFoundError:
fmt.Printf("Asset not found: %s\n", e.Message)
case *errors.APIError:
fmt.Printf("API Error [%d]: %s\n", e.StatusCode, e.Message)
case *errors.ValidationError:
fmt.Printf("Validation Error: %v\n", e.Fields)
default:
fmt.Printf("Unexpected error: %s\n", err.Error())
}
}
Concurrent Operations
import "sync"
func processConcurrently() {
var wg sync.WaitGroup
assetIDs := []string{"asset-1", "asset-2", "asset-3"}
for _, id := range assetIDs {
wg.Add(1)
go func(assetID string) {
defer wg.Done()
asset, err := client.Assets.Get(ctx, assetID)
if err != nil {
log.Printf("Error fetching asset %s: %v", assetID, err)
return
}
fmt.Printf("Processed asset: %s\n", asset.Name)
}(id)
}
wg.Wait()
}
Custom Types and Structs
type Asset struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Query string `json:"query,omitempty"`
ConnectionID string `json:"connection_id"`
Metadata map[string]interface{} `json:"metadata"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type Connection struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Credentials map[string]interface{} `json:"credentials"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
}
Batch Operations
// Bulk asset creation
requests := []*spartera.AssetRequest{
{
Name: "Sales Report",
Type: "sql",
Query: "SELECT * FROM sales",
},
{
Name: "User Report",
Type: "sql",
Query: "SELECT * FROM users",
},
}
results, err := client.Assets.BulkCreate(ctx, requests)
if err != nil {
log.Fatal(err)
}
