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

     1  package services
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"net/http"
     7  	"strings"
     8  
     9  	rthttpclient "github.com/cobalt77/jfrog-client-go/artifactory/httpclient"
    10  	"github.com/cobalt77/jfrog-client-go/auth"
    11  	"github.com/cobalt77/jfrog-client-go/utils"
    12  	"github.com/cobalt77/jfrog-client-go/utils/errorutils"
    13  )
    14  
    15  type SystemService struct {
    16  	client     *rthttpclient.ArtifactoryHttpClient
    17  	ArtDetails auth.ServiceDetails
    18  }
    19  
    20  func NewSystemService(client *rthttpclient.ArtifactoryHttpClient) *SystemService {
    21  	return &SystemService{client: client}
    22  }
    23  
    24  func (ss *SystemService) GetArtifactoryDetails() auth.ServiceDetails {
    25  	return ss.ArtDetails
    26  }
    27  
    28  func (ss *SystemService) SetArtifactoryDetails(rt auth.ServiceDetails) {
    29  	ss.ArtDetails = rt
    30  }
    31  
    32  func (ss *SystemService) GetJfrogHttpClient() (*rthttpclient.ArtifactoryHttpClient, error) {
    33  	return ss.client, nil
    34  }
    35  
    36  func (ss *SystemService) IsDryRun() bool {
    37  	return false
    38  }
    39  
    40  func (ss *SystemService) GetVersion() (string, error) {
    41  	httpDetails := ss.ArtDetails.CreateHttpClientDetails()
    42  	resp, body, _, err := ss.client.SendGet(ss.ArtDetails.GetUrl()+"api/system/version", true, &httpDetails)
    43  	if err != nil {
    44  		return "", err
    45  	}
    46  
    47  	if resp.StatusCode != http.StatusOK {
    48  		return "", errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + utils.IndentJson(body)))
    49  	}
    50  	var version artifactoryVersion
    51  	err = json.Unmarshal(body, &version)
    52  	if err != nil {
    53  		return "", errorutils.CheckError(err)
    54  	}
    55  	return strings.TrimSpace(version.Version), nil
    56  }
    57  
    58  func (ss *SystemService) GetServiceId() (string, error) {
    59  	httpDetails := ss.ArtDetails.CreateHttpClientDetails()
    60  	resp, body, _, err := ss.client.SendGet(ss.ArtDetails.GetUrl()+"api/system/service_id", true, &httpDetails)
    61  	if err != nil {
    62  		return "", err
    63  	}
    64  	if resp.StatusCode != http.StatusOK {
    65  		return "", errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + utils.IndentJson(body)))
    66  	}
    67  
    68  	return string(body), nil
    69  }
    70  
    71  type artifactoryVersion struct {
    72  	Version string `json:"version,omitempty"`
    73  }