|
1 | 1 | package api
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "errors" |
5 | 4 | "fmt"
|
6 | 5 | "log"
|
7 | 6 | "strconv"
|
8 | 7 | )
|
9 | 8 |
|
10 | 9 | func (api *API) CreateNotification(instanceID int, params map[string]interface{}) (map[string]interface{}, error) {
|
11 |
| - data := make(map[string]interface{}) |
12 |
| - failed := make(map[string]interface{}) |
13 |
| - log.Printf("[DEBUG] go-api::notification::create instance ID: %v, params: %v", instanceID, params) |
14 |
| - path := fmt.Sprintf("/api/instances/%d/alarms/recipients", instanceID) |
| 10 | + var ( |
| 11 | + data map[string]interface{} |
| 12 | + failed map[string]interface{} |
| 13 | + path = fmt.Sprintf("/api/instances/%d/alarms/recipients", instanceID) |
| 14 | + ) |
| 15 | + |
| 16 | + log.Printf("[DEBUG] go-api::notification::create path: %s", path) |
15 | 17 | response, err := api.sling.New().Post(path).BodyJSON(params).Receive(&data, &failed)
|
16 | 18 | log.Printf("[DEBUG] go-api::notification::create data: %v", data)
|
17 | 19 |
|
18 | 20 | if err != nil {
|
19 | 21 | return nil, err
|
20 | 22 | }
|
21 | 23 | if response.StatusCode != 201 {
|
22 |
| - return nil, fmt.Errorf("CreateNotification failed, status: %v, message: %s", response.StatusCode, failed) |
| 24 | + return nil, fmt.Errorf("create notification failed, status: %d, message: %s", |
| 25 | + response.StatusCode, failed) |
23 | 26 | }
|
24 | 27 |
|
25 | 28 | if v, ok := data["id"]; ok {
|
26 | 29 | data["id"] = strconv.FormatFloat(v.(float64), 'f', 0, 64)
|
27 |
| - log.Printf("[DEBUG] go-api::notification::create id set: %v", data["id"]) |
28 | 30 | } else {
|
29 |
| - msg := fmt.Sprintf("go-api::notification::create Invalid notification identifier: %v", data["id"]) |
30 |
| - log.Printf("[ERROR] %s", msg) |
31 |
| - return nil, errors.New(msg) |
| 31 | + return nil, fmt.Errorf("create notification invalid identifier: %v", data["id"]) |
32 | 32 | }
|
33 | 33 |
|
34 | 34 | return data, err
|
35 | 35 | }
|
36 | 36 |
|
37 | 37 | func (api *API) ReadNotification(instanceID int, recipientID string) (map[string]interface{}, error) {
|
38 |
| - data := make(map[string]interface{}) |
39 |
| - failed := make(map[string]interface{}) |
40 |
| - log.Printf("[DEBUG] go-api::notification::read instance ID: %v, recipient ID: %v", instanceID, recipientID) |
41 |
| - path := fmt.Sprintf("/api/instances/%v/alarms/recipients/%v", instanceID, recipientID) |
| 38 | + var ( |
| 39 | + data map[string]interface{} |
| 40 | + failed map[string]interface{} |
| 41 | + path = fmt.Sprintf("/api/instances/%d/alarms/recipients/%s", instanceID, recipientID) |
| 42 | + ) |
| 43 | + |
| 44 | + log.Printf("[DEBUG] go-api::notification::read path: %s", path) |
42 | 45 | response, err := api.sling.New().Path(path).Receive(&data, &failed)
|
43 | 46 | log.Printf("[DEBUG] go-api::notification::read data: %v", data)
|
44 | 47 |
|
45 | 48 | if err != nil {
|
46 | 49 | return nil, err
|
47 | 50 | }
|
48 | 51 | if response.StatusCode != 200 {
|
49 |
| - return nil, fmt.Errorf("ReadNotification failed, status: %v, message: %s", response.StatusCode, failed) |
| 52 | + return nil, fmt.Errorf("read notification failed, status: %v, message: %s", |
| 53 | + response.StatusCode, failed) |
50 | 54 | }
|
51 | 55 |
|
52 | 56 | return data, err
|
53 | 57 | }
|
54 | 58 |
|
55 | 59 | func (api *API) ReadNotifications(instanceID int) ([]map[string]interface{}, error) {
|
56 |
| - var data []map[string]interface{} |
57 |
| - failed := make(map[string]interface{}) |
58 |
| - log.Printf("[DEBUG] go-api::ReadNotifications::read instance ID: %v", instanceID) |
59 |
| - path := fmt.Sprintf("/api/instances/%d/alarms/recipients", instanceID) |
| 60 | + var ( |
| 61 | + data []map[string]interface{} |
| 62 | + failed map[string]interface{} |
| 63 | + path = fmt.Sprintf("/api/instances/%d/alarms/recipients", instanceID) |
| 64 | + ) |
| 65 | + |
| 66 | + log.Printf("[DEBUG] go-api::ReadNotifications::read path: %s", path) |
60 | 67 | response, err := api.sling.New().Path(path).Receive(&data, &failed)
|
61 | 68 | log.Printf("[DEBUG] go-api::ReadNotifications::read data: %v", data)
|
62 | 69 |
|
63 | 70 | if err != nil {
|
64 | 71 | return nil, err
|
65 | 72 | }
|
66 | 73 | if response.StatusCode != 200 {
|
67 |
| - return nil, fmt.Errorf("ReadNotifications failed, status: %v, message: %s", response.StatusCode, failed) |
| 74 | + return nil, fmt.Errorf("read notification failed, status: %d, message: %s", |
| 75 | + response.StatusCode, failed) |
68 | 76 | }
|
69 | 77 |
|
70 | 78 | return data, err
|
71 | 79 | }
|
72 | 80 |
|
73 |
| -func (api *API) UpdateNotification(instanceID int, params map[string]interface{}) error { |
74 |
| - failed := make(map[string]interface{}) |
75 |
| - log.Printf("[DEBUG] go-api::notification::update instance id: %v, params: %v", instanceID, params) |
76 |
| - path := fmt.Sprintf("/api/instances/%d/alarms/recipients/%v", instanceID, params["id"]) |
| 81 | +func (api *API) UpdateNotification(instanceID int, recipientID string, params map[string]interface{}) error { |
| 82 | + var ( |
| 83 | + failed map[string]interface{} |
| 84 | + path = fmt.Sprintf("/api/instances/%d/alarms/recipients/%s", instanceID, recipientID) |
| 85 | + ) |
| 86 | + |
| 87 | + log.Printf("[DEBUG] go-api::notification::update path: %s", path) |
77 | 88 | response, err := api.sling.New().Put(path).BodyJSON(params).Receive(nil, &failed)
|
78 | 89 |
|
79 | 90 | if response.StatusCode != 200 {
|
80 |
| - return fmt.Errorf("UpdateNotification failed, status: %v, message: %s", response.StatusCode, failed) |
| 91 | + return fmt.Errorf("update notification failed, status: %d, message: %s", |
| 92 | + response.StatusCode, failed) |
81 | 93 | }
|
82 | 94 |
|
83 | 95 | return err
|
84 | 96 | }
|
85 | 97 |
|
86 |
| -func (api *API) DeleteNotification(instanceID int, params map[string]interface{}) error { |
87 |
| - failed := make(map[string]interface{}) |
88 |
| - log.Printf("[DEBUG] go-api::notification::delete instance id: %v, params: %v", instanceID, params) |
89 |
| - path := fmt.Sprintf("/api/instances/%v/alarms/recipients/%v", instanceID, params["id"]) |
90 |
| - response, err := api.sling.New().Delete(path).BodyJSON(params).Receive(nil, &failed) |
| 98 | +func (api *API) DeleteNotification(instanceID int, recipientID string) error { |
| 99 | + var ( |
| 100 | + failed map[string]interface{} |
| 101 | + path = fmt.Sprintf("/api/instances/%d/alarms/recipients/%s", instanceID, recipientID) |
| 102 | + ) |
| 103 | + |
| 104 | + log.Printf("[DEBUG] go-api::notification::delete path: %s", path) |
| 105 | + response, err := api.sling.New().Delete(path).Receive(nil, &failed) |
91 | 106 |
|
92 | 107 | if response.StatusCode != 204 {
|
93 |
| - return fmt.Errorf("DeleteNotification failed, status: %v, message: %s", response.StatusCode, failed) |
| 108 | + return fmt.Errorf("delete notification failed, status: %d, message: %s", |
| 109 | + response.StatusCode, failed) |
94 | 110 | }
|
95 | 111 |
|
96 | 112 | return err
|
|
0 commit comments