Skip to content

Commit

Permalink
Merge pull request #20 from dstdfx/default-endpoint
Browse files Browse the repository at this point in the history
Add ability to init client with default endpoint
  • Loading branch information
dstdfx authored May 12, 2020
2 parents e4516cc + 869f433 commit 8f73436
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const (
// userAgent contains a basic user agent that will be used in queries.
userAgent = appName + "/" + appVersion

// defaultEndpoint represents default endpoint for Selectel Domains API v1.
defaultEndpoint = "https://api.selectel.ru/domains/v1"

// defaultHTTPTimeout represents the default timeout (in seconds) for HTTP requests.
defaultHTTPTimeout = 120

Expand Down Expand Up @@ -72,6 +75,17 @@ func NewDomainsClientV1(token, endpoint string) *ServiceClient {
}
}

// NewDomainsClientV1WithDefaultEndpoint initializes a new client for the Domains API V1
// with default endpoint.
func NewDomainsClientV1WithDefaultEndpoint(token string) *ServiceClient {
return &ServiceClient{
HTTPClient: newHTTPClient(),
Token: token,
Endpoint: defaultEndpoint,
UserAgent: userAgent,
}
}

// NewDomainsClientV1WithCustomHTTP initializes a new client for the Domains API V1
// using custom HTTP client.
// If customHTTPClient is nil - default HTTP client will be used.
Expand Down
24 changes: 24 additions & 0 deletions pkg/v1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ func TestNewDomainsClientV1WithCustomHTTP(t *testing.T) {
}
}

func TestNewDomainsClientV1WithDefaultEndpoint(t *testing.T) {
token := testutils.Token
expected := &ServiceClient{
Token: token,
Endpoint: defaultEndpoint,
UserAgent: userAgent,
}

actual := NewDomainsClientV1WithDefaultEndpoint(token)

if expected.Token != actual.Token {
t.Errorf("expected Endpoint %s, but got %s", expected.Endpoint, actual.Endpoint)
}
if expected.Endpoint != actual.Endpoint {
t.Errorf("expected Token %s, but got %s", expected.Token, actual.Token)
}
if expected.UserAgent != actual.UserAgent {
t.Errorf("expected UserAgent %s, but got %s", expected.UserAgent, actual.UserAgent)
}
if actual.HTTPClient == nil {
t.Errorf("expected initialized HTTPClient but it's nil")
}
}

func TestDoGetRequest(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
Expand Down

0 comments on commit 8f73436

Please sign in to comment.