Skip to content

Commit 72acc97

Browse files
committed
cache http response body
1 parent 91cd7f9 commit 72acc97

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

kite-service/pkg/eval/ctx.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -427,26 +427,35 @@ func (s SnowflakeEnv) String() string {
427427

428428
type HTTPResponseEnv struct {
429429
resp *http.Response
430+
body []byte
430431

431432
Status string `expr:"status" json:"status"`
432433
StatusCode int `expr:"status_code" json:"status_code"`
433434
BodyFunc func() (string, error) `expr:"body" json:"-"`
434435
}
435436

436437
func NewHTTPResponseEnv(resp *http.Response) *HTTPResponseEnv {
437-
return &HTTPResponseEnv{
438+
res := &HTTPResponseEnv{
438439
resp: resp,
439440

440441
Status: resp.Status,
441442
StatusCode: resp.StatusCode,
442-
BodyFunc: func() (string, error) {
443-
body, err := io.ReadAll(resp.Body)
444-
if err != nil {
445-
return "", err
446-
}
447-
return string(body), nil
448-
},
449443
}
444+
res.BodyFunc = func() (string, error) {
445+
if res.body != nil {
446+
return string(res.body), nil
447+
}
448+
449+
body, err := io.ReadAll(resp.Body)
450+
if err != nil {
451+
return "", err
452+
}
453+
454+
res.body = body
455+
return string(body), nil
456+
}
457+
458+
return res
450459
}
451460

452461
func (h HTTPResponseEnv) String() string {

0 commit comments

Comments
 (0)