-
Notifications
You must be signed in to change notification settings - Fork 2
/
entity_update.go
134 lines (118 loc) · 3.1 KB
/
entity_update.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
131
132
133
134
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"github.com/gorilla/mux"
)
type updateResponse struct {
Results map[string]interface{} `json:"results"`
Exception bool `json:"exception,omitempty"`
Message string `json:"message,omitempty"`
ErrorCode int `json:"error_code,omitempty"`
}
func entityUpdateHandler(config clientConfig) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
entityType, ok := vars["entity_type"]
if !ok {
log.Errorf("Missing Entity Type")
rw.WriteHeader(http.StatusBadRequest)
return
}
var entityID int
var err error
entityIDStr, ok := vars["id"]
if ok {
entityID, err = strconv.Atoi(entityIDStr)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
return
}
} else {
rw.WriteHeader(http.StatusBadRequest)
return
}
log.Debugf("Entity: %s - %d", entityType, entityID)
var patchData map[string]interface{}
patchBody, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Error(err)
rw.WriteHeader(http.StatusBadRequest)
return
}
err = json.Unmarshal(patchBody, &patchData)
if err != nil {
log.Error(err)
rw.WriteHeader(http.StatusBadRequest)
return
}
log.Info("Patch Data:", patchData)
fields := make([]map[string]interface{}, len(patchData))
i := 0
for key, value := range patchData {
field := make(map[string]interface{})
field["field_name"] = key
field["value"] = value
fields[i] = field
i++
}
query := map[string]interface{}{
"type": entityType,
"id": entityID,
"fields": fields,
}
sgConn, ok := context.GetOk(req, "sgConn")
if !ok {
rw.WriteHeader(http.StatusInternalServerError)
return
}
sg := sgConn.(Shotgun)
sgReq, err := sg.Request("update", query)
if err != nil {
log.Error("Request Error: ", err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
var updateResp updateResponse
respBody, err := ioutil.ReadAll(sgReq.Body)
if err != nil {
log.Error(err)
rw.WriteHeader(http.StatusBadGateway)
return
}
err = json.Unmarshal(respBody, &updateResp)
if err != nil {
log.Error(err)
rw.WriteHeader(http.StatusBadGateway)
return
}
log.Debug("Response: ", updateResp)
if updateResp.Exception {
if strings.Contains(updateResp.Message, "unique") {
rw.WriteHeader(http.StatusConflict)
} else if strings.Contains(updateResp.Message, "Permission") {
rw.WriteHeader(http.StatusForbidden)
} else if strings.Contains(updateResp.Message, "does not exist") {
rw.WriteHeader(http.StatusNotFound)
} else {
rw.WriteHeader(http.StatusBadRequest)
}
rw.Write(bytes.NewBufferString(updateResp.Message).Bytes())
return
}
jsonResp, err := json.Marshal(updateResp.Results)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusOK)
rw.Write(jsonResp)
}
}