Skip to content

Commit 3d24b06

Browse files
committed
feat: allow custom http client
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
1 parent 3a244d9 commit 3d24b06

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

pkg/api/api.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/Escape-Technologies/cli/pkg/log"
1111
)
1212

13-
func NewAPIClient() (*ClientWithResponses, error) {
13+
func NewAPIClient(opts ...ClientOption) (*ClientWithResponses, error) {
1414
log.Trace("Initializing client")
1515
server := os.Getenv("ESCAPE_API_URL")
1616
if server == "" {
@@ -20,20 +20,18 @@ func NewAPIClient() (*ClientWithResponses, error) {
2020
if key == "" {
2121
return nil, fmt.Errorf("ESCAPE_API_KEY is not set")
2222
}
23-
return NewClientWithResponses(
24-
server,
25-
WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
26-
log.Debug("Sending request %s %s", req.Method, req.URL)
27-
if req.Body != nil {
28-
clone := req.Clone(context.Background())
29-
body, err := io.ReadAll(clone.Body)
30-
if err != nil {
31-
return err
32-
}
33-
log.Trace("Body %s", string(body))
23+
opts = append(opts, WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
24+
log.Debug("Sending request %s %s", req.Method, req.URL)
25+
if req.Body != nil {
26+
clone := req.Clone(context.Background())
27+
body, err := io.ReadAll(clone.Body)
28+
if err != nil {
29+
return err
3430
}
35-
req.Header.Set("Authorization", fmt.Sprintf("Key %s", key))
36-
return nil
37-
}),
38-
)
31+
log.Trace("Body %s", string(body))
32+
}
33+
req.Header.Set("Authorization", fmt.Sprintf("Key %s", key))
34+
return nil
35+
}))
36+
return NewClientWithResponses(server, opts...)
3937
}

0 commit comments

Comments
 (0)