From b5a0b3f0fd87835a23c5cce21568e7ea60683c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=97=8D+85CD?= <50108258+kwaa@users.noreply.github.com> Date: Tue, 17 Dec 2024 17:39:15 +0800 Subject: [PATCH] refactor(openai): with context --- pkg/backend/openai.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/backend/openai.go b/pkg/backend/openai.go index e7212f1..f74d0da 100644 --- a/pkg/backend/openai.go +++ b/pkg/backend/openai.go @@ -22,16 +22,26 @@ func openai(c echo.Context, options FullOptions) mo.Result[any] { payload := fo.May(json.Marshal(values)) - res, err := http.Post( + req, err := http.NewRequestWithContext( + c.Request().Context(), + http.MethodPost, "https://openai.com/v1/audio/speech", - "application/json", bytes.NewBuffer(payload), ) if err != nil { return mo.Err[any](apierrors.NewErrBadRequest().WithCaller()) } - // defer res.Body.Close() + // TODO: Bearer Auth + req.Header.Set("Content-Type", "application/json") + + res, err := http.DefaultClient.Do(req) + + if err != nil { + return mo.Err[any](apierrors.NewErrBadRequest().WithCaller()) + } + + defer res.Body.Close() // body, _ := io.ReadAll(res.Body)