aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/pkg/net.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/pkg/net.go')
-rw-r--r--internal/pkg/net.go51
1 files changed, 25 insertions, 26 deletions
diff --git a/internal/pkg/net.go b/internal/pkg/net.go
index fb902213..74eeca53 100644
--- a/internal/pkg/net.go
+++ b/internal/pkg/net.go
@@ -11,10 +11,14 @@ import (
"hakurei.app/container/check"
)
-// An httpArtifact is an [Artifact] backed by an [http] request.
+// An httpArtifact is an [Artifact] backed by a [http] url string. The method is
+// hardcoded as [http.MethodGet]. Request body is not allowed because it cannot
+// be deterministically represented by Params.
type httpArtifact struct {
- // Caller-supplied request.
- req *http.Request
+ // Caller-supplied context.
+ ctx context.Context
+ // Caller-supplied url string.
+ url string
// Caller-supplied checksum of the response body. This is validated during
// curing and the first call to Data.
@@ -30,15 +34,6 @@ type httpArtifact struct {
mu sync.Mutex
}
-// NewHTTP returns a new [File] backed by the supplied client and request. If
-// c is nil, [http.DefaultClient] is used instead.
-func NewHTTP(c *http.Client, req *http.Request, checksum Checksum) File {
- if c == nil {
- c = http.DefaultClient
- }
- return &httpArtifact{req: req, checksum: checksum, doFunc: c.Do}
-}
-
// NewHTTPGet returns a new [File] backed by the supplied client. A GET request
// is set up for url. If c is nil, [http.DefaultClient] is used instead.
func NewHTTPGet(
@@ -46,24 +41,22 @@ func NewHTTPGet(
c *http.Client,
url string,
checksum Checksum,
-) (File, error) {
- req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
- if err != nil {
- return nil, err
+) File {
+ if ctx == nil {
+ ctx = context.Background()
}
- return NewHTTP(c, req, checksum), nil
+ if c == nil {
+ c = http.DefaultClient
+ }
+ return &httpArtifact{ctx: ctx, url: url, checksum: checksum, doFunc: c.Do}
}
// Kind returns the hardcoded [Kind] constant.
-func (a *httpArtifact) Kind() Kind { return KindHTTP }
-
-// ID returns the caller-supplied hash of the response body.
-func (a *httpArtifact) ID() ID { return a.checksum }
+func (a *httpArtifact) Kind() Kind { return KindHTTPGet }
-// Params is unreachable.
-func (a *httpArtifact) Params() []byte {
- panic("not implemented")
-}
+// Params returns the backing url string. Context is not represented as it does
+// not affect [Cache.Cure] outcome.
+func (a *httpArtifact) Params() []byte { return []byte(a.url) }
// Dependencies returns a nil slice.
func (a *httpArtifact) Dependencies() []Artifact { return nil }
@@ -82,8 +75,14 @@ func (e ResponseStatusError) Error() string {
// do sends the caller-supplied request on the caller-supplied [http.Client]
// and reads its response body to EOF and returns the resulting bytes.
func (a *httpArtifact) do() (data []byte, err error) {
+ var req *http.Request
+ req, err = http.NewRequestWithContext(a.ctx, http.MethodGet, a.url, nil)
+ if err != nil {
+ return
+ }
+
var resp *http.Response
- if resp, err = a.doFunc(a.req); err != nil {
+ if resp, err = a.doFunc(req); err != nil {
return
}