forked from pivotal-cf/brokerapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_handler.go
63 lines (50 loc) · 1.53 KB
/
api_handler.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
package handlers
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"code.cloudfoundry.org/lager"
"github.com/pivotal-cf/brokerapi/domain"
)
const (
invalidServiceDetailsErrorKey = "invalid-service-details"
instanceIDLogKey = "instance-id"
serviceIdMissingKey = "service-id-missing"
planIdMissingKey = "plan-id-missing"
unknownErrorKey = "unknown-error"
bindingIDLogKey = "binding-id"
)
var (
serviceIdError = errors.New("service_id missing")
planIdError = errors.New("plan_id missing")
invalidServiceIDError = errors.New("service-id not in the catalog")
invalidPlanIDError = errors.New("plan-id not in the catalog")
)
type APIHandler struct {
serviceBroker domain.ServiceBroker
logger lager.Logger
}
func NewApiHandler(broker domain.ServiceBroker, logger lager.Logger) APIHandler {
return APIHandler{broker, logger}
}
func (h APIHandler) respond(w http.ResponseWriter, status int, response interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
encoder := json.NewEncoder(w)
encoder.SetEscapeHTML(false)
err := encoder.Encode(response)
if err != nil {
h.logger.Error("encoding response", err, lager.Data{"status": status, "response": response})
}
}
type brokerVersion struct {
Major int
Minor int
}
func getAPIVersion(req *http.Request) brokerVersion {
var version brokerVersion
apiVersion := req.Header.Get("X-Broker-API-Version")
fmt.Sscanf(apiVersion, "%d.%d", &version.Major, &version.Minor)
return version
}