forked from jfrog/jfrog-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Metadata Service Manager (jfrog#997)
- Loading branch information
Showing
6 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-client-go/auth" | ||
) | ||
|
||
type metadataDetails struct { | ||
auth.CommonConfigFields | ||
} | ||
|
||
func NewMetadataDetails() auth.ServiceDetails { | ||
return &metadataDetails{} | ||
} | ||
|
||
func (rt *metadataDetails) GetVersion() (string, error) { | ||
panic("Failed: Method is not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package metadata | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-client-go/config" | ||
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
"github.com/jfrog/jfrog-client-go/metadata/services" | ||
) | ||
|
||
type Manager interface { | ||
GraphqlQuery(query []byte) ([]byte, error) | ||
} | ||
|
||
type metadataManager struct { | ||
client *jfroghttpclient.JfrogHttpClient | ||
config config.Config | ||
} | ||
|
||
func NewManager(config config.Config) (Manager, error) { | ||
details := config.GetServiceDetails() | ||
var err error | ||
manager := &metadataManager{config: config} | ||
manager.client, err = 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()). | ||
SetRetries(config.GetHttpRetries()). | ||
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()). | ||
Build() | ||
|
||
return manager, err | ||
} | ||
|
||
func (mm *metadataManager) Client() *jfroghttpclient.JfrogHttpClient { | ||
return mm.client | ||
} | ||
|
||
func (mm *metadataManager) GraphqlQuery(query []byte) ([]byte, error) { | ||
metadataService := services.NewMetadataService(mm.config.GetServiceDetails(), mm.client) | ||
return metadataService.Query(query) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package services | ||
|
||
import ( | ||
"fmt" | ||
rtUtils "github.com/jfrog/jfrog-client-go/artifactory/services/utils" | ||
"github.com/jfrog/jfrog-client-go/auth" | ||
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
"github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
const queryUrl = "api/v1/query" | ||
|
||
type Service interface { | ||
Query(query []byte) ([]byte, error) | ||
} | ||
|
||
type metadataService struct { | ||
client *jfroghttpclient.JfrogHttpClient | ||
serviceDetails *auth.ServiceDetails | ||
} | ||
|
||
func NewMetadataService(serviceDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) Service { | ||
return &metadataService{serviceDetails: &serviceDetails, client: client} | ||
} | ||
|
||
func (m *metadataService) GetMetadataDetails() auth.ServiceDetails { | ||
return *m.serviceDetails | ||
} | ||
|
||
func (m *metadataService) Query(query []byte) ([]byte, error) { | ||
graphqlUrl, err := url.Parse(m.GetMetadataDetails().GetUrl() + queryUrl) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse URL: %w", err) | ||
} | ||
httpClientDetails := m.GetMetadataDetails().CreateHttpClientDetails() | ||
rtUtils.SetContentType("application/json", &httpClientDetails.Headers) | ||
|
||
resp, body, err := m.client.SendPost(graphqlUrl.String(), query, &httpClientDetails) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
return body, errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package services | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/jfrog/jfrog-client-go/artifactory/auth" | ||
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
const queryData = `{"query":"someGraphqlQuery"}` | ||
|
||
func TestMetadataService_Query(t *testing.T) { | ||
handlerFunc, requestNum := createMetadataHandlerFunc(t) | ||
|
||
mockServer, metadataService := createMockMetadataServer(t, handlerFunc) | ||
defer mockServer.Close() | ||
|
||
_, err := metadataService.Query([]byte(queryData)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 1, *requestNum) | ||
} | ||
|
||
func createMockMetadataServer(t *testing.T, testHandler http.HandlerFunc) (*httptest.Server, *metadataService) { | ||
testServer := httptest.NewServer(testHandler) | ||
|
||
serviceDetails := auth.NewArtifactoryDetails() | ||
serviceDetails.SetUrl(testServer.URL + "/") | ||
|
||
client, err := jfroghttpclient.JfrogClientBuilder().Build() | ||
assert.NoError(t, err) | ||
return testServer, &metadataService{serviceDetails: &serviceDetails, client: client} | ||
} | ||
|
||
func createMetadataHandlerFunc(t *testing.T) (http.HandlerFunc, *int) { | ||
requestNum := 0 | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
if r.RequestURI == "/api/v1/query" { | ||
assert.Equal(t, "application/json", r.Header.Get("Content-Type")) | ||
|
||
w.WriteHeader(http.StatusOK) | ||
requestNum++ | ||
writeMockMetadataResponse(t, w, []byte(queryData)) | ||
} | ||
}, &requestNum | ||
} | ||
|
||
func writeMockMetadataResponse(t *testing.T, w http.ResponseWriter, payload []byte) { | ||
content, err := json.Marshal(payload) | ||
assert.NoError(t, err) | ||
_, err = w.Write(content) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters