Skip to content

Commit

Permalink
Merge pull request #21 from dstdfx/feature/custom-http-support
Browse files Browse the repository at this point in the history
Custom HTTP client support
  • Loading branch information
dstdfx authored Jun 18, 2019
2 parents 63ebe97 + 1fbd266 commit f04898f
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ linters:
- gosec
- lll
- maligned
- bodyclose
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
58 changes: 58 additions & 0 deletions gcore/gcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
78 changes: 78 additions & 0 deletions gcore/gcore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package gcore

import (
"context"
"net/http"
"testing"
"time"

th "github.com/dstdfx/go-gcore/gcore/internal/testhelper"
)
Expand All @@ -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()
Expand All @@ -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)
}
}

0 comments on commit f04898f

Please sign in to comment.