From 5c1492fdc3996710357c578b95699a2136957c85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7=E5=B3=B0?= <39459242+hhfmln@users.noreply.github.com> Date: Tue, 25 Jun 2024 23:32:13 +0800 Subject: [PATCH] add httpClient option to support proxy requests (#30) Co-authored-by: huanghaifeng <1345733996@qq.com> --- pkg/anthropic/client.go | 5 ++++- pkg/anthropic/options.go | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/anthropic/client.go b/pkg/anthropic/client.go index d637eba..6cca571 100644 --- a/pkg/anthropic/client.go +++ b/pkg/anthropic/client.go @@ -12,7 +12,7 @@ type Client struct { } // NewClient initializes a new Anthropic API client with the required headers. -func NewClient(apiKey string) (*Client, error) { +func NewClient(apiKey string, options ...GenericOption[Client]) (*Client, error) { if apiKey == "" { return nil, ErrAnthropicApiKeyRequired } @@ -22,6 +22,9 @@ func NewClient(apiKey string) (*Client, error) { apiKey: apiKey, baseURL: "https://api.anthropic.com", } + for _, opt := range options { + opt(client) + } return client, nil } diff --git a/pkg/anthropic/options.go b/pkg/anthropic/options.go index 5caa1c0..99b12fc 100644 --- a/pkg/anthropic/options.go +++ b/pkg/anthropic/options.go @@ -1,5 +1,7 @@ package anthropic +import "net/http" + type CompletionOption func(*CompletionRequest) type MessageOption func(*MessageRequest) @@ -124,3 +126,12 @@ func WithTopP[T any](topP float64) GenericOption[T] { } } } + +// WithHTTPClient sets a custom HTTP client for the Client. +func WithHTTPClient[T any](httpClient *http.Client) GenericOption[T] { + return func(r *T) { + if v, ok := any(r).(*Client); ok { + v.httpClient = httpClient + } + } +}