Skip to content

Implement http.RoundTripper #10

Open
@EtienneBruines

Description

@EtienneBruines

To make the HTTP function more accessible to Go developers, it would be nice if go-pdk implemented http.RoundTripper to process standard HTTP requests. This way, a larger ecosystem of middleware can be used by plugins.

Ideally, this would allow a plugin like this:

package main

import (
    "net/http"

    "github.com/extism/go-pdk"
)

//export http_get_example1
func http_get_example1() int32 {
    req, _ := http.NewRequest("GET", "https://example.org", nil)
    resp, _ := pdk.HttpClient.Do(req)
    // Process the response

    return 0
}

//export http_get_example2
func http_get_example2() int32 {
    client := http.Client{
        RoundTripper: pdk.RoundTripper{},
    }

    req, _ := http.NewRequest("GET", "https://example.org", nil)
    resp, _ := client.Do(req)
    // Process the response

    return 0
}

There might be quite some things that the pdk.RoundTripper cannot support (streaming responses, for example). But since that's not supported anyways, we could just return an error explaining what isn't supported. For basic requests/responses this should be relatively doable.

The `http.RoundTripper interface is an easy one to implement:

https://github.com/golang/go/blob/a031f4ef83edc132d5f49382bfef491161de2476/src/net/http/client.go#L117-L143

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions