1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00
go-micro/client/grpc/response.go

45 lines
815 B
Go
Raw Normal View History

2019-06-08 20:40:44 +02:00
package grpc
import (
"strings"
"github.com/micro/go-micro/codec"
2019-06-18 19:51:52 +02:00
"github.com/micro/go-micro/codec/bytes"
2019-06-08 20:40:44 +02:00
"google.golang.org/grpc"
2019-06-11 10:52:35 +02:00
"google.golang.org/grpc/encoding"
2019-06-08 20:40:44 +02:00
)
type response struct {
2019-06-11 10:52:35 +02:00
conn *grpc.ClientConn
2019-06-08 20:40:44 +02:00
stream grpc.ClientStream
2019-06-11 10:52:35 +02:00
codec encoding.Codec
2019-06-18 19:51:52 +02:00
gcodec codec.Codec
2019-06-08 20:40:44 +02:00
}
// Read the response
func (r *response) Codec() codec.Reader {
2019-06-18 19:51:52 +02:00
return r.gcodec
2019-06-08 20:40:44 +02:00
}
// read the header
func (r *response) Header() map[string]string {
md, err := r.stream.Header()
if err != nil {
return map[string]string{}
}
hdr := make(map[string]string)
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 19:51:52 +02:00
f := &bytes.Frame{}
if err := r.gcodec.ReadBody(f); err != nil {
return nil, err
}
return f.Data, nil
2019-06-08 20:40:44 +02:00
}