Skip to content

Commit 5ded51a

Browse files
committed
Merge branch 'set-base-url' into release/v1.40.5
2 parents f03a57c + 163820a commit 5ded51a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

client.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ func NewOrgClient(authToken, org string) *Client {
7171
return NewClientWithConfig(config)
7272
}
7373

74+
// SetBaseURL updates the base URL for the client.
75+
// This allows changing the endpoint after client instantiation.
76+
func (c *Client) SetBaseURL(baseURL string) {
77+
c.config.BaseURL = baseURL
78+
}
79+
80+
// GetBaseURL returns the current base URL for the client.
81+
func (c *Client) GetBaseURL() string {
82+
return c.config.BaseURL
83+
}
84+
7485
type requestOptions struct {
7586
body any
7687
header http.Header

client_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,35 @@ func TestClient(t *testing.T) {
3939
}
4040
}
4141

42+
func TestClient_SetBaseURL(t *testing.T) {
43+
const mockToken = "mock token"
44+
client := NewClient(mockToken)
45+
46+
// Test default base URL
47+
defaultBaseURL := client.GetBaseURL()
48+
if defaultBaseURL != openaiAPIURLv1 {
49+
t.Errorf("Expected default BaseURL to be %q, got %q", openaiAPIURLv1, defaultBaseURL)
50+
}
51+
52+
// Test setting a new base URL
53+
const newBaseURL = "https://custom-api.example.com/v1"
54+
client.SetBaseURL(newBaseURL)
55+
56+
// Verify the base URL was updated
57+
updatedBaseURL := client.GetBaseURL()
58+
if updatedBaseURL != newBaseURL {
59+
t.Errorf("Expected BaseURL to be %q after SetBaseURL, got %q", newBaseURL, updatedBaseURL)
60+
}
61+
62+
// Test that the updated base URL is used in fullURL method
63+
suffix := "/chat/completions"
64+
fullURL := client.fullURL(suffix)
65+
expectedFullURL := newBaseURL + suffix
66+
if fullURL != expectedFullURL {
67+
t.Errorf("Expected fullURL to be %q, got %q", expectedFullURL, fullURL)
68+
}
69+
}
70+
4271
func TestSetCommonHeadersAnthropic(t *testing.T) {
4372
config := DefaultAnthropicConfig("mock-token", "")
4473
client := NewClientWithConfig(config)

0 commit comments

Comments
 (0)