-
Notifications
You must be signed in to change notification settings - Fork 2
/
entity_get.go
120 lines (105 loc) · 2.6 KB
/
entity_get.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"github.com/gorilla/mux"
)
func entityGetHandler(config clientConfig) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
log.Debug("Calling entityGetHandler")
vars := mux.Vars(req)
entityType, ok := vars["entity_type"]
if !ok {
log.Errorf("Missing Entity Type")
rw.WriteHeader(http.StatusBadRequest)
return
}
query := map[string]interface{}{
"return_fields": nil,
"type": entityType,
"return_paging_info": true,
"api_return_image_urls": true,
"return_only": "active",
"paging": map[string]int{
"current_page": 1,
"entities_per_page": 1,
},
"filters": nil,
}
entityIDStr, ok := vars["id"]
if ok {
entityID, err := strconv.Atoi(entityIDStr)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
return
}
query["filters"] = map[string]interface{}{
"logical_operator": "and",
"conditions": []map[string]interface{}{
map[string]interface{}{
"path": "id",
"relation": "is",
"values": []int{int(entityID)},
},
},
}
log.Debugf("Entity: %s - %d", entityType, entityID)
} else {
rw.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(rw, "Id missing")
return
}
req.ParseForm()
fieldsStr := req.FormValue("fields")
fields := []string{"id"}
if fieldsStr != "" {
fields = strings.Split(fieldsStr, ",")
}
query["return_fields"] = fields
log.Debug(query)
sgConn, ok := context.GetOk(req, "sgConn")
if !ok {
rw.WriteHeader(http.StatusInternalServerError)
return
}
sg := sgConn.(Shotgun)
sgReq, err := sg.Request("read", query)
if err != nil {
log.Error("Request Error: ", err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
var readResp readResponse
respBody, err := ioutil.ReadAll(sgReq.Body)
if err != nil {
log.Error(err)
rw.WriteHeader(http.StatusBadGateway)
return
}
err = json.Unmarshal(respBody, &readResp)
if err != nil {
log.Error(err)
rw.WriteHeader(http.StatusBadGateway)
return
}
log.Debugf("Response: %v", readResp)
if len(readResp.Results.Entities) == 0 {
rw.WriteHeader(http.StatusNotFound)
return
}
jsonResp, err := json.Marshal(readResp.Results.Entities[0])
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusOK)
rw.Write(jsonResp)
}
}