Files
poiesis/internal/standard/fetch.go
2026-01-27 16:31:05 -05:00

123 lines
2.4 KiB
Go

package standard
import (
"context"
"fmt"
"io"
"maps"
"net/http"
"strings"
"reichard.io/poiesis/internal/builtin"
)
type FetchArgs struct {
Input string `json:"input"`
Init *RequestInit `json:"init,omitempty"`
}
func (f FetchArgs) Validate() error {
return nil
}
type RequestInit struct {
Method string `json:"method,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Body *string `json:"body,omitempty"`
}
func (o *RequestInit) Validate() error {
if o.Method == "" {
o.Method = "GET"
}
return nil
}
type Response struct {
OK bool `json:"ok"`
Status int `json:"status"`
Body string `json:"body"`
Headers map[string]string `json:"headers"`
}
type AddArgs struct {
A int `json:"a"`
B int `json:"b"`
}
func (a AddArgs) Validate() error {
return nil
}
type GreetArgs struct {
Name string `json:"name"`
}
func (g GreetArgs) Validate() error {
return nil
}
func Fetch(_ context.Context, args FetchArgs) (Response, error) {
method := "GET"
headers := make(map[string]string)
if args.Init != nil {
method = args.Init.Method
if args.Init.Headers != nil {
maps.Copy(headers, args.Init.Headers)
}
}
req, err := http.NewRequest(method, args.Input, nil)
if err != nil {
return Response{}, fmt.Errorf("failed to create request: %w", err)
}
for k, v := range headers {
req.Header.Set(k, v)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return Response{}, fmt.Errorf("failed to fetch: %w", err)
}
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return Response{}, fmt.Errorf("failed to read body: %w", err)
}
resultHeaders := make(map[string]string)
for key, values := range resp.Header {
if len(values) > 0 {
val := values[0]
resultHeaders[key] = val
resultHeaders[strings.ToLower(key)] = val
}
}
return Response{
OK: resp.StatusCode >= 200 && resp.StatusCode < 300,
Status: resp.StatusCode,
Body: string(body),
Headers: resultHeaders,
}, nil
}
func Add(_ context.Context, args AddArgs) (int, error) {
return args.A + args.B, nil
}
func Greet(_ context.Context, args GreetArgs) (string, error) {
return fmt.Sprintf("Hello, %s!", args.Name), nil
}
func init() {
builtin.RegisterAsyncBuiltin("fetch", Fetch)
builtin.RegisterBuiltin("add", Add)
builtin.RegisterBuiltin("greet", Greet)
}