Skip to content

sdk/go: Override http.DefaultTransport #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lann opened this issue Mar 22, 2022 · 2 comments
Closed

sdk/go: Override http.DefaultTransport #206

lann opened this issue Mar 22, 2022 · 2 comments

Comments

@lann
Copy link
Collaborator

lann commented Mar 22, 2022

If we can replace http.DefaultTransport with our implementation then most http libraries that make outbound http requests should magically work.

@radu-matei
Copy link
Member

Are you thinking something similar to:

diff --git a/examples/http-tinygo-outbound-http/main.go b/examples/http-tinygo-outbound-http/main.go
index c00f286..cebfc89 100644
--- a/examples/http-tinygo-outbound-http/main.go
+++ b/examples/http-tinygo-outbound-http/main.go
@@ -31,5 +31,13 @@ func main() {
 		if err != nil {
 			fmt.Fprintf(os.Stderr, "Cannot send HTTP request: %v", err)
 		}
+
+		var client = http.DefaultClient
+		client.Transport = &spin_http.WASIHTTPTransport{}
+
+		_, err = client.Get("https://some-random-api.ml/facts/dog")
+		if err != nil {
+			fmt.Fprintf(os.Stderr, "Cannot send HTTP request: %v", err)
+		}
 	})
 }
diff --git a/sdk/go/http/http.go b/sdk/go/http/http.go
index 5107011..5f4bd15 100644
--- a/sdk/go/http/http.go
+++ b/sdk/go/http/http.go
@@ -41,3 +41,12 @@ func Post(url string, contentType string, body io.Reader) (*http.Response, error
 func Send(req *http.Request) (*http.Response, error) {
 	return send(req)
 }
+
+// WASIHTTPTransport implements the Go http.RoundTripper interface so that
+// an http.Client can be used from a WASI application.
+type WASIHTTPTransport struct{}
+
+// RoundTrip implements the go.RoundTripper interface using the WASI outbound HTTP implementation.
+func (t *WASIHTTPTransport) RoundTrip(r *http.Request) (*http.Response, error) {
+	return Send(r)
+}

This ends up compiling something related to timings (I assume because of timeouts), and it fails at runtime because of tinygo-org/tinygo#1037:

TRACE build: spin_engine: Created module for component tinygo-hello from file "/Users/radu/projects/src/github.com/fermyon/spin/examples/http-tinygo-outbound-http/main.wasm"    
Error: unknown import: `env::time.resetTimer` has not been defined

@lann
Copy link
Collaborator Author

lann commented Mar 24, 2022

Yes, that is basically what I had in mind (though I believe you can set http.DefaultTransport = WASIHTTPTransport{} and support more packages).

it fails at runtime because of tinygo-org/tinygo#1037

That's unfortunate! Maybe we can just let this issue be our tracker for if/when that is resolved.

@kate-goldenring kate-goldenring moved this to 🆕 Triage Needed in Spin Triage Aug 16, 2022
@lann lann moved this from 🆕 Triage Needed to 📋 Investigating in Spin Triage Sep 19, 2022
@lann lann closed this as completed Aug 15, 2023
@github-project-automation github-project-automation bot moved this from 📋 Investigating / Open for Comment to ✅ Done in Spin Triage Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants