-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
invoke_method_response.go
137 lines (113 loc) · 4.26 KB
/
invoke_method_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------
package v1
import (
"errors"
commonv1pb "github.com/dapr/dapr/pkg/proto/common/v1"
internalv1pb "github.com/dapr/dapr/pkg/proto/internals/v1"
any "github.com/golang/protobuf/ptypes/any"
"github.com/valyala/fasthttp"
"google.golang.org/grpc/metadata"
)
// InvokeMethodResponse holds InternalInvokeResponse protobuf message
// and provides the helpers to manage it.
type InvokeMethodResponse struct {
r *internalv1pb.InternalInvokeResponse
}
// NewInvokeMethodResponse returns new InvokeMethodResponse object with status
func NewInvokeMethodResponse(statusCode int32, statusMessage string, statusDetails []*any.Any) *InvokeMethodResponse {
return &InvokeMethodResponse{
r: &internalv1pb.InternalInvokeResponse{
Status: &internalv1pb.Status{Code: statusCode, Message: statusMessage, Details: statusDetails},
Message: &commonv1pb.InvokeResponse{},
},
}
}
// InternalInvokeResponse returns InvokeMethodResponse for InternalInvokeResponse pb to use the helpers
func InternalInvokeResponse(resp *internalv1pb.InternalInvokeResponse) (*InvokeMethodResponse, error) {
rsp := &InvokeMethodResponse{r: resp}
if resp.Message == nil {
return nil, errors.New("Message field is nil")
}
return rsp, nil
}
// WithMessage sets InvokeResponse pb object to Message field
func (imr *InvokeMethodResponse) WithMessage(pb *commonv1pb.InvokeResponse) *InvokeMethodResponse {
imr.r.Message = pb
return imr
}
// WithRawData sets Message using byte data and content type
func (imr *InvokeMethodResponse) WithRawData(data []byte, contentType string) *InvokeMethodResponse {
if contentType == "" {
contentType = JSONContentType
}
imr.r.Message.ContentType = contentType
imr.r.Message.Data = &any.Any{Value: data}
return imr
}
// WithHeaders sets gRPC response header metadata
func (imr *InvokeMethodResponse) WithHeaders(headers metadata.MD) *InvokeMethodResponse {
imr.r.Headers = MetadataToInternalMetadata(headers)
return imr
}
// WithFastHTTPHeaders populates fasthttp response header to gRPC header metadata
func (imr *InvokeMethodResponse) WithFastHTTPHeaders(header *fasthttp.ResponseHeader) *InvokeMethodResponse {
var md = DaprInternalMetadata{}
header.VisitAll(func(key []byte, value []byte) {
md[string(key)] = &internalv1pb.ListStringValue{
Values: []string{string(value)},
}
})
if len(md) > 0 {
imr.r.Headers = md
}
return imr
}
// WithTrailers sets Trailer in internal InvokeMethodResponse
func (imr *InvokeMethodResponse) WithTrailers(trailer metadata.MD) *InvokeMethodResponse {
imr.r.Trailers = MetadataToInternalMetadata(trailer)
return imr
}
// Status gets Response status
func (imr *InvokeMethodResponse) Status() *internalv1pb.Status {
return imr.r.GetStatus()
}
// IsHTTPResponse returns true if response status code is http response status
func (imr *InvokeMethodResponse) IsHTTPResponse() bool {
// gRPC status code <= 15 - https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
// HTTP status code >= 100 - https://tools.ietf.org/html/rfc2616#section-10
return imr.r.GetStatus().Code >= 100
}
// Proto clones the internal InvokeMethodResponse pb object
func (imr *InvokeMethodResponse) Proto() *internalv1pb.InternalInvokeResponse {
return imr.r
}
// Headers gets Headers metadata
func (imr *InvokeMethodResponse) Headers() DaprInternalMetadata {
return imr.r.Headers
}
// Trailers gets Trailers metadata
func (imr *InvokeMethodResponse) Trailers() DaprInternalMetadata {
return imr.r.Trailers
}
// Message returns message field in InvokeMethodResponse
func (imr *InvokeMethodResponse) Message() *commonv1pb.InvokeResponse {
return imr.r.Message
}
// RawData returns content_type and byte array body
func (imr *InvokeMethodResponse) RawData() (string, []byte) {
m := imr.r.Message
if m == nil || m.GetData() == nil {
return "", nil
}
contentType := m.GetContentType()
dataTypeURL := m.GetData().GetTypeUrl()
dataValue := m.GetData().GetValue()
// set content_type to application/json only if typeurl is unset and data is given
if contentType == "" && (dataTypeURL == "" && dataValue != nil) {
contentType = JSONContentType
}
return contentType, dataValue
}