mirror of
https://tvoygit.ru/Djam/abfapi.git
synced 2025-02-23 18:32:46 +00:00
update abfapi.AbfJson methods
This commit is contained in:
parent
e6f1c3e66f
commit
6b0a21ce87
1 changed files with 35 additions and 42 deletions
77
abfapi.go
77
abfapi.go
|
@ -44,9 +44,9 @@ type AbfJson struct {
|
|||
client *http.Client
|
||||
}
|
||||
|
||||
func NewAbfJson(abfURL, fileStoreURL, login, password string, logger *log.Logger, timeout time.Duration) (*AbfJson, error) {
|
||||
func NewAbfJson(abfURL, fileStoreURL, login, password string, logger *log.Logger) (AbfJson, error) {
|
||||
if !strings.HasPrefix(fileStoreURL, "http://") && !strings.HasPrefix(fileStoreURL, "https://") {
|
||||
return nil, errors.New("file-store URL has to start with \"http(s)://\"")
|
||||
return AbfJson{}, errors.New("file-store URL has to start with \"http(s)://\"")
|
||||
}
|
||||
|
||||
cacheDir := filepath.Join(os.TempDir(), "abf_cache")
|
||||
|
@ -56,13 +56,13 @@ func NewAbfJson(abfURL, fileStoreURL, login, password string, logger *log.Logger
|
|||
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
Timeout: timeout,
|
||||
Timeout: 60 * time.Second, // Установите фиксированное значение таймаута или используйте другой способ установки
|
||||
}
|
||||
|
||||
lpw := fmt.Sprintf("%s:%s", login, password)
|
||||
encodedLpw := base64.StdEncoding.EncodeToString([]byte(lpw))
|
||||
|
||||
return &AbfJson{
|
||||
return AbfJson{
|
||||
login: login,
|
||||
password: password,
|
||||
abfURL: strings.TrimSuffix(abfURL, "/"),
|
||||
|
@ -106,13 +106,7 @@ func (a *AbfJson) bytesToHuman(n int64, format string, symbols string) string {
|
|||
}
|
||||
|
||||
func (a *AbfJson) getURLContents(path string, params url.Values, method string, body io.Reader) ([]byte, error) {
|
||||
var fullURL string
|
||||
if strings.HasPrefix(path, "/api/v1/upload") {
|
||||
fullURL = a.fileStoreURL + path
|
||||
} else {
|
||||
fullURL = a.abfURL + path
|
||||
}
|
||||
|
||||
fullURL := path
|
||||
if len(params) > 0 {
|
||||
fullURL += "?" + params.Encode()
|
||||
}
|
||||
|
@ -147,8 +141,7 @@ func (a *AbfJson) getURLContents(path string, params url.Values, method string,
|
|||
|
||||
func (a *AbfJson) processResponse(data []byte) (map[string]interface{}, error) {
|
||||
var response map[string]interface{}
|
||||
err := json.Unmarshal(data, &response)
|
||||
if err != nil {
|
||||
if err := json.Unmarshal(data, &response); err != nil {
|
||||
return nil, fmt.Errorf("internal server error: it has returned non-json data")
|
||||
}
|
||||
|
||||
|
@ -169,7 +162,7 @@ func (a *AbfJson) processResponse(data []byte) (map[string]interface{}, error) {
|
|||
}
|
||||
|
||||
func (a *AbfJson) GetArchitectures() (map[string]interface{}, error) {
|
||||
data, err := a.getURLContents("/api/v1/arches.json", nil, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+"/api/v1/arches.json", nil, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -182,7 +175,7 @@ func (a *AbfJson) GetPlatforms(typ string) (map[string]interface{}, error) {
|
|||
if typ != "" {
|
||||
params["type"] = []string{typ}
|
||||
}
|
||||
data, err := a.getURLContents("/api/v1/platforms.json", params, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+"/api/v1/platforms.json", params, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -192,7 +185,7 @@ func (a *AbfJson) GetPlatforms(typ string) (map[string]interface{}, error) {
|
|||
|
||||
func (a *AbfJson) GetPlatformByID(plID int) (map[string]interface{}, error) {
|
||||
path := fmt.Sprintf("/api/v1/platforms/%d.json", plID)
|
||||
data, err := a.getURLContents(path, nil, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+path, nil, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -202,7 +195,7 @@ func (a *AbfJson) GetPlatformByID(plID int) (map[string]interface{}, error) {
|
|||
|
||||
func (a *AbfJson) GetUserID(username string) (map[string]interface{}, error) {
|
||||
path := fmt.Sprintf("/api/v1/users/%s.json", username)
|
||||
data, err := a.getURLContents(path, nil, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+path, nil, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -211,7 +204,7 @@ func (a *AbfJson) GetUserID(username string) (map[string]interface{}, error) {
|
|||
}
|
||||
|
||||
func (a *AbfJson) GetBuildPlatforms() (map[string]interface{}, error) {
|
||||
data, err := a.getURLContents("/api/v1/platforms/platforms_for_build.json", nil, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+"/api/v1/platforms/platforms_for_build.json", nil, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -221,7 +214,7 @@ func (a *AbfJson) GetBuildPlatforms() (map[string]interface{}, error) {
|
|||
|
||||
func (a *AbfJson) GetRepositoryByID(repID int) (map[string]interface{}, error) {
|
||||
path := fmt.Sprintf("/api/v1/repositories/%d.json", repID)
|
||||
data, err := a.getURLContents(path, nil, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+path, nil, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -231,7 +224,7 @@ func (a *AbfJson) GetRepositoryByID(repID int) (map[string]interface{}, error) {
|
|||
|
||||
func (a *AbfJson) GetBuildListByID(blID int) (map[string]interface{}, error) {
|
||||
path := fmt.Sprintf("/api/v1/build_lists/%d.json", blID)
|
||||
data, err := a.getURLContents(path, nil, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+path, nil, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -243,7 +236,7 @@ func (a *AbfJson) GetListBuildLists(prjID int, filterQuery url.Values, page int)
|
|||
filterQuery.Set("page", fmt.Sprintf("%d", page))
|
||||
filterQuery.Set("per_page", "10")
|
||||
path := fmt.Sprintf("/api/v1/build_lists.json")
|
||||
data, err := a.getURLContents(path, filterQuery, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+path, filterQuery, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -255,7 +248,7 @@ func (a *AbfJson) GetProjectBuildLists(prjID int, filterQuery url.Values, page i
|
|||
filterQuery.Set("page", fmt.Sprintf("%d", page))
|
||||
filterQuery.Set("per_page", "10")
|
||||
path := fmt.Sprintf("/api/v1/projects/%d/build_lists.json", prjID)
|
||||
data, err := a.getURLContents(path, filterQuery, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+path, filterQuery, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -265,7 +258,7 @@ func (a *AbfJson) GetProjectBuildLists(prjID int, filterQuery url.Values, page i
|
|||
|
||||
func (a *AbfJson) GetProjectByID(pID int) (map[string]interface{}, error) {
|
||||
path := fmt.Sprintf("/api/v1/projects/%d.json", pID)
|
||||
data, err := a.getURLContents(path, nil, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+path, nil, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -275,7 +268,7 @@ func (a *AbfJson) GetProjectByID(pID int) (map[string]interface{}, error) {
|
|||
|
||||
func (a *AbfJson) GetProjectIDByName(key [2]string) (map[string]interface{}, error) {
|
||||
params := url.Values{"name": {key[1]}, "owner": {key[0]}}
|
||||
data, err := a.getURLContents("/api/v1/projects/get_id.json", params, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+"/api/v1/projects/get_id.json", params, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -289,7 +282,7 @@ func (a *AbfJson) NewBuildTask(data map[string]interface{}) (map[string]interfac
|
|||
return nil, err
|
||||
}
|
||||
|
||||
dataBytes, err := a.getURLContents("/api/v1/build_lists.json", nil, http.MethodPost, bytes.NewBuffer(jsonData))
|
||||
dataBytes, err := a.getURLContents(a.abfURL+"/api/v1/build_lists.json", nil, http.MethodPost, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -304,7 +297,7 @@ func (a *AbfJson) Publish(taskID int) (map[string]interface{}, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
dataBytes, err := a.getURLContents(path, nil, http.MethodPut, bytes.NewBuffer(jsonData))
|
||||
dataBytes, err := a.getURLContents(a.abfURL+path, nil, http.MethodPut, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -319,7 +312,7 @@ func (a *AbfJson) NewPullRequest(data map[string]interface{}, pID int) (map[stri
|
|||
return nil, err
|
||||
}
|
||||
|
||||
dataBytes, err := a.getURLContents(path, nil, http.MethodPost, bytes.NewBuffer(jsonData))
|
||||
dataBytes, err := a.getURLContents(a.abfURL+path, nil, http.MethodPost, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -334,7 +327,7 @@ func (a *AbfJson) UpdateProject(data map[string]interface{}, pID int) (map[strin
|
|||
return nil, err
|
||||
}
|
||||
|
||||
dataBytes, err := a.getURLContents(path, nil, http.MethodPut, bytes.NewBuffer(jsonData))
|
||||
dataBytes, err := a.getURLContents(a.abfURL+path, nil, http.MethodPut, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -349,7 +342,7 @@ func (a *AbfJson) RemoveProjectFromRepo(data map[string]interface{}, repoID int)
|
|||
return nil, err
|
||||
}
|
||||
|
||||
dataBytes, err := a.getURLContents(path, nil, http.MethodDelete, bytes.NewBuffer(jsonData))
|
||||
dataBytes, err := a.getURLContents(a.abfURL+path, nil, http.MethodDelete, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -364,7 +357,7 @@ func (a *AbfJson) ForkProject(data map[string]interface{}, projID int) (map[stri
|
|||
return nil, err
|
||||
}
|
||||
|
||||
dataBytes, err := a.getURLContents(path, nil, http.MethodPost, bytes.NewBuffer(jsonData))
|
||||
dataBytes, err := a.getURLContents(a.abfURL+path, nil, http.MethodPost, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -379,7 +372,7 @@ func (a *AbfJson) AliasProject(data map[string]interface{}, projID int) (map[str
|
|||
return nil, err
|
||||
}
|
||||
|
||||
dataBytes, err := a.getURLContents(path, nil, http.MethodPost, bytes.NewBuffer(jsonData))
|
||||
dataBytes, err := a.getURLContents(a.abfURL+path, nil, http.MethodPost, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -394,7 +387,7 @@ func (a *AbfJson) DestroyProject(data map[string]interface{}, projID int) (map[s
|
|||
return nil, err
|
||||
}
|
||||
|
||||
dataBytes, err := a.getURLContents(path, nil, http.MethodDelete, bytes.NewBuffer(jsonData))
|
||||
dataBytes, err := a.getURLContents(a.abfURL+path, nil, http.MethodDelete, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -409,7 +402,7 @@ func (a *AbfJson) AddProjectToRepo(data map[string]interface{}, repoID int) (map
|
|||
return nil, err
|
||||
}
|
||||
|
||||
dataBytes, err := a.getURLContents(path, nil, http.MethodPut, bytes.NewBuffer(jsonData))
|
||||
dataBytes, err := a.getURLContents(a.abfURL+path, nil, http.MethodPut, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -423,7 +416,7 @@ func (a *AbfJson) NewProject(data map[string]interface{}) (map[string]interface{
|
|||
return nil, err
|
||||
}
|
||||
|
||||
dataBytes, err := a.getURLContents("/api/v1/projects.json", nil, http.MethodPost, bytes.NewBuffer(jsonData))
|
||||
dataBytes, err := a.getURLContents(a.abfURL+"/api/v1/projects.json", nil, http.MethodPost, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -433,7 +426,7 @@ func (a *AbfJson) NewProject(data map[string]interface{}) (map[string]interface{
|
|||
|
||||
func (a *AbfJson) GetGitRefsList(projID int) (map[string]interface{}, error) {
|
||||
path := fmt.Sprintf("/api/v1/projects/%d/refs_list.json", projID)
|
||||
data, err := a.getURLContents(path, nil, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+path, nil, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -443,7 +436,7 @@ func (a *AbfJson) GetGitRefsList(projID int) (map[string]interface{}, error) {
|
|||
|
||||
func (a *AbfJson) GetUserByID(userID int) (map[string]interface{}, error) {
|
||||
path := fmt.Sprintf("/api/v1/users/%d.json", userID)
|
||||
data, err := a.getURLContents(path, nil, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+path, nil, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -453,7 +446,7 @@ func (a *AbfJson) GetUserByID(userID int) (map[string]interface{}, error) {
|
|||
|
||||
func (a *AbfJson) GetGroupByID(groupID int) (map[string]interface{}, error) {
|
||||
path := fmt.Sprintf("/api/v1/groups/%d.json", groupID)
|
||||
data, err := a.getURLContents(path, nil, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+path, nil, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -463,7 +456,7 @@ func (a *AbfJson) GetGroupByID(groupID int) (map[string]interface{}, error) {
|
|||
|
||||
func (a *AbfJson) GetSearchResults(searchType, query string) (map[string]interface{}, error) {
|
||||
params := url.Values{"type": {searchType}, "query": {query}, "per_page": {"100"}}
|
||||
data, err := a.getURLContents("/api/v1/search.json", params, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+"/api/v1/search.json", params, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -474,7 +467,7 @@ func (a *AbfJson) GetSearchResults(searchType, query string) (map[string]interfa
|
|||
func (a *AbfJson) GetList(listType string, page int) (map[string]interface{}, error) {
|
||||
params := url.Values{"page": {fmt.Sprintf("%d", page)}, "per_page": {"100"}}
|
||||
path := fmt.Sprintf("/api/v1/%s.json", listType)
|
||||
data, err := a.getURLContents(path, params, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+path, params, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -485,7 +478,7 @@ func (a *AbfJson) GetList(listType string, page int) (map[string]interface{}, er
|
|||
func (a *AbfJson) GetProjectsSingle(repoID, page int) (map[string]interface{}, error) {
|
||||
params := url.Values{"page": {fmt.Sprintf("%d", page)}, "per_page": {"100"}}
|
||||
path := fmt.Sprintf("/api/v1/repositories/%d/projects.json", repoID)
|
||||
data, err := a.getURLContents(path, params, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+path, params, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -538,7 +531,7 @@ func (a *AbfJson) UploadFile(filePath string, silent bool) (string, error) {
|
|||
return "", err
|
||||
}
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
file, err = os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -593,7 +586,7 @@ func (a *AbfJson) UploadFile(filePath string, silent bool) (string, error) {
|
|||
|
||||
func (a *AbfJson) GetFileInfoByHash(shaHash string) ([]map[string]interface{}, error) {
|
||||
params := url.Values{"hash": {shaHash}}
|
||||
data, err := a.getURLContents("/api/v1/file_stores.json", params, http.MethodGet, nil)
|
||||
data, err := a.getURLContents(a.abfURL+"/api/v1/file_stores.json", params, http.MethodGet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue