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/request.go

88 lines
1.6 KiB
Go
Raw Normal View History

2019-06-03 19:44:43 +02:00
package grpc
import (
"fmt"
"strings"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/codec"
)
type grpcRequest struct {
service string
method string
contentType string
request interface{}
opts client.RequestOptions
2019-06-11 10:52:35 +02:00
codec codec.Codec
2019-06-03 19:44:43 +02:00
}
2019-06-18 19:51:52 +02:00
// service Struct.Method /service.Struct/Method
func methodToGRPC(service, method string) string {
2019-06-03 19:44:43 +02:00
// no method or already grpc method
if len(method) == 0 || method[0] == '/' {
return method
}
2019-06-18 19:51:52 +02:00
2019-06-03 19:44:43 +02:00
// assume method is Foo.Bar
mParts := strings.Split(method, ".")
if len(mParts) != 2 {
return method
}
2019-06-18 19:51:52 +02:00
2019-06-19 13:34:45 +02:00
if len(service) == 0 {
return fmt.Sprintf("/%s/%s", mParts[0], mParts[1])
}
2019-06-03 19:44:43 +02:00
// return /pkg.Foo/Bar
2019-06-18 19:51:52 +02:00
return fmt.Sprintf("/%s.%s/%s", service, mParts[0], mParts[1])
2019-06-03 19:44:43 +02:00
}
func newGRPCRequest(service, method string, request interface{}, contentType string, reqOpts ...client.RequestOption) client.Request {
var opts client.RequestOptions
for _, o := range reqOpts {
o(&opts)
}
// set the content-type specified
if len(opts.ContentType) > 0 {
contentType = opts.ContentType
}
return &grpcRequest{
service: service,
method: method,
request: request,
contentType: contentType,
opts: opts,
}
}
func (g *grpcRequest) ContentType() string {
return g.contentType
}
func (g *grpcRequest) Service() string {
return g.service
}
func (g *grpcRequest) Method() string {
return g.method
}
func (g *grpcRequest) Endpoint() string {
return g.method
}
func (g *grpcRequest) Codec() codec.Writer {
2019-06-08 20:40:44 +02:00
return g.codec
2019-06-03 19:44:43 +02:00
}
func (g *grpcRequest) Body() interface{} {
return g.request
}
func (g *grpcRequest) Stream() bool {
return g.opts.Stream
}