/
endpoint.go
78 lines (65 loc) · 1.9 KB
/
endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package jengo_src
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
)
type Kwargs map[string]interface{}
func GetEndpoint(Kwargs map[string]interface{}) string {
switch Kwargs["name"] {
// Jobs`
case "jobs":
return "/api/json?tree=jobs[name,color,buildable,url,description]"
case "job":
endpoint := fmt.Sprintf("/job/%s/api/json?pretty=true", Kwargs["job_name"])
return endpoint
// Builds
case "builds":
endpoint := fmt.Sprintf("/job/%s/api/json?tree=builds[number,status,timestamp,id,result,estimatedDuration,duration,executor,description,url]", Kwargs["job_name"])
return endpoint
case "build":
endpoint := fmt.Sprintf("/job/%s/%s/api/json?pretty=true", Kwargs["job_name"], Kwargs["build_name"])
return endpoint
// Nodes
case "nodes":
return "/computer/api/json?pretty=true"
case "node":
endpoint := fmt.Sprintf("/computer/(%s)/api/json?pretty=true", Kwargs["node_name"])
return endpoint
// Plugins
case "plugins":
return "/pluginManager/api/json?depth=1"
// Credentials
case "credentials":
return "/credentials/store/system/domain/_/api/json?pretty=true"
// Update Center
case "update-center":
return "/updateCenter/site/default/api/json?pretty=true&depth=1"
// User
case "user":
endpoint := fmt.Sprintf("/securityRealm/user/%s/api/json", Kwargs["user_name"])
return endpoint
// Default: Endpoint not found
default:
panic(errors.New("Not possible endpoint"))
}
}
func HandleRequest(METHOD string, Kwargs map[string]interface{}) ([]byte, error) {
req := CreateRequest(METHOD, GetEndpoint(Kwargs))
// Send req using http Client
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
ErrorLog(err)
}
if response.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("Erorr: %v", response.StatusCode))
}
defer response.Body.Close()
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
ErrorLog(err)
}
return responseData, nil
}