-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathresponse.go
62 lines (51 loc) · 1.56 KB
/
response.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
package grpc
import (
"strings"
"github.com/gojek/fiber"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
type Response struct {
Metadata metadata.MD
Message []byte
Status status.Status
}
func (r *Response) IsSuccess() bool {
return r.StatusCode() == int(codes.OK)
}
func (r *Response) Payload() []byte {
return r.Message
}
func (r *Response) StatusCode() int {
return int(r.Status.Code())
}
// Label returns all the values associated with the given key, in the response metadata.
// If the key does not exist, an empty slice will be returned.
func (r *Response) Label(key string) []string {
return r.Metadata.Get(key)
}
// WithLabel appends the given value(s) to the key, in the response metadata.
// If the key does not already exist, a new key will be created.
// The modified response is returned.
func (r *Response) WithLabel(key string, values ...string) fiber.Response {
r.Metadata.Append(key, values...)
return r
}
// WithLabels does the same thing as WithLabel but over a collection of key-values.
func (r *Response) WithLabels(labels fiber.Labels) fiber.Response {
for _, key := range labels.Keys() {
values := labels.Label(key)
r.Metadata.Append(key, values...)
}
return r
}
func (r *Response) BackendName() string {
return strings.Join(r.Label("backend"), ",")
}
// WithBackendName sets the given backend name in the response metadata.
// The modified response is returned.
func (r *Response) WithBackendName(backendName string) fiber.Response {
r.Metadata.Set("backend", backendName)
return r
}