-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_adapter.go
40 lines (35 loc) · 1.04 KB
/
response_adapter.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
package celerity
import (
"encoding/json"
)
// ResponseAdapter - Response adapters are used to marshal an endpoint response
// into bytes
type ResponseAdapter interface {
Process(Context, Response) ([]byte, error)
}
// JSONResponseAdapter - Processes an endpoint response into JSON
type JSONResponseAdapter struct{}
//JSONResponse - used by the JSONResponseAdapter to build the structure of the
//default JSON response.
type JSONResponse struct {
RequestID string `json:"requestId"`
Success bool `json:"success"`
Error string `json:"error"`
Data interface{} `json:"data"`
Meta map[string]interface{} `json:"meta"`
}
//Process - Process the response into JSON data.
func (ra *JSONResponseAdapter) Process(c Context, r Response) ([]byte, error) {
rObj := JSONResponse{
RequestID: c.RequestID,
Meta: r.Meta,
Data: r.Data,
}
if r.Error != nil {
rObj.Success = false
rObj.Error = r.Error.Error()
} else {
rObj.Success = true
}
return json.Marshal(rObj)
}