From 869f43391631ffef8174fada88f8260382c2b715 Mon Sep 17 00:00:00 2001 From: Daniil Rutskiy Date: Tue, 12 May 2020 14:22:51 +0300 Subject: [PATCH] Add ability to init client with default endpoint --- pkg/v1/client.go | 14 ++++++++++++++ pkg/v1/client_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/pkg/v1/client.go b/pkg/v1/client.go index 9910a86..b60065c 100644 --- a/pkg/v1/client.go +++ b/pkg/v1/client.go @@ -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 @@ -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. diff --git a/pkg/v1/client_test.go b/pkg/v1/client_test.go index 82adfb5..c1668ff 100644 --- a/pkg/v1/client_test.go +++ b/pkg/v1/client_test.go @@ -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()