-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
131 lines (119 loc) · 4.03 KB
/
api.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// This file contains all the api calls
package circleci
import (
"github.com/parnurzeal/gorequest"
"errors"
"encoding/json"
"strconv"
)
const (
BASE_URL = "https://circleci.com/api/v1/"
)
// Does api call with the superagent
func makeCallWithRequest(req *gorequest.SuperAgent, token string) (*string, error) {
resp, body, errs := req.Param("circle-token", token).Set("accept", "application/json").End()
if errs != nil {
var errorMessage string
for _, err := range errs {
errorMessage += err.Error() + "\n"
}
return nil, errors.New(errorMessage)
}
if resp.StatusCode != 200 {
return nil, errors.New(body)
}
return &body, nil
}
// Does the api call on the given path
func makeCallOnPath(path, token string) (*string, error) {
resp, body, errs := gorequest.New().Get(BASE_URL + path).Param("circle-token", token).Set("accept", "application/json").End()
if errs != nil {
var errorMessage string
for _, err := range errs {
errorMessage += err.Error() + "\n"
}
return nil, errors.New(errorMessage)
}
if resp.StatusCode != 200 {
return nil, errors.New(body)
}
return &body, nil
}
// Marshals the json into the map of string and interface
func marshalJSONObject(value *string) (*map[string]interface{}, error) {
var resp map[string]interface{}
err := json.Unmarshal([]byte(*value), &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// marshals the json array into the interface
func marshalJSONArray(value *string) (*[]interface{}, error) {
var resp []interface{}
err := json.Unmarshal([]byte(*value), &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
//GET: /me
//Provides information about the signed in user.
func Me(token string) (*map[string]interface{}, error) {
body, err := makeCallOnPath("me", token)
if err != nil {
return nil, err
}
return marshalJSONObject(body)
}
//GET: /projects
// Lists all the projects on the circle ci profile
func Projects(token string) (*[]interface{}, error) {
body, err := makeCallOnPath("projects", token)
if err != nil {
return nil, err
}
return marshalJSONArray(body)
}
//GET: /project/:username/:project
//Build summary for each of the last 30 builds for a single git repo.
func RecentBuildsFor(token, username, project string, limit, offset int, filter string) (*[]interface{}, error) {
req := gorequest.New().Get(BASE_URL + "project/" + username + "/" + project).Param("limit", strconv.Itoa(limit)).Param("offset", strconv.Itoa(offset)).Param("filter", filter);
// query struct for the api call
body, err := makeCallWithRequest(req, token)
if err != nil {
return nil, err
}
return marshalJSONArray(body)
}
func GetBuildForProjectAndBranch(token, username, project, branch string, buildNumber int) (*map[string]interface{}, error) {
req := gorequest.New().Get(BASE_URL + "project/" + username + "/" + project + "/" + strconv.Itoa(buildNumber));
// query struct for the api call
body, err := makeCallWithRequest(req, token)
if err != nil {
return nil, err
}
return marshalJSONObject(body)
}
//GET: /project/:username/:project/tree/:branch
//Build summary for each of the last 30 builds for a single git repo.
func RecentBuildsForBranch(token, username, project, branch string, limit, offset int, filter string) (*[]interface{}, error) {
req := gorequest.New().Get(BASE_URL + "project/" + username + "/" + project + "/tree/" + branch).Param("limit", strconv.Itoa(limit)).Param("offset", strconv.Itoa(offset)).Param("filter", filter);
// query struct for the api call
body, err := makeCallWithRequest(req, token)
if err != nil {
return nil, err
}
return marshalJSONArray(body)
}
//GET: /project/:username/:project/:build_num/artifacts
//List the artifacts produced by a given build.
func GetArtifactsOfBuildNoForProject(token, username, project string, buildNum int) (*[]interface{}, error) {
req := gorequest.New().Get(BASE_URL + "project/" + username + "/" + project + "/" + strconv.Itoa(buildNum) + "/artifacts");
// query struct for the api call
body, err := makeCallWithRequest(req, token)
if err != nil {
return nil, err
}
return marshalJSONArray(body)
}