http

package module
v0.0.0-...-12a90eb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sink/http

A crucible/sink destination that delivers payloads via HTTP POST using the standard library's net/http. Runtime dependencies: the standard library and crucible/sink only, no third-party HTTP client.

reg := httpsink.NewRegistry()
sink.Register(reg, func(_ context.Context, o OrderShipped) sink.Op[httpsink.Doer] {
    body, _ := json.Marshal(o)
    return httpsink.Post("https://hooks.example.com/orders", "application/json", body)
})

m := sink.NewManifold()
m.Attach(httpsink.New(http.DefaultClient, reg))
m.Sink(ctx, OrderShipped{OrderID: "A-1"})

Doer is the narrow surface the destination needs (Do(*http.Request) (*http.Response, error)), satisfied by *http.Client. A non-2xx response is returned as an error that includes the status text. The response body is always drained and closed. Register a transformer per payload type; an unregistered payload is skipped (sink.ErrUnregistered).

PostJSON is a convenience constructor that marshals any value to JSON before posting:

sink.Register(reg, func(_ context.Context, o OrderShipped) sink.Op[httpsink.Doer] {
    return httpsink.PostJSON("https://hooks.example.com/orders", o)
})

Stability

Experimental (pre-v1). The API may change until the suite locks v1.0.0.

License

Apache-2.0. See LICENSE and NOTICE.

Documentation

Overview

Package http is a sink destination that delivers payloads over HTTP using the standard library's net/http. It depends only on the standard library and crucible/sink — there is no third-party SDK dependency. Register a transformer that turns each payload type into a Post or PostJSON operation, then attach the result of New to a sink.Manifold.

Stability

Experimental (pre-v1); the API may change until the suite locks v1.0.0.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(doer Doer, reg *csink.Registry[csink.Op[Doer]], opts ...csink.EmitterOption) csink.Outlet

New builds an Outlet that applies each payload's registered Op[Doer] to doer. The outlet is named "http" unless overridden with sink.WithName.

Example
// SPDX-License-Identifier: Apache-2.0

package main

import (
	"context"
	"fmt"
	"io"
	gohttp "net/http"
	"net/http/httptest"

	csink "github.com/stablekernel/crucible/sink"

	httpsink "github.com/stablekernel/crucible/sink/http"
)

type invoicePaid struct{ InvoiceID string }

func main() {
	// Start a test server that records the request and responds 200 OK.
	var received string
	srv := httptest.NewServer(gohttp.HandlerFunc(func(w gohttp.ResponseWriter, r *gohttp.Request) {
		body, _ := io.ReadAll(r.Body)
		received = string(body)
		w.WriteHeader(gohttp.StatusOK)
	}))
	defer srv.Close()

	reg := httpsink.NewRegistry()
	csink.Register(reg, func(_ context.Context, p invoicePaid) csink.Op[httpsink.Doer] {
		return httpsink.Post(srv.URL+"/webhooks", "application/json", []byte(`{"invoice":"`+p.InvoiceID+`"}`))
	})

	outlet := httpsink.New(srv.Client(), reg)
	_ = outlet.Sink(context.Background(), invoicePaid{InvoiceID: "INV-42"})

	fmt.Println(received)
}
Output:
{"invoice":"INV-42"}

func NewRegistry

func NewRegistry() *csink.Registry[csink.Op[Doer]]

NewRegistry returns an empty registry of Op[Doer] for callers to populate with sink.Register.

func Post

func Post(url, contentType string, body []byte) csink.Op[Doer]

Post returns an Op that sends an HTTP POST request to url with the given content-type header and body. The response body is always drained and closed. A non-2xx status code is returned as an error that includes the status text.

func PostJSON

func PostJSON(url string, v any) csink.Op[Doer]

PostJSON returns an Op that marshals v to JSON and sends an HTTP POST request to url with Content-Type "application/json". The response body is always drained and closed. A non-2xx status code is returned as an error.

Types

type Doer

type Doer interface {
	Do(req *gohttp.Request) (*gohttp.Response, error)
}

Doer is the narrow net/http surface this destination needs. It is satisfied by *http.Client (the standard library's default client), so consumers can wire any *http.Client — custom timeouts, transports, or test servers — without this package importing anything beyond the standard library.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL