-
Notifications
You must be signed in to change notification settings - Fork 2
/
materials.go
126 lines (101 loc) · 3.49 KB
/
materials.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
package gocd
import (
"encoding/json"
"fmt"
"net/http"
"github.com/jinzhu/copier"
"github.com/nikhilsbhat/gocd-sdk-go/pkg/errors"
)
func (conf *client) GetMaterials() ([]Material, error) {
newClient := &client{}
if err := copier.CopyWithOption(newClient, conf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil {
return nil, err
}
var materials Materials
resp, err := newClient.httpClient.R().
SetHeaders(map[string]string{
"Accept": HeaderVersionZero,
}).
Get(MaterialEndpoint)
if err != nil {
return nil, &errors.APIError{Err: err, Message: "get all available materials"}
}
if resp.StatusCode() != http.StatusOK {
return nil, &errors.NonOkError{Code: resp.StatusCode(), Response: resp}
}
if err = json.Unmarshal(resp.Body(), &materials); err != nil {
return nil, &errors.MarshalError{Err: err}
}
return materials.Materials, nil
}
func (conf *client) GetMaterialUsage(materialID string) ([]string, error) {
newClient := &client{}
if err := copier.CopyWithOption(newClient, conf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil {
return nil, err
}
var materialUsage MaterialUsage
resp, err := newClient.httpClient.R().
SetHeaders(map[string]string{
"Accept": HeaderVersionZero,
}).
Get(fmt.Sprintf(MaterialUsageEndpoint, materialID))
if err != nil {
return nil, &errors.APIError{Err: err, Message: fmt.Sprintf("get material usage '%s'", materialID)}
}
if resp.StatusCode() != http.StatusOK {
return nil, &errors.NonOkError{Code: resp.StatusCode(), Response: resp}
}
if err = json.Unmarshal(resp.Body(), &materialUsage); err != nil {
return nil, &errors.MarshalError{Err: err}
}
return materialUsage.Usages, nil
}
func (conf *client) NotifyMaterial(material Material) (string, error) {
newClient := &client{}
if err := copier.CopyWithOption(newClient, conf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil {
return "", err
}
resp, err := newClient.httpClient.R().
SetHeaders(map[string]string{
"Accept": HeaderVersionTwo,
"Content-Type": ContentJSON,
}).
SetBody(map[string]string{
"repository_url": material.RepoURL,
}).
Post(fmt.Sprintf(MaterialNotifyEndpoint, material.Type))
if err != nil {
return "", &errors.APIError{Err: err, Message: fmt.Sprintf("notify material '%s' of type %s", material.RepoURL, material.Type)}
}
if resp.StatusCode() != http.StatusAccepted {
return "", &errors.NonOkError{Code: resp.StatusCode(), Response: resp}
}
notifyMessage := map[string]string{}
if err = json.Unmarshal(resp.Body(), ¬ifyMessage); err != nil {
return "", &errors.MarshalError{Err: err}
}
return notifyMessage["message"], nil
}
func (conf *client) MaterialTriggerUpdate(materialID string) (map[string]string, error) {
newClient := &client{}
if err := copier.CopyWithOption(newClient, conf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil {
return nil, err
}
resp, err := newClient.httpClient.R().
SetHeaders(map[string]string{
"Accept": HeaderVersionZero,
HeaderConfirm: "true",
}).
Post(fmt.Sprintf(MaterialTriggerUpdate, materialID))
if err != nil {
return nil, &errors.APIError{Err: err, Message: fmt.Sprintf("trigger update '%s'", materialID)}
}
if resp.StatusCode() != http.StatusCreated {
return nil, &errors.NonOkError{Code: resp.StatusCode(), Response: resp}
}
updateMessage := map[string]string{}
if err = json.Unmarshal(resp.Body(), &updateMessage); err != nil {
return nil, &errors.MarshalError{Err: err}
}
return updateMessage, nil
}