This commit is contained in:
2026-01-27 13:57:01 -05:00
parent 28b1ad32f5
commit 7bf4f115b1
4 changed files with 103 additions and 28 deletions

View File

@@ -11,13 +11,14 @@ import (
)
type FetchArgs struct {
URL string `json:"url"`
Options *FetchOptions `json:"options"`
Input string `json:"input"`
Init *FetchOptions `json:"init,omitempty"`
}
type FetchOptions struct {
Method string `json:"method"`
Headers *map[string]string `json:"headers"`
Method string `json:"method,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Body *string `json:"body,omitempty"`
}
func (o *FetchOptions) Defaults() *FetchOptions {
@@ -47,14 +48,14 @@ func Fetch(args FetchArgs) (*FetchResult, error) {
method := "GET"
headers := make(map[string]string)
if args.Options != nil {
method = args.Options.Method
if args.Options.Headers != nil {
maps.Copy(headers, *args.Options.Headers)
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.URL, nil)
req, err := http.NewRequest(method, args.Input, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -102,7 +103,7 @@ func greet(args GreetArgs) string {
}
func init() {
builtin.RegisterBuiltin[FetchArgs, *FetchResult]("fetch", Fetch)
builtin.RegisterBuiltin[FetchArgs, *FetchResult]("fetch", Fetch, builtin.WithPromise())
builtin.RegisterBuiltin[AddArgs, int]("add", add)
builtin.RegisterBuiltin[GreetArgs, string]("greet", greet)
}

View File

@@ -18,7 +18,7 @@ func TestFetch(t *testing.T) {
}))
defer server.Close()
result, err := Fetch(FetchArgs{URL: server.URL})
result, err := Fetch(FetchArgs{Input: server.URL})
require.NoError(t, err)
assert.True(t, result.OK)
@@ -32,7 +32,7 @@ func TestFetch(t *testing.T) {
func TestFetchHTTPBin(t *testing.T) {
t.Skip("httpbin.org test is flaky")
result, err := Fetch(FetchArgs{URL: "https://httpbin.org/get"})
result, err := Fetch(FetchArgs{Input: "https://httpbin.org/get"})
require.NoError(t, err)
assert.True(t, result.OK)
@@ -42,7 +42,7 @@ func TestFetchHTTPBin(t *testing.T) {
}
func TestFetchWith404(t *testing.T) {
result, err := Fetch(FetchArgs{URL: "https://httpbin.org/status/404"})
result, err := Fetch(FetchArgs{Input: "https://httpbin.org/status/404"})
require.NoError(t, err)
assert.False(t, result.OK)
@@ -50,7 +50,7 @@ func TestFetchWith404(t *testing.T) {
}
func TestFetchWithInvalidURL(t *testing.T) {
_, err := Fetch(FetchArgs{URL: "http://this-domain-does-not-exist-12345.com"})
_, err := Fetch(FetchArgs{Input: "http://this-domain-does-not-exist-12345.com"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to fetch")
}
@@ -69,9 +69,9 @@ func TestFetchWithHeaders(t *testing.T) {
}
options := &FetchOptions{
Method: "GET",
Headers: &headers,
Headers: headers,
}
result, err := Fetch(FetchArgs{URL: server.URL, Options: options})
result, err := Fetch(FetchArgs{Input: server.URL, Init: options})
require.NoError(t, err)
assert.True(t, result.OK)
}
@@ -85,7 +85,7 @@ func TestFetchDefaults(t *testing.T) {
defer server.Close()
options := &FetchOptions{}
result, err := Fetch(FetchArgs{URL: server.URL, Options: options})
result, err := Fetch(FetchArgs{Input: server.URL, Init: options})
require.NoError(t, err)
assert.True(t, result.OK)
}