|
| 1 | +package http |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + outgoinghandler "github.com/fermyon/spin-go-sdk/v2/internal/wasi/http/v0.2.0/outgoing-handler" |
| 9 | + "github.com/fermyon/spin-go-sdk/v2/internal/wasi/http/v0.2.0/types" |
| 10 | + "go.bytecodealliance.org/cm" |
| 11 | +) |
| 12 | + |
| 13 | +// NewTransport returns http.RoundTripper backed by Spin SDK |
| 14 | +func NewTransport() http.RoundTripper { |
| 15 | + return &Transport{} |
| 16 | +} |
| 17 | + |
| 18 | +// Transport implements http.RoundTripper |
| 19 | +type Transport struct{} |
| 20 | + |
| 21 | +// RoundTrip makes roundtrip using Spin SDK |
| 22 | +func (r *Transport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 23 | + return Send(req) |
| 24 | +} |
| 25 | + |
| 26 | +// NewClient returns a new HTTP client compatible with the Spin SDK |
| 27 | +func NewClient() *http.Client { |
| 28 | + return &http.Client{ |
| 29 | + Transport: &Transport{}, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func Send(req *http.Request) (*http.Response, error) { |
| 34 | + or, err := NewOutgoingHttpRequest(req) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + result := outgoinghandler.Handle(or, cm.None[types.RequestOptions]()) |
| 40 | + if result.Err() != nil { |
| 41 | + return nil, fmt.Errorf("TODO: convert to readable error") |
| 42 | + } |
| 43 | + |
| 44 | + if result.IsErr() { |
| 45 | + return nil, fmt.Errorf("error is %v", result.Err()) |
| 46 | + } |
| 47 | + |
| 48 | + okresult := result.OK() |
| 49 | + |
| 50 | + //wait until resp is returned |
| 51 | + okresult.Subscribe().Block() |
| 52 | + |
| 53 | + incomingResp := okresult.Get() |
| 54 | + if incomingResp.None() { |
| 55 | + return nil, fmt.Errorf("incoming resp is None") |
| 56 | + } |
| 57 | + |
| 58 | + if incomingResp.Some().IsErr() { |
| 59 | + return nil, fmt.Errorf("error is %v", incomingResp.Some().Err()) |
| 60 | + } |
| 61 | + |
| 62 | + if incomingResp.Some().OK().IsErr() { |
| 63 | + return nil, fmt.Errorf("error is %v", incomingResp.Some().OK().Err()) |
| 64 | + } |
| 65 | + |
| 66 | + okresp := incomingResp.Some().OK().OK() |
| 67 | + var body io.ReadCloser |
| 68 | + if consumeResult := okresp.Consume(); consumeResult.IsErr() { |
| 69 | + return nil, fmt.Errorf("failed to consume incoming request %s", *consumeResult.Err()) |
| 70 | + } else if streamResult := consumeResult.OK().Stream(); streamResult.IsErr() { |
| 71 | + return nil, fmt.Errorf("failed to consume incoming requests's stream %s", streamResult.Err()) |
| 72 | + } else { |
| 73 | + body = NewReadCloser(*streamResult.OK()) |
| 74 | + } |
| 75 | + |
| 76 | + resp := &http.Response{ |
| 77 | + StatusCode: int(okresp.Status()), |
| 78 | + Body: body, |
| 79 | + } |
| 80 | + |
| 81 | + return resp, nil |
| 82 | +} |
| 83 | + |
| 84 | +func Get(url string) (*http.Response, error) { |
| 85 | + req, err := http.NewRequest(http.MethodGet, url, nil) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + return Send(req) |
| 91 | +} |
| 92 | + |
| 93 | +func Post(url string, contentType string, body io.Reader) (*http.Response, error) { |
| 94 | + req, err := http.NewRequest(http.MethodPost, url, body) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + if contentType != "" { |
| 100 | + req.Header.Set("Content-Type", contentType) |
| 101 | + } |
| 102 | + |
| 103 | + return Send(req) |
| 104 | +} |
0 commit comments