Skip to content

Commit

Permalink
add httpClient option to support proxy requests (#30)
Browse files Browse the repository at this point in the history
Co-authored-by: huanghaifeng <[email protected]>
  • Loading branch information
hhfmln and huanghaifeng authored Jun 25, 2024
1 parent a7eb486 commit 5c1492f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/anthropic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
11 changes: 11 additions & 0 deletions pkg/anthropic/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package anthropic

import "net/http"

type CompletionOption func(*CompletionRequest)
type MessageOption func(*MessageRequest)

Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 5c1492f

Please sign in to comment.