-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
44 lines (39 loc) · 1.01 KB
/
function.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
// Package p contains an HTTP Cloud Function.
package columba
import (
"columba/Consumers"
"columba/Providers"
"encoding/json"
"fmt"
"github.com/getsentry/raven-go"
"io/ioutil"
"net/http"
)
type ErrorResponse struct {
Status string `json:"status"`
Description string `json:"description"`
}
// Entry point called by Cloud Function
func Columba(w http.ResponseWriter, r *http.Request) {
var response []byte
err, _ := raven.CapturePanic(func() {
query, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
order := Consumers.ExtractOrderShopify(string(query))
rates := Providers.GetShippingRates(order)
response, _ = json.Marshal(Consumers.CreateShopifyResponse(rates))
w.Header().Add("Content-Type", "application/json")
}, nil)
if err != nil {
errorResponse := ErrorResponse{
Status: "500",
Description: fmt.Sprintf("Error: %v", err),
}
response, _ = json.Marshal(errorResponse)
_, _ = fmt.Fprint(w, string(response))
} else {
_, _ = fmt.Fprint(w, string(response))
}
}