An open-source, faster Devin alternative
TypeScript
feat(webhooks): Svix REST client (internal/svix) + live test
Igor Zalutski655fc6438fa5
diff --git a/internal/svix/client.go b/internal/svix/client.gonew file mode 100644index 00000000..f61ddc62--- /dev/null+++ b/internal/svix/client.go@@ -0,0 +1,304 @@+// Package svix is a thin REST client for the Svix managed-webhook API. OC uses+// Svix for webhook delivery (retries, signing, SSRF, logs); this client lets the+// /api/webhooks proxy manage the org's Svix Application + Endpoints and read its+// delivery (attempt) logs. The edge events-ingest Worker sends messages via its+// own (TS) client. See .agents/work/sandbox-webhooks-rearchitecture.md.+//+// Region is encoded in the token suffix (e.g. "sk_….us" → api.us.svix.com).+package svix++import (+ "bytes"+ "context"+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "net/http"+ "strings"+ "time"+)++// Client talks to one Svix region (derived from the token suffix).+type Client struct {+ token string+ baseURL string+ hc *http.Client+}++// NewClient builds a Svix client. token is "sk_<base>.<region>"; the region after+// the final '.' selects the API host (us/eu/in → api.<region>.svix.com). A token+// with no region suffix falls back to api.svix.com.+func NewClient(token string) *Client {+ return &Client{+ token: token,+ baseURL: baseURLForToken(token),+ hc: &http.Client{Timeout: 15 * time.Second},+ }+}++func baseURLForToken(token string) string {+ if i := strings.LastIndex(token, "."); i >= 0 && i < len(token)-1 {+ region := token[i+1:]+ // Region codes are short alphanumerics (us, eu, in); guard against a+ // token that simply contains a '.' in its body.+ if region != "" && len(region) <= 4 {+ return "https://api." + region + ".svix.com"+ }+ }+ return "https://api.svix.com"+}++// Enabled reports whether the client has a token (webhooks are off otherwise).+func (c *Client) Enabled() bool { return c != nil && c.token != "" }++// Error is a classified Svix API error. Transient (5xx / 429 / network) callers+// should retry; permanent (4xx) callers should not.+type Error struct {+ StatusCode int+ Op string+ Body string+}++func (e *Error) Error() string {+ return fmt.Sprintf("svix %s: HTTP %d: %s", e.Op, e.StatusCode, e.Body)+}++// Transient reports whether the error is worth retrying (5xx, 429, or a+// transport error represented as StatusCode 0).+func (e *Error) Transient() bool {+ return e.StatusCode == 0 || e.StatusCode == http.StatusTooManyRequests || e.StatusCode >= 500+}++// IsTransient classifies any error (including a wrapped *Error) for retry.+func IsTransient(err error) bool {+ var se *Error+ if errors.As(err, &se) {+ return se.Transient()+ }+ return err != nil // unknown errors: treat as transient/retryable+}++// do performs one request. body (if non-nil) is JSON-encoded; out (if non-nil)+// is JSON-decoded from a 2xx response. extraHeaders are applied last (e.g.+// idempotency-key). Non-2xx returns a classified *Error.+func (c *Client) do(ctx context.Context, op, method, path string, body, out any, extraHeaders map[string]string) error {+ var rdr io.Reader+ if body != nil {+ b, err := json.Marshal(body)+ if err != nil {+ return fmt.Errorf("svix %s: marshal: %w", op, err)+ }+ rdr = bytes.NewReader(b)+ }+ req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, rdr)+ if err != nil {+ return fmt.Errorf("svix %s: new request: %w", op, err)+ }+ req.Header.Set("Authorization", "Bearer "+c.token)+ req.Header.Set("Accept", "application/json")+ if body != nil {+ req.Header.Set("Content-Type", "application/json")+ }+ for k, v := range extraHeaders {+ req.Header.Set(k, v)+ }+ resp, err := c.hc.Do(req)+ if err != nil {+ return &Error{StatusCode: 0, Op: op, Body: err.Error()}+ }+ defer resp.Body.Close()+ respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))+ if resp.StatusCode < 200 || resp.StatusCode >= 300 {+ return &Error{StatusCode: resp.StatusCode, Op: op, Body: string(respBody)}+ }+ if out != nil && len(respBody) > 0 {+ if err := json.Unmarshal(respBody, out); err != nil {+ return fmt.Errorf("svix %s: decode response: %w", op, err)+ }+ }+ return nil+}++// ---------------------------------------------------------------------------+// Applications (one per org, keyed by uid = org id)+// ---------------------------------------------------------------------------++// Application is the subset of a Svix app we use.+type Application struct {+ ID string `json:"id"`+ UID string `json:"uid"`+}++// EnsureApplication creates (or returns, via get_if_exists) the org's app keyed+// by uid. Idempotent: safe to call on every webhook create.+func (c *Client) EnsureApplication(ctx context.Context, uid, name string) (*Application, error) {+ var app Application+ err := c.do(ctx, "ensure_application", http.MethodPost,+ "/api/v1/app/?get_if_exists=true",+ map[string]any{"uid": uid, "name": name}, &app, nil)+ if err != nil {+ return nil, err+ }+ return &app, nil+}++// ---------------------------------------------------------------------------+// Endpoints (destinations under an app)+// ---------------------------------------------------------------------------++// Endpoint is the subset of a Svix endpoint we surface.+type Endpoint struct {+ ID string `json:"id"`+ URL string `json:"url"`+ Description string `json:"description"`+ Disabled bool `json:"disabled"`+ FilterTypes []string `json:"filterTypes,omitempty"`+ Channels []string `json:"channels,omitempty"`+ Metadata map[string]string `json:"metadata,omitempty"`+ CreatedAt string `json:"createdAt,omitempty"`+}++// EndpointParams is the create/update payload (fields left zero are omitted).+type EndpointParams struct {+ URL string `json:"url"`+ Description string `json:"description,omitempty"`+ FilterTypes []string `json:"filterTypes,omitempty"`+ Channels []string `json:"channels,omitempty"`+ Metadata map[string]string `json:"metadata,omitempty"`+ Disabled *bool `json:"disabled,omitempty"`+}++// CreateEndpoint registers a destination under the org's app. The signing secret+// is generated by Svix; fetch it with GetEndpointSecret.+func (c *Client) CreateEndpoint(ctx context.Context, appID string, p EndpointParams) (*Endpoint, error) {+ var ep Endpoint+ if err := c.do(ctx, "create_endpoint", http.MethodPost,+ "/api/v1/app/"+appID+"/endpoint/", p, &ep, nil); err != nil {+ return nil, err+ }+ return &ep, nil+}++// UpdateEndpoint replaces an endpoint's config (PUT).+func (c *Client) UpdateEndpoint(ctx context.Context, appID, epID string, p EndpointParams) (*Endpoint, error) {+ var ep Endpoint+ if err := c.do(ctx, "update_endpoint", http.MethodPut,+ "/api/v1/app/"+appID+"/endpoint/"+epID+"/", p, &ep, nil); err != nil {+ return nil, err+ }+ return &ep, nil+}++// GetEndpoint fetches a single endpoint.+func (c *Client) GetEndpoint(ctx context.Context, appID, epID string) (*Endpoint, error) {+ var ep Endpoint+ if err := c.do(ctx, "get_endpoint", http.MethodGet,+ "/api/v1/app/"+appID+"/endpoint/"+epID+"/", nil, &ep, nil); err != nil {+ return nil, err+ }+ return &ep, nil+}++// ListEndpoints lists endpoints under an app (first page; iterate=false default).+func (c *Client) ListEndpoints(ctx context.Context, appID string) ([]Endpoint, error) {+ var out struct {+ Data []Endpoint `json:"data"`+ }+ if err := c.do(ctx, "list_endpoints", http.MethodGet,+ "/api/v1/app/"+appID+"/endpoint/?limit=250", nil, &out, nil); err != nil {+ return nil, err+ }+ return out.Data, nil+}++// DeleteEndpoint removes an endpoint (idempotent enough; 404 surfaces as *Error).+func (c *Client) DeleteEndpoint(ctx context.Context, appID, epID string) error {+ return c.do(ctx, "delete_endpoint", http.MethodDelete,+ "/api/v1/app/"+appID+"/endpoint/"+epID+"/", nil, nil, nil)+}++// GetEndpointSecret returns the endpoint's signing secret (whsec_…).+func (c *Client) GetEndpointSecret(ctx context.Context, appID, epID string) (string, error) {+ var out struct {+ Key string `json:"key"`+ }+ if err := c.do(ctx, "get_endpoint_secret", http.MethodGet,+ "/api/v1/app/"+appID+"/endpoint/"+epID+"/secret/", nil, &out, nil); err != nil {+ return "", err+ }+ return out.Key, nil+}++// SetEndpointHeaders sets custom HTTP headers delivered on every request to the+// endpoint — how per-destination registration metadata rides each delivery.+func (c *Client) SetEndpointHeaders(ctx context.Context, appID, epID string, headers map[string]string) error {+ return c.do(ctx, "set_endpoint_headers", http.MethodPut,+ "/api/v1/app/"+appID+"/endpoint/"+epID+"/headers/",+ map[string]any{"headers": headers}, nil, nil)+}++// SendExample sends Svix's example event of the given type to an endpoint. NOTE:+// Svix requires the event type to have a registered schema with examples, else it+// 400s (missing_schema). For POST /api/webhooks/:id/test we instead send a real+// CreateMessage with a synthetic payload, which has no schema dependency.+func (c *Client) SendExample(ctx context.Context, appID, epID, eventType string) error {+ return c.do(ctx, "send_example", http.MethodPost,+ "/api/v1/app/"+appID+"/endpoint/"+epID+"/send-example/",+ map[string]any{"eventType": eventType}, nil, nil)+}++// ---------------------------------------------------------------------------+// Messages (events sent to an app; Svix fans out to matching endpoints)+// ---------------------------------------------------------------------------++// Message is the subset of a created message we surface.+type Message struct {+ ID string `json:"id"`+ EventID string `json:"eventId,omitempty"`+}++// MessageParams is the create-message payload.+type MessageParams struct {+ EventType string // public type, e.g. "sandbox.created"+ EventID string // optional business id; sanitized to Svix's charset+ Payload any // the event envelope (camelCase)+ Channels []string+ // IdempotencyKey dedupes retries (the raw OC event id — colons allowed in+ // the header). Distinct from EventID, which Svix restricts to [A-Za-z0-9-_.].+ IdempotencyKey string+}++// SanitizeEventID maps an OC event id (which uses ':', e.g.+// "sb-x:sandbox.stopped") into Svix's eventId charset [a-zA-Z0-9-_.] by+// replacing ':' with '.'. The raw id should still be passed as the+// Idempotency-Key (header, unrestricted) for dedup.+func SanitizeEventID(id string) string {+ return strings.ReplaceAll(id, ":", ".")+}++// CreateMessage sends an event to the org's app. Svix fans it out to endpoints+// whose filterTypes/channels match. IdempotencyKey makes retries safe (same key+// → same message, no duplicate).+func (c *Client) CreateMessage(ctx context.Context, appID string, m MessageParams) (*Message, error) {+ body := map[string]any{+ "eventType": m.EventType,+ "payload": m.Payload,+ }+ if m.EventID != "" {+ body["eventId"] = SanitizeEventID(m.EventID)+ }+ if len(m.Channels) > 0 {+ body["channels"] = m.Channels+ }+ var hdrs map[string]string+ if m.IdempotencyKey != "" {+ hdrs = map[string]string{"idempotency-key": m.IdempotencyKey}+ }+ var out Message+ if err := c.do(ctx, "create_message", http.MethodPost,+ "/api/v1/app/"+appID+"/msg/", body, &out, hdrs); err != nil {+ return nil, err+ }+ return &out, nil+}diff --git a/internal/svix/client_live_test.go b/internal/svix/client_live_test.gonew file mode 100644index 00000000..4257b203--- /dev/null+++ b/internal/svix/client_live_test.go@@ -0,0 +1,122 @@+package svix++import (+ "context"+ "net/http"+ "os"+ "strings"+ "testing"+ "time"+)++// TestLive exercises the client against the real Svix API. It is skipped unless+// SVIX_API_TOKEN is set, so normal `go test` is unaffected:+//+// SVIX_API_TOKEN=$(cat /tmp/svix_token) go test ./internal/svix/ -run TestLive -v+func TestLive(t *testing.T) {+ token := os.Getenv("SVIX_API_TOKEN")+ if token == "" {+ t.Skip("SVIX_API_TOKEN not set; skipping live Svix test")+ }+ c := NewClient(token)+ ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)+ defer cancel()++ // Region resolution from the token suffix.+ t.Logf("baseURL=%s", c.baseURL)++ // Ensure the example event type exists (idempotent).+ _ = c.do(ctx, "register_event_type", http.MethodPost, "/api/v1/event-type/",+ map[string]any{"name": "sandbox.created", "description": "OC live test"}, nil, nil)++ uid := "oc-livetest-" + strings.ReplaceAll(time.Now().UTC().Format("20060102T150405.000"), ".", "-")++ // EnsureApplication is idempotent on uid.+ app1, err := c.EnsureApplication(ctx, uid, "OC live test")+ if err != nil {+ t.Fatalf("EnsureApplication #1: %v", err)+ }+ app2, err := c.EnsureApplication(ctx, uid, "OC live test")+ if err != nil {+ t.Fatalf("EnsureApplication #2: %v", err)+ }+ if app1.ID != app2.ID {+ t.Fatalf("EnsureApplication not idempotent: %s != %s", app1.ID, app2.ID)+ }+ t.Logf("app=%s (idempotent)", app1.ID)++ // Cleanup the whole app (and its endpoints) at the end.+ defer func() {+ if err := c.do(context.Background(), "delete_app", http.MethodDelete,+ "/api/v1/app/"+app1.ID+"/", nil, nil, nil); err != nil {+ t.Logf("cleanup delete app: %v", err)+ }+ }()++ // Create an endpoint scoped to one event type.+ ep, err := c.CreateEndpoint(ctx, app1.ID, EndpointParams{+ URL: "https://httpbingo.org/post",+ Description: "oc live test",+ FilterTypes: []string{"sandbox.created"},+ })+ if err != nil {+ t.Fatalf("CreateEndpoint: %v", err)+ }+ t.Logf("endpoint=%s", ep.ID)++ // Secret should be a whsec_ key.+ secret, err := c.GetEndpointSecret(ctx, app1.ID, ep.ID)+ if err != nil {+ t.Fatalf("GetEndpointSecret: %v", err)+ }+ if !strings.HasPrefix(secret, "whsec_") {+ t.Fatalf("unexpected secret prefix: %q", firstN(secret, 8))+ }+ t.Logf("secret prefix ok (whsec_…)")++ // Per-destination metadata as custom headers (delivered on every event).+ if err := c.SetEndpointHeaders(ctx, app1.ID, ep.ID, map[string]string{"X-OC-Tenant": "t1"}); err != nil {+ t.Fatalf("SetEndpointHeaders: %v", err)+ }+ t.Logf("custom headers set")++ // List shows the endpoint.+ eps, err := c.ListEndpoints(ctx, app1.ID)+ if err != nil {+ t.Fatalf("ListEndpoints: %v", err)+ }+ if len(eps) != 1 || eps[0].ID != ep.ID {+ t.Fatalf("ListEndpoints = %d eps, want our 1", len(eps))+ }+ t.Logf("list ok (%d endpoint)", len(eps))++ // Test send (backs /api/webhooks/:id/test) — a real message with a synthetic+ // payload (no event-type schema dependency). Colon id sanitized; raw id is+ // the idempotency key.+ msg, err := c.CreateMessage(ctx, app1.ID, MessageParams{+ EventType: "sandbox.created",+ EventID: "sb-livetest:sandbox.created",+ Payload: map[string]any{"type": "sandbox.created", "sandboxId": "sb-livetest"},+ IdempotencyKey: "sb-livetest:sandbox.created",+ })+ if err != nil {+ t.Fatalf("CreateMessage: %v", err)+ }+ if msg.EventID != "sb-livetest.sandbox.created" {+ t.Fatalf("eventId not sanitized: %q", msg.EventID)+ }+ t.Logf("create-message ok (msg=%s eventId=%s)", msg.ID, msg.EventID)++ // Delete endpoint.+ if err := c.DeleteEndpoint(ctx, app1.ID, ep.ID); err != nil {+ t.Fatalf("DeleteEndpoint: %v", err)+ }+ t.Logf("endpoint deleted; live test passed")+}++func firstN(s string, n int) string {+ if len(s) < n {+ return s+ }+ return s[:n]+}diff --git a/internal/svix/client_test.go b/internal/svix/client_test.gonew file mode 100644index 00000000..dbdd5c17--- /dev/null+++ b/internal/svix/client_test.go@@ -0,0 +1,32 @@+package svix++import "testing"++func TestBaseURLForToken(t *testing.T) {+ cases := map[string]string{+ "sk_abc.us": "https://api.us.svix.com",+ "sk_abc.eu": "https://api.eu.svix.com",+ "sk_abc.in": "https://api.in.svix.com",+ "sk_no_region": "https://api.svix.com", // no '.' suffix+ "sk_has.dot.body.us": "https://api.us.svix.com", // region is after the LAST dot+ }+ for token, want := range cases {+ if got := baseURLForToken(token); got != want {+ t.Errorf("baseURLForToken(%q) = %q, want %q", token, got, want)+ }+ }+}++func TestSanitizeEventID(t *testing.T) {+ cases := map[string]string{+ "sb-x:sandbox.stopped": "sb-x.sandbox.stopped",+ "sb-y:sandbox.hibernated:2": "sb-y.sandbox.hibernated.2",+ "already-clean.id_1": "already-clean.id_1",+ "sb-z:sandbox.migrated:worker7": "sb-z.sandbox.migrated.worker7",+ }+ for in, want := range cases {+ if got := SanitizeEventID(in); got != want {+ t.Errorf("SanitizeEventID(%q) = %q, want %q", in, got, want)+ }+ }+}