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

     1  package repositories
     2  
     3  import (
     4  	"errors"
     5  	"github.com/cobalt77/jfrog-client-go/bintray/auth"
     6  	"github.com/cobalt77/jfrog-client-go/httpclient"
     7  	"github.com/cobalt77/jfrog-client-go/utils/errorutils"
     8  	"net/http"
     9  	"path"
    10  )
    11  
    12  func NewService(client *httpclient.HttpClient) *RepositoryService {
    13  	us := &RepositoryService{client: client}
    14  	return us
    15  }
    16  
    17  type RepositoryService struct {
    18  	client         *httpclient.HttpClient
    19  	BintrayDetails auth.BintrayDetails
    20  }
    21  
    22  type Path struct {
    23  	Subject string
    24  	Repo    string
    25  }
    26  
    27  func (rs *RepositoryService) IsRepoExists(repositoryPath *Path) (bool, error) {
    28  	url := rs.BintrayDetails.GetApiUrl() + path.Join("repos", repositoryPath.Subject, repositoryPath.Repo)
    29  	httpClientsDetails := rs.BintrayDetails.CreateHttpClientDetails()
    30  
    31  	client, err := httpclient.ClientBuilder().Build()
    32  	if err != nil {
    33  		return false, err
    34  	}
    35  	resp, _, err := client.SendHead(url, httpClientsDetails)
    36  	if err != nil {
    37  		return false, err
    38  	}
    39  	if resp.StatusCode == http.StatusOK {
    40  		return true, nil
    41  	}
    42  	if resp.StatusCode == http.StatusNotFound {
    43  		return false, nil
    44  	}
    45  
    46  	return false, errorutils.CheckError(errors.New("Bintray response: " + resp.Status))
    47  }