Created
November 28, 2020 00:55
-
-
Save cyx/e112aed5126ecfa1691a45362c76e4b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package runtime | |
import ( | |
"bytes" | |
"context" | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
) | |
type HTTP struct { | |
URL *url.URL | |
httpClient *http.Client | |
} | |
func (h *HTTP) Execute(ctx context.Context, req Request) (Response, error) { | |
body := bytes.NewReader(req.Payload) | |
r, err := http.NewRequest(http.MethodPost, h.URL.String(), body) | |
if err != nil { | |
return Response{}, err | |
} | |
r = r.WithContext(ctx) | |
res, err := h.httpClient.Do(r) | |
if err != nil { | |
return Response{}, err | |
} | |
defer res.Body.Close() | |
respBody, err := ioutil.ReadAll(res.Body) | |
if err != nil { | |
return Response{}, err | |
} | |
return Response{Payload: respBody}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment