Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/support all apis #3

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add branhces api + test cases
subzerobo committed Dec 17, 2024
commit 8f3a9f322ef5616df29cf45cc59e9b21564dbd2a
12 changes: 3 additions & 9 deletions apis/artifacts.go
Original file line number Diff line number Diff line change
@@ -21,14 +21,8 @@ func NewArtifactsAPI(client *client.Client) *ArtifactsAPI {
}
}

var (
ErrArtifactNotFound = errors.New("artifact not found")
ErrMethodNotAllowed = errors.New("method not allowed or disabled on the server")
ErrInvalidInput = errors.New("input must be between 1 and 512 characters")
)

// GetArtifactByGlobalID Gets the content for an artifact version in the registry using its globally unique identifier.
// See https://schema-registry.dev.mollielabs.net/apis/registry/v3#operation/getContentByGlobalId
// See https://www.apicur.io/registry/docs/apicurio-registry/3.0.x/assets-attachments/registry-rest-api.htm#tag/Artifacts/operation/getContentByGlobalId
func (api *ArtifactsAPI) GetArtifactByGlobalID(ctx context.Context, globalID int64, params *models.GetArtifactByGlobalIDParams) (*models.ArtifactContent, error) {
returnArtifactType := false
query := ""
@@ -69,7 +63,7 @@ func (api *ArtifactsAPI) GetArtifactByGlobalID(ctx context.Context, globalID int

// SearchArtifacts - Search for artifacts using the given filter parameters.
// Search for artifacts using the given filter parameters.
// See https://schema-registry.dev.mollielabs.net/apis/registry/v3#operation/searchArtifacts
// See https://www.apicur.io/registry/docs/apicurio-registry/3.0.x/assets-attachments/registry-rest-api.htm#tag/Artifacts/operation/searchArtifacts
func (api *ArtifactsAPI) SearchArtifacts(ctx context.Context, params *models.SearchArtifactsParams) (*[]models.SearchedArtifact, error) {
query := ""
if params != nil {
@@ -295,7 +289,7 @@ func (api *ArtifactsAPI) DeleteArtifact(ctx context.Context, groupID, artifactId
if err != nil {
return err
}

return handleResponse(resp, http.StatusNoContent, nil)
}

3 changes: 3 additions & 0 deletions apis/artifacts_test.go
Original file line number Diff line number Diff line change
@@ -26,6 +26,9 @@ var (
stubArtifactContent = `{"type": "record", "name": "Test", "fields": [{"name": "field1", "type": "string"}]}`
stubArtifactId = "test-artifact"
stubGroupId = "test-group"
stubBranchID = "test-branch"
stubVersionID = "1.0.0"
stubVersionID2 = "2.0.0"
)

func setupHTTPClient() *client.Client {
324 changes: 324 additions & 0 deletions apis/branches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,324 @@
package apis

import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/mollie/go-apicurio-registry/client"
"github.com/mollie/go-apicurio-registry/models"
"github.com/pkg/errors"
"net/http"
)

type BranchAPI struct {
Client *client.Client
}

func NewBranchAPI(client *client.Client) *BranchAPI {
return &BranchAPI{
Client: client,
}
}

// ListBranches Returns a list of all branches in the artifact.
// Each branch is a list of version identifiers, ordered from the latest (tip of the branch) to the oldest.
// See https://www.apicur.io/registry/docs/apicurio-registry/3.0.x/assets-attachments/registry-rest-api.htm#tag/Branches/operation/listBranches
func (api *BranchAPI) ListBranches(ctx context.Context, groupId, artifactId string, params *models.ListBranchesParams) ([]models.BranchInfo, error) {
if err := validateInput(artifactId, regexGroupIDArtifactID, "Artifact ID"); err != nil {
return nil, err
}
if err := validateInput(groupId, regexGroupIDArtifactID, "Group ID"); err != nil {
return nil, err
}

query := ""
if params != nil {
if err := params.Validate(); err != nil {
return nil, errors.Wrap(err, "invalid parameters provided")
}
query = "?" + params.ToQuery().Encode()
}

url := fmt.Sprintf("%s/groups/%s/artifacts/%s/branches%s", api.Client.BaseURL, groupId, artifactId, query)
resp, err := api.executeRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}

var result models.BranchesInfoResponse
if err := handleResponse(resp, http.StatusOK, &result); err != nil {
return nil, err
}

return result.Branches, nil

}

// CreateBranch Creates a new branch for the artifact.
// A new branch consists of metadata and a list of versions.
// See https://www.apicur.io/registry/docs/apicurio-registry/3.0.x/assets-attachments/registry-rest-api.htm#tag/Branches/operation/createBranch
func (api *BranchAPI) CreateBranch(ctx context.Context, groupId, artifactId string, branch *models.CreateBranchRequest) (*models.BranchInfo, error) {
if err := validateInput(artifactId, regexGroupIDArtifactID, "Artifact ID"); err != nil {
return nil, err
}
if err := validateInput(groupId, regexGroupIDArtifactID, "Group ID"); err != nil {
return nil, err
}

if err := branch.Validate(); err != nil {
return nil, errors.Wrap(err, "invalid branch provided")
}

url := fmt.Sprintf("%s/groups/%s/artifacts/%s/branches", api.Client.BaseURL, groupId, artifactId)
resp, err := api.executeRequest(ctx, http.MethodPost, url, branch)
if err != nil {
return nil, err
}

var result models.BranchInfo
if err := handleResponse(resp, http.StatusOK, &result); err != nil {
return nil, err
}

return &result, nil
}

// GetBranchMetaData Get branch metaData
// See https://www.apicur.io/registry/docs/apicurio-registry/3.0.x/assets-attachments/registry-rest-api.htm#tag/Branches/operation/getBranchMetaData
func (api *BranchAPI) GetBranchMetaData(ctx context.Context, groupId, artifactId, branchId string) (*models.BranchInfo, error) {
if err := validateInput(artifactId, regexGroupIDArtifactID, "Artifact ID"); err != nil {
return nil, err
}
if err := validateInput(groupId, regexGroupIDArtifactID, "Group ID"); err != nil {
return nil, err
}
if err := validateInput(branchId, regexBranchID, "Branch ID"); err != nil {
return nil, err
}

url := fmt.Sprintf("%s/groups/%s/artifacts/%s/branches/%s", api.Client.BaseURL, groupId, artifactId, branchId)
resp, err := api.executeRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}

var result models.BranchInfo
if err := handleResponse(resp, http.StatusOK, &result); err != nil {
return nil, err
}

return &result, nil
}

// UpdateBranchMetaData Update branch metaData
// See https://www.apicur.io/registry/docs/apicurio-registry/3.0.x/assets-attachments/registry-rest-api.htm#tag/Branches/operation/updateBranchMetaData
func (api *BranchAPI) UpdateBranchMetaData(ctx context.Context, groupId, artifactId, branchId, description string) (*models.BranchInfo, error) {
if err := validateInput(artifactId, regexGroupIDArtifactID, "Artifact ID"); err != nil {
return nil, err
}
if err := validateInput(groupId, regexGroupIDArtifactID, "Group ID"); err != nil {
return nil, err
}
if err := validateInput(branchId, regexBranchID, "Branch ID"); err != nil {
return nil, err
}

url := fmt.Sprintf("%s/groups/%s/artifacts/%s/branches/%s", api.Client.BaseURL, groupId, artifactId, branchId)

branchMetaData := models.UpdateBranchMetaDataRequest{
Description: description,
}
resp, err := api.executeRequest(ctx, http.MethodPut, url, branchMetaData)
if err != nil {
return nil, err
}

var result models.BranchInfo
if err := handleResponse(resp, http.StatusOK, &result); err != nil {
return nil, err
}

return &result, nil

}

// DeleteBranch Deletes a single branch in the artifact.
// See https://www.apicur.io/registry/docs/apicurio-registry/3.0.x/assets-attachments/registry-rest-api.htm#tag/Branches/operation/deleteBranch
func (api *BranchAPI) DeleteBranch(ctx context.Context, groupId, artifactId, branchId string) error {
if err := validateInput(artifactId, regexGroupIDArtifactID, "Artifact ID"); err != nil {
return err
}
if err := validateInput(groupId, regexGroupIDArtifactID, "Group ID"); err != nil {
return err
}
if err := validateInput(branchId, regexBranchID, "Branch ID"); err != nil {
return err
}

url := fmt.Sprintf("%s/groups/%s/artifacts/%s/branches/%s", api.Client.BaseURL, groupId, artifactId, branchId)
resp, err := api.executeRequest(ctx, http.MethodDelete, url, nil)
if err != nil {
return err
}

if err := handleResponse(resp, http.StatusNoContent, nil); err != nil {
return err
}

return nil
}

// GetVersionsInBranch Get a list of all versions in the branch.
// Returns a list of version identifiers in the branch, ordered from the latest (tip of the branch) to the oldest.
// See https://www.apicur.io/registry/docs/apicurio-registry/3.0.x/assets-attachments/registry-rest-api.htm#tag/Branches/operation/listBranchVersions
func (api *BranchAPI) GetVersionsInBranch(ctx context.Context, groupId, artifactId, branchId string, params *models.ListBranchesParams) ([]models.ArtifactVersion, error) {
if err := validateInput(artifactId, regexGroupIDArtifactID, "Artifact ID"); err != nil {
return nil, err
}
if err := validateInput(groupId, regexGroupIDArtifactID, "Group ID"); err != nil {
return nil, err
}
if err := validateInput(branchId, regexBranchID, "Branch ID"); err != nil {
return nil, err
}

query := ""
if params != nil {
if err := params.Validate(); err != nil {
return nil, errors.Wrap(err, "invalid parameters provided")
}
query = "?" + params.ToQuery().Encode()
}

url := fmt.Sprintf("%s/groups/%s/artifacts/%s/branches/%s/versions%s", api.Client.BaseURL, groupId, artifactId, branchId, query)
resp, err := api.executeRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}

var result models.ArtifactVersionListResponse
if err := handleResponse(resp, http.StatusOK, &result); err != nil {
return nil, err
}

return result.Versions, nil
}

// ReplaceVersionsInBranch Add a new version to an artifact branch. Branch is created if it does not exist.
// Returns a list of version identifiers in the artifact branch, ordered from the latest (tip of the branch) to the oldest.
// See https://www.apicur.io/registry/docs/apicurio-registry/3.0.x/assets-attachments/registry-rest-api.htm#tag/Branches/operation/replaceBranchVersions
func (api *BranchAPI) ReplaceVersionsInBranch(ctx context.Context, groupId, artifactId, branchId string, versions []string) error {
if err := validateInput(artifactId, regexGroupIDArtifactID, "Artifact ID"); err != nil {
return err
}
if err := validateInput(groupId, regexGroupIDArtifactID, "Group ID"); err != nil {
return err
}
if err := validateInput(branchId, regexBranchID, "Branch ID"); err != nil {
return err
}

if len(versions) == 0 {
return errors.New("versions must not be empty")
}

for _, version := range versions {
err := validateInput(version, regexVersion, "Version")
if err != nil {
return err
}
}

url := fmt.Sprintf("%s/groups/%s/artifacts/%s/branches/%s/versions", api.Client.BaseURL, groupId, artifactId, branchId)

requestBody := map[string]interface{}{
"versions": versions,
}

resp, err := api.executeRequest(ctx, http.MethodPut, url, requestBody)
if err != nil {
return err
}

if err := handleResponse(resp, http.StatusNoContent, nil); err != nil {
return err
}

return nil

}

// AddVersionToBranch Add a new version to an artifact branch.
// See https://www.apicur.io/registry/docs/apicurio-registry/3.0.x/assets-attachments/registry-rest-api.htm#tag/Branches/operation/addVersionToBranch
func (api *BranchAPI) AddVersionToBranch(ctx context.Context, groupId, artifactId, branchId, version string) error {
if err := validateInput(artifactId, regexGroupIDArtifactID, "Artifact ID"); err != nil {
return err
}
if err := validateInput(groupId, regexGroupIDArtifactID, "Group ID"); err != nil {
return err
}
if err := validateInput(branchId, regexBranchID, "Branch ID"); err != nil {
return err
}
if err := validateInput(version, regexVersion, "Version"); err != nil {
return err
}

url := fmt.Sprintf("%s/groups/%s/artifacts/%s/branches/%s/versions", api.Client.BaseURL, groupId, artifactId, branchId)

requestBody := map[string]interface{}{
"version": version,
}
resp, err := api.executeRequest(ctx, http.MethodPost, url, requestBody)
if err != nil {
return err
}

if err := handleResponse(resp, http.StatusNoContent, nil); err != nil {
return err
}

return nil
}

// executeRequest handles the creation and execution of an HTTP request.
func (api *BranchAPI) executeRequest(ctx context.Context, method, url string, body interface{}) (*http.Response, error) {
var reqBody []byte
var err error
contentType := "*/*"

switch v := body.(type) {
case string:
reqBody = []byte(v)
contentType = "*/*"
case []byte:
reqBody = v
contentType = "*/*"
default:
contentType = "application/json"
reqBody, err = json.Marshal(body)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal request body as JSON")
}
}

// Create the HTTP request
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(reqBody))
if err != nil {
return nil, errors.Wrap(err, "failed to create HTTP request")
}

// Set appropriate Content-Type header
if body != nil {
req.Header.Set("Content-Type", contentType)
}

// Execute the request
resp, err := api.Client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to execute HTTP request")
}

return resp, nil
}
Loading