micro-client-grpc/response.go

43 lines
765 B
Go
Raw Permalink Normal View History

2019-06-08 21:40:44 +03:00
package grpc
import (
"strings"
"go.unistack.org/micro/v3/codec"
"go.unistack.org/micro/v3/metadata"
2019-06-08 21:40:44 +03:00
"google.golang.org/grpc"
)
type response struct {
conn *poolConn
2019-06-08 21:40:44 +03:00
stream grpc.ClientStream
codec codec.Codec
2019-06-08 21:40:44 +03:00
}
// Read the response
func (r *response) Codec() codec.Codec {
return r.codec
2019-06-08 21:40:44 +03:00
}
// read the header
func (r *response) Header() metadata.Metadata {
meta, err := r.stream.Header()
2019-06-08 21:40:44 +03:00
if err != nil {
return metadata.New(0)
2019-06-08 21:40:44 +03:00
}
md := metadata.New(len(meta))
for k, v := range meta {
md.Set(k, strings.Join(v, ","))
2019-06-08 21:40:44 +03:00
}
return md
2019-06-08 21:40:44 +03:00
}
// Read the undecoded response
func (r *response) Read() ([]byte, error) {
f := &codec.Frame{}
if err := r.codec.ReadBody(&wrapStream{r.stream}, f); err != nil {
2019-06-18 20:51:52 +03:00
return nil, err
}
return f.Data, nil
2019-06-08 21:40:44 +03:00
}