From e1789d079bf58b3509ef08219344ff4bf73eee46 Mon Sep 17 00:00:00 2001 From: Daniil Rutskiy Date: Mon, 17 Jun 2019 14:13:19 +0300 Subject: [PATCH 1/2] Custom HTTP client support * Add ability to specify custom HTTP client during common/reseller clients initializations --- .golangci.yml | 1 + gcore/gcore.go | 58 +++++++++++++++++++++++++++++++++ gcore/gcore_test.go | 78 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 6a112c0..c14f2da 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,3 +7,4 @@ linters: - gosec - lll - maligned + - bodyclose diff --git a/gcore/gcore.go b/gcore/gcore.go index 5faacce..7fe0fe8 100644 --- a/gcore/gcore.go +++ b/gcore/gcore.go @@ -173,6 +173,37 @@ func NewCommonClient(logger ...log.GenericLogger) *CommonClient { return commonClient } +// NewCommonClientWithCustomHTTP creates basic G-Core client with custom HTTP client. +func NewCommonClientWithCustomHTTP(customClient *http.Client, logger ...log.GenericLogger) *CommonClient { + baseURL, _ := url.Parse(defaultBaseURL) + + if customClient == nil { + customClient = NewHTTPClient() + } + + c := &Client{ + client: customClient, + BaseURL: baseURL, + UserAgent: defaultUserAgent, + log: log.SelectLogger(logger...), + } + c.common.client = c + + commonServices := CommonServices{} + commonServices.Account = (*AccountService)(&c.common) + commonServices.Resources = (*ResourcesService)(&c.common) + commonServices.OriginGroups = (*OriginGroupsService)(&c.common) + commonServices.Rules = (*RulesService)(&c.common) + commonServices.Certificates = (*CertService)(&c.common) + + commonClient := &CommonClient{ + Client: c, + CommonServices: commonServices, + } + + return commonClient +} + // NewResellerClient creates reseller G-Core client. func NewResellerClient(logger ...log.GenericLogger) *ResellerClient { baseURL, _ := url.Parse(defaultBaseURL) @@ -196,6 +227,33 @@ func NewResellerClient(logger ...log.GenericLogger) *ResellerClient { return resellClient } +// NewResellerClientWithCustomHTTP creates reseller G-Core client with custom HTTP client. +func NewResellerClientWithCustomHTTP(customClient *http.Client, logger ...log.GenericLogger) *ResellerClient { + baseURL, _ := url.Parse(defaultBaseURL) + + if customClient == nil { + customClient = NewHTTPClient() + } + + c := &Client{ + client: customClient, + BaseURL: baseURL, + UserAgent: defaultUserAgent, + log: log.SelectLogger(logger...), + } + c.common.client = c + + resellerServices := ResellerServices{} + resellerServices.Clients = (*ClientsService)(&c.common) + resellerServices.GeoRestrictions = (*GeoRestrictionsService)(&c.common) + resellClient := &ResellerClient{ + Client: c, + ResellerServices: resellerServices, + } + + return resellClient +} + // NewRequest method returns new request by given options. func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body interface{}) (*http.Request, error) { diff --git a/gcore/gcore_test.go b/gcore/gcore_test.go index 76acf7f..b19f1dd 100644 --- a/gcore/gcore_test.go +++ b/gcore/gcore_test.go @@ -2,7 +2,9 @@ package gcore import ( "context" + "net/http" "testing" + "time" th "github.com/dstdfx/go-gcore/gcore/internal/testhelper" ) @@ -29,6 +31,44 @@ func TestNewCommonClient(t *testing.T) { } } +func TestNewCommonClientWithCustomHTTP(t *testing.T) { + testEnv := th.SetupTestEnv() + defer testEnv.TearDownTestEnv() + + customClient := &http.Client{ + Timeout: 5 * time.Second, + } + + common := NewCommonClientWithCustomHTTP(customClient) + common.BaseURL = testEnv.GetServerURL() + + err := common.Authenticate(context.Background(), TestFakeAuthOptions) + if err != nil { + t.Fatal(err) + } + + if common.Token.Value != th.TestFakeToken { + t.Errorf("Expected: %s, got %s", th.TestFakeToken, common.Token.Value) + } +} + +func TestNewCommonClientWithNilCustomHTTP(t *testing.T) { + testEnv := th.SetupTestEnv() + defer testEnv.TearDownTestEnv() + + common := NewCommonClientWithCustomHTTP(nil) + common.BaseURL = testEnv.GetServerURL() + + err := common.Authenticate(context.Background(), TestFakeAuthOptions) + if err != nil { + t.Fatal(err) + } + + if common.Token.Value != th.TestFakeToken { + t.Errorf("Expected: %s, got %s", th.TestFakeToken, common.Token.Value) + } +} + func TestNewResellerClient(t *testing.T) { testEnv := th.SetupTestEnv() defer testEnv.TearDownTestEnv() @@ -45,3 +85,41 @@ func TestNewResellerClient(t *testing.T) { t.Errorf("Expected: %s, got %s", th.TestFakeToken, reseller.Token.Value) } } + +func TestNewResellerClientWithCustomHTTP(t *testing.T) { + testEnv := th.SetupTestEnv() + defer testEnv.TearDownTestEnv() + + customClient := &http.Client{ + Timeout: 5 * time.Second, + } + + reseller := NewResellerClientWithCustomHTTP(customClient) + reseller.BaseURL = testEnv.GetServerURL() + + err := reseller.Authenticate(context.Background(), TestFakeAuthOptions) + if err != nil { + t.Fatal(err) + } + + if reseller.Token.Value != th.TestFakeToken { + t.Errorf("Expected: %s, got %s", th.TestFakeToken, reseller.Token.Value) + } +} + +func TestNewResellerClientWithNilCustomHTTP(t *testing.T) { + testEnv := th.SetupTestEnv() + defer testEnv.TearDownTestEnv() + + reseller := NewResellerClientWithCustomHTTP(nil) + reseller.BaseURL = testEnv.GetServerURL() + + err := reseller.Authenticate(context.Background(), TestFakeAuthOptions) + if err != nil { + t.Fatal(err) + } + + if reseller.Token.Value != th.TestFakeToken { + t.Errorf("Expected: %s, got %s", th.TestFakeToken, reseller.Token.Value) + } +} From 1fbd266d1571be340f4177eb5add9e8d9e4aa6e9 Mon Sep 17 00:00:00 2001 From: Daniil Rutskiy Date: Mon, 17 Jun 2019 14:31:21 +0300 Subject: [PATCH 2/2] Add newer version of golangci-lint --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7a9cac4..6288f89 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ sudo: false language: go install: - go get github.com/mattn/goveralls - - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v1.12 + - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v1.17.1 go: - "1.11" before_script: