-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpUtils.go
More file actions
43 lines (38 loc) · 1.17 KB
/
HttpUtils.go
File metadata and controls
43 lines (38 loc) · 1.17 KB
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
package main
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
)
/** GetNotificationDocument
Checks that the incoming request to a notification endpoint is of the type we expect, stream in the content and parse
it into a VSNotificationDocument
*/
func GetNotificationDocument(w http.ResponseWriter, req *http.Request) (*VSNotificationDocument, *[]byte) {
if req.Method != "POST" {
w.Header().Add("Content-Type", "application/json")
resp := GenericResponse{Status: "error", Detail: "Expected a POST"}
responseBytes, _ := json.Marshal(resp)
w.WriteHeader(405)
io.Copy(w, bytes.NewReader(responseBytes))
return nil, nil
}
bodyContent, readErr := ioutil.ReadAll(req.Body)
if readErr != nil {
log.Print("ERROR VidispineMessageHandler could not read content sent by server: ", readErr)
w.WriteHeader(400)
return nil, nil
}
var notification VSNotificationDocument
parseErr := json.Unmarshal(bodyContent, ¬ification)
if parseErr != nil {
log.Printf("ERROR Could not parse content from server (expecting JSON). Offending content was: ")
log.Printf(string(bodyContent))
w.WriteHeader(400)
return nil, nil
}
return ¬ification, &bodyContent
}