Skip to content

Commit

Permalink
fix: pass http.Client to all managers
Browse files Browse the repository at this point in the history
  • Loading branch information
ahippler committed Sep 30, 2024
1 parent d6a1c83 commit 637ea14
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 11 deletions.
1 change: 1 addition & 0 deletions access/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func New(config config.Config) (*AccessServicesManager, error) {
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetRetries(config.GetHttpRetries()).
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()).
SetHttpClient(config.GetHttpClient()).
Build()

return manager, err
Expand Down
9 changes: 4 additions & 5 deletions artifactory/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,17 +604,16 @@ func (sm *ArtifactoryServicesManagerImp) ImportReleaseBundle(filePath string) er
return releaseService.ImportReleaseBundle(filePath)
}

func buildJFrogHttpClient(config config.Config, authDetails auth.ServiceDetails) (*jfroghttpclient.JfrogHttpClient, error) {
func buildJFrogHttpClient(config config.Config, details auth.ServiceDetails) (*jfroghttpclient.JfrogHttpClient, error) {
return jfroghttpclient.JfrogClientBuilder().
SetCertificatesPath(config.GetCertificatesPath()).
SetInsecureTls(config.IsInsecureTls()).
SetClientCertPath(details.GetClientCertPath()).
SetClientCertKeyPath(details.GetClientCertKeyPath()).
AppendPreRequestInterceptor(details.RunPreRequestFunctions).
SetContext(config.GetContext()).
SetDialTimeout(config.GetDialTimeout()).
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetClientCertPath(authDetails.GetClientCertPath()).
SetClientCertKeyPath(authDetails.GetClientCertKeyPath()).
AppendPreRequestInterceptor(authDetails.RunPreRequestFunctions).
SetContext(config.GetContext()).
SetRetries(config.GetHttpRetries()).
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()).
SetHttpClient(config.GetHttpClient()).
Expand Down
1 change: 1 addition & 0 deletions distribution/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func New(config config.Config) (*DistributionServicesManager, error) {
SetContext(config.GetContext()).
SetRetries(config.GetHttpRetries()).
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()).
SetHttpClient(config.GetHttpClient()).
Build()
return manager, err
}
Expand Down
1 change: 1 addition & 0 deletions evidence/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func New(config config.Config) (*EvidenceServicesManager, error) {
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetRetries(config.GetHttpRetries()).
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()).
SetHttpClient(config.GetHttpClient()).
Build()

return manager, err
Expand Down
1 change: 1 addition & 0 deletions lifecycle/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func New(config config.Config) (*LifecycleServicesManager, error) {
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetRetries(config.GetHttpRetries()).
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()).
SetHttpClient(config.GetHttpClient()).
Build()

return manager, err
Expand Down
2 changes: 2 additions & 0 deletions metadata/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type Manager interface {
GraphqlQuery(query []byte) ([]byte, error)
Client() *jfroghttpclient.JfrogHttpClient
}

type metadataManager struct {
Expand All @@ -30,6 +31,7 @@ func NewManager(config config.Config) (Manager, error) {
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetRetries(config.GetHttpRetries()).
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()).
SetHttpClient(config.GetHttpClient()).
Build()

return manager, err
Expand Down
1 change: 1 addition & 0 deletions pipelines/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func New(config config.Config) (*PipelinesServicesManager, error) {
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetRetries(config.GetHttpRetries()).
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()).
SetHttpClient(config.GetHttpClient()).
Build()
return manager, err
}
Expand Down
51 changes: 51 additions & 0 deletions tests/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package tests

import (
"net/http"
"testing"

"github.com/jfrog/jfrog-client-go/access"
"github.com/jfrog/jfrog-client-go/config"
"github.com/jfrog/jfrog-client-go/distribution"
"github.com/jfrog/jfrog-client-go/evidence"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/jfrog/jfrog-client-go/lifecycle"
"github.com/jfrog/jfrog-client-go/metadata"
"github.com/jfrog/jfrog-client-go/pipelines"
"github.com/jfrog/jfrog-client-go/xray"
"github.com/jfrog/jfrog-client-go/xsc"
"github.com/stretchr/testify/assert"
)

func TestUsesCustomHttpClient(t *testing.T) {
t.Run("artifactory", func(t *testing.T) { usesCustomHttpClient(t, access.New) })
t.Run("access", func(t *testing.T) { usesCustomHttpClient(t, access.New) })
t.Run("distribution", func(t *testing.T) { usesCustomHttpClient(t, distribution.New) })
t.Run("evidence", func(t *testing.T) { usesCustomHttpClient(t, evidence.New) })
t.Run("lifecycle", func(t *testing.T) { usesCustomHttpClient(t, lifecycle.New) })
t.Run("metadata", func(t *testing.T) { usesCustomHttpClient(t, metadata.NewManager) })
t.Run("pipelines", func(t *testing.T) { usesCustomHttpClient(t, pipelines.New) })
t.Run("xray", func(t *testing.T) { usesCustomHttpClient(t, xray.New) })
t.Run("xsc", func(t *testing.T) { usesCustomHttpClient(t, xsc.New) })
}

type Manager interface {
Client() *jfroghttpclient.JfrogHttpClient
}

func usesCustomHttpClient[K Manager](t *testing.T, createManager func(config config.Config) (K, error)) {
client := http.DefaultClient
config, err := config.NewConfigBuilder().
SetServiceDetails(GetRtDetails()).
SetHttpClient(client).
Build()
if err != nil {
t.Error(err)
}
m, err := createManager(config)
if err != nil {
t.Error(err)
}
actualClient := m.Client().GetHttpClient().GetClient()
assert.Equal(t, client, actualClient, "Expected the client to be the same")
}
7 changes: 4 additions & 3 deletions xray/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ func New(config config.Config) (*XrayServicesManager, error) {
manager.client, err = jfroghttpclient.JfrogClientBuilder().
SetCertificatesPath(config.GetCertificatesPath()).
SetInsecureTls(config.IsInsecureTls()).
SetContext(config.GetContext()).
SetDialTimeout(config.GetDialTimeout()).
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetClientCertPath(details.GetClientCertPath()).
SetClientCertKeyPath(details.GetClientCertKeyPath()).
AppendPreRequestInterceptor(details.RunPreRequestFunctions).
SetContext(config.GetContext()).
SetDialTimeout(config.GetDialTimeout()).
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetRetries(config.GetHttpRetries()).
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()).
SetHttpClient(config.GetHttpClient()).
Build()
return manager, err
}
Expand Down
7 changes: 4 additions & 3 deletions xsc/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ func New(config config.Config) (*XscServicesManager, error) {
manager.client, err = jfroghttpclient.JfrogClientBuilder().
SetCertificatesPath(config.GetCertificatesPath()).
SetInsecureTls(config.IsInsecureTls()).
SetContext(config.GetContext()).
SetDialTimeout(config.GetDialTimeout()).
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetClientCertPath(details.GetClientCertPath()).
SetClientCertKeyPath(details.GetClientCertKeyPath()).
AppendPreRequestInterceptor(details.RunPreRequestFunctions).
SetContext(config.GetContext()).
SetDialTimeout(config.GetDialTimeout()).
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetRetries(config.GetHttpRetries()).
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()).
SetHttpClient(config.GetHttpClient()).
Build()
return manager, err
}
Expand Down

0 comments on commit 637ea14

Please sign in to comment.