Skip to content

Commit

Permalink
Allow setting the API protocol in SetBaseURL (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
LBGarber authored Nov 16, 2021
1 parent b637283 commit 4e4e0cd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 11 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ var envDebug = false
type Client struct {
resty *resty.Client
userAgent string
baseURL string
apiVersion string
resources map[string]*Resource
debug bool
retryConditionals []RetryConditional

millisecondsPerPoll time.Duration

baseURL string
apiVersion string
apiProto string

Account *Resource
AccountSettings *Resource
DomainRecords *Resource
Expand Down Expand Up @@ -148,6 +150,7 @@ func (c *Client) SetBaseURL(baseURL string) *Client {
baseURLPath, _ := url.Parse(baseURL)

c.baseURL = path.Join(baseURLPath.Host, baseURLPath.Path)
c.apiProto = baseURLPath.Scheme

c.updateHostURL()

Expand All @@ -164,6 +167,7 @@ func (c *Client) SetAPIVersion(apiVersion string) *Client {
}

func (c *Client) updateHostURL() {
apiProto := APIProto
baseURL := APIHost
apiVersion := APIVersion

Expand All @@ -175,7 +179,11 @@ func (c *Client) updateHostURL() {
apiVersion = c.apiVersion
}

c.resty.SetHostURL(fmt.Sprintf("%s://%s/%s", APIProto, baseURL, apiVersion))
if c.apiProto != "" {
apiProto = c.apiProto
}

c.resty.SetHostURL(fmt.Sprintf("%s://%s/%s", apiProto, baseURL, apiVersion))
}

// SetRootCertificate adds a root certificate to the underlying TLS client config
Expand Down
12 changes: 12 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func TestClient_SetAPIVersion(t *testing.T) {
updatedAPIVersion := "v4beta_changed"
updatedExpectedHost := fmt.Sprintf("https://%s/%s", updatedBaseURL, updatedAPIVersion)

protocolBaseURL := "http://api.more.cool.com"
protocolAPIVersion := "v4_http"
protocolExpectedHost := fmt.Sprintf("%s/%s", protocolBaseURL, protocolAPIVersion)

client := NewClient(nil)

if client.resty.HostURL != defaultURL {
Expand Down Expand Up @@ -45,4 +49,12 @@ func TestClient_SetAPIVersion(t *testing.T) {
if client.resty.HostURL != expectedHost {
t.Fatal(cmp.Diff(client.resty.HostURL, expectedHost))
}

// Custom protocol
client.SetBaseURL(protocolBaseURL)
client.SetAPIVersion(protocolAPIVersion)

if client.resty.HostURL != protocolExpectedHost {
t.Fatal(cmp.Diff(client.resty.HostURL, expectedHost))
}
}

0 comments on commit 4e4e0cd

Please sign in to comment.