2019-06-08 21:40:44 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
"github.com/unistack-org/micro/v3/codec/bytes"
|
2019-06-08 21:40:44 +03:00
|
|
|
"google.golang.org/grpc"
|
2019-06-11 11:52:35 +03:00
|
|
|
"google.golang.org/grpc/encoding"
|
2019-06-08 21:40:44 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type response struct {
|
2019-06-11 11:52:35 +03:00
|
|
|
conn *grpc.ClientConn
|
2019-06-08 21:40:44 +03:00
|
|
|
stream grpc.ClientStream
|
2019-06-11 11:52:35 +03:00
|
|
|
codec encoding.Codec
|
2019-06-18 20:51:52 +03:00
|
|
|
gcodec codec.Codec
|
2019-06-08 21:40:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the response
|
|
|
|
func (r *response) Codec() codec.Reader {
|
2019-06-18 20:51:52 +03:00
|
|
|
return r.gcodec
|
2019-06-08 21:40:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// read the header
|
|
|
|
func (r *response) Header() map[string]string {
|
|
|
|
md, err := r.stream.Header()
|
|
|
|
if err != nil {
|
|
|
|
return map[string]string{}
|
|
|
|
}
|
2020-02-24 17:15:20 +03:00
|
|
|
hdr := make(map[string]string, len(md))
|
2019-06-08 21:40:44 +03:00
|
|
|
for k, v := range md {
|
|
|
|
hdr[k] = strings.Join(v, ",")
|
|
|
|
}
|
|
|
|
return hdr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the undecoded response
|
|
|
|
func (r *response) Read() ([]byte, error) {
|
2019-06-18 20:51:52 +03:00
|
|
|
f := &bytes.Frame{}
|
|
|
|
if err := r.gcodec.ReadBody(f); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f.Data, nil
|
2019-06-08 21:40:44 +03:00
|
|
|
}
|