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.
Add Artifactory API to get package artifact lead file (jfrog#1044)
- Loading branch information
1 parent
f0d689e
commit b60e50e
Showing
8 changed files
with
149 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
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,53 @@ | ||
package services | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/jfrog/jfrog-client-go/auth" | ||
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
clientUtils "github.com/jfrog/jfrog-client-go/utils" | ||
"github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
"net/http" | ||
) | ||
|
||
const apiLeadFile = "api/packagesSearch/leadFile" | ||
|
||
type PackageService struct { | ||
Client *jfroghttpclient.JfrogHttpClient | ||
ArtDetails auth.ServiceDetails | ||
} | ||
|
||
func NewPackageService(client *jfroghttpclient.JfrogHttpClient) *PackageService { | ||
return &PackageService{Client: client} | ||
} | ||
|
||
func (ps *PackageService) GetJfrogHttpClient() *jfroghttpclient.JfrogHttpClient { | ||
return ps.Client | ||
} | ||
|
||
func (ps *PackageService) GetPackageLeadFile(leadFileRequest LeadFileParams) ([]byte, error) { | ||
requestUrl, err := clientUtils.BuildUrl(ps.ArtDetails.GetUrl(), apiLeadFile, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
requestContent, err := json.Marshal(leadFileRequest) | ||
if err != nil { | ||
return nil, errorutils.CheckError(err) | ||
} | ||
|
||
httpClientsDetails := ps.ArtDetails.CreateHttpClientDetails() | ||
httpClientsDetails.SetContentTypeApplicationJson() | ||
|
||
resp, body, err := ps.Client.SendPost(requestUrl, requestContent, &httpClientsDetails) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return body, errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK) | ||
} | ||
|
||
type LeadFileParams struct { | ||
PackageVersion string `json:"package_version"` | ||
PackageName string `json:"package_name"` | ||
PackageRepoName string `json:"package_repo_name"` | ||
PackageType string `json:"package_type"` | ||
} |
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,65 @@ | ||
package tests | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-client-go/artifactory/auth" | ||
"github.com/jfrog/jfrog-client-go/artifactory/services" | ||
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
var leadFileRequest = services.LeadFileParams{ | ||
PackageVersion: "1.0.0", | ||
PackageName: "test-package", | ||
PackageRepoName: "test-repo", | ||
PackageType: "test-type", | ||
} | ||
|
||
func TestPackage(t *testing.T) { | ||
initArtifactoryTest(t) | ||
t.Run("TestGetLeadFileSuccessfully", TestGetLeadFileSuccessfully) | ||
} | ||
|
||
func TestGetLeadFileSuccessfully(t *testing.T) { | ||
handlerFunc := createDefaultHandlerFunc(t) | ||
mockServer, packageService := createMockPackageServer(t, handlerFunc) | ||
|
||
expectedLeadFile := "path/to/lead/file" | ||
defer mockServer.Close() | ||
|
||
leadFilePath, err := packageService.GetPackageLeadFile(leadFileRequest) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedLeadFile, string(leadFilePath)) | ||
} | ||
|
||
func createDefaultHandlerFunc(t *testing.T) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
if r.RequestURI == "/api/packagesSearch/leadFile" { | ||
assert.Equal(t, "application/json", r.Header.Get("Content-Type")) | ||
w.WriteHeader(http.StatusOK) | ||
writeMockLeadFileResponse(t, w, []byte("path/to/lead/file")) | ||
} else { | ||
t.Errorf("Unexpected URL: got %s, want %s", r.RequestURI, "/api/packagesSearch/leadFile") | ||
http.Error(w, "Not Found", http.StatusNotFound) | ||
} | ||
} | ||
} | ||
|
||
func writeMockLeadFileResponse(t *testing.T, w http.ResponseWriter, payload []byte) { | ||
_, err := w.Write(payload) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func createMockPackageServer(t *testing.T, testHandler http.HandlerFunc) (*httptest.Server, *services.PackageService) { | ||
testServer := httptest.NewServer(testHandler) | ||
|
||
serviceDetails := auth.NewArtifactoryDetails() | ||
serviceDetails.SetUrl(testServer.URL + "/") | ||
|
||
packageService, err := jfroghttpclient.JfrogClientBuilder().Build() | ||
|
||
assert.NoError(t, err) | ||
return testServer, &services.PackageService{Client: packageService, ArtDetails: serviceDetails} | ||
} |
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