github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/services/getreplication.go (about)

     1  package services
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"net/http"
     7  
     8  	rthttpclient "github.com/cobalt77/jfrog-client-go/artifactory/httpclient"
     9  	"github.com/cobalt77/jfrog-client-go/artifactory/services/utils"
    10  	"github.com/cobalt77/jfrog-client-go/auth"
    11  	clientutils "github.com/cobalt77/jfrog-client-go/utils"
    12  	"github.com/cobalt77/jfrog-client-go/utils/errorutils"
    13  	"github.com/cobalt77/jfrog-client-go/utils/log"
    14  )
    15  
    16  type GetReplicationService struct {
    17  	client     *rthttpclient.ArtifactoryHttpClient
    18  	ArtDetails auth.ServiceDetails
    19  }
    20  
    21  func NewGetReplicationService(client *rthttpclient.ArtifactoryHttpClient) *GetReplicationService {
    22  	return &GetReplicationService{client: client}
    23  }
    24  
    25  func (drs *GetReplicationService) GetJfrogHttpClient() *rthttpclient.ArtifactoryHttpClient {
    26  	return drs.client
    27  }
    28  
    29  func (drs *GetReplicationService) GetReplication(repoKey string) ([]utils.ReplicationParams, error) {
    30  	body, err := drs.preform(repoKey)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	var replicationConf []utils.ReplicationParams
    35  	if err := json.Unmarshal(body, &replicationConf); err != nil {
    36  		return nil, errorutils.CheckError(err)
    37  	}
    38  	return replicationConf, nil
    39  }
    40  
    41  func (drs *GetReplicationService) preform(repoKey string) ([]byte, error) {
    42  	httpClientsDetails := drs.ArtDetails.CreateHttpClientDetails()
    43  	log.Info("Retrieve replication configuration...")
    44  	resp, body, _, err := drs.client.SendGet(drs.ArtDetails.GetUrl()+"api/replications/"+repoKey, true, &httpClientsDetails)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	if resp.StatusCode != http.StatusOK {
    49  		return nil, errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + clientutils.IndentJson(body)))
    50  	}
    51  	log.Debug("Artifactory response:", resp.Status)
    52  	log.Info("Done retrieve replication job.")
    53  	return body, nil
    54  }