-
Notifications
You must be signed in to change notification settings - Fork 2
/
entity_create.go
121 lines (107 loc) · 2.91 KB
/
entity_create.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
type createResponse struct {
Results map[string]interface{} `json:"results"`
Exception bool `json:"exception,omitempty"`
Message string `json:"message,omitempty"`
ErrorCode int `json:"error_code,omitempty"`
}
func entityCreateHandler(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
}
log.Debugf("Entity: %s", entityType)
var postData map[string]interface{}
postBody, err := ioutil.ReadAll(req.Body)
log.Debugf("Post Body: %s", postBody)
if err != nil {
log.Errorf("Bad Request Body: %v", err)
rw.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(rw, err)
return
}
err = json.Unmarshal(postBody, &postData)
if err != nil {
log.Errorf("Bad Json: %v", err)
rw.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(rw, err)
return
}
log.Debugf("Post Data: %v", postData)
fields := make([]map[string]interface{}, len(postData))
i := 0
for key, value := range postData {
field := make(map[string]interface{})
field["field_name"] = key
field["value"] = value
fields[i] = field
i++
}
query := map[string]interface{}{
"return_fields": []string{"id"},
"type": entityType,
"fields": fields,
}
ctx := req.Context()
sgConn := ctx.Value("sgConn")
if sgConn == nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
sg := sgConn.(Shotgun)
sgReq, err := sg.Request("create", query)
if err != nil {
log.Errorf("Request Error: %s", err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
var createResp createResponse
respBody, err := ioutil.ReadAll(sgReq.Body)
if err != nil {
log.Error(err)
rw.WriteHeader(http.StatusBadGateway)
return
}
log.Debugf("Json Response: %s", respBody)
err = json.Unmarshal(respBody, &createResp)
if err != nil {
log.Error(err)
rw.WriteHeader(http.StatusBadGateway)
return
}
log.Debugf("Response: %s", createResp)
if createResp.Exception {
if strings.Contains(createResp.Message, "unique") {
rw.WriteHeader(http.StatusConflict)
} else if strings.Contains(createResp.Message, "Permission") {
rw.WriteHeader(http.StatusForbidden)
} else {
rw.WriteHeader(http.StatusBadRequest)
}
rw.Write(bytes.NewBufferString(createResp.Message).Bytes())
return
}
jsonResp, err := json.Marshal(createResp.Results)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusCreated)
rw.Write(jsonResp)
}
}