2019-06-03 18:44:43 +01:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-10-27 00:57:12 +03:00
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type grpcRequest struct {
|
2021-04-26 19:17:29 +03:00
|
|
|
request interface{}
|
|
|
|
codec codec.Codec
|
2019-06-03 18:44:43 +01:00
|
|
|
service string
|
|
|
|
method string
|
|
|
|
contentType string
|
|
|
|
opts client.RequestOptions
|
|
|
|
}
|
|
|
|
|
2019-06-18 18:51:52 +01:00
|
|
|
// service Struct.Method /service.Struct/Method
|
|
|
|
func methodToGRPC(service, method string) string {
|
2019-06-03 18:44:43 +01:00
|
|
|
// no method or already grpc method
|
|
|
|
if len(method) == 0 || method[0] == '/' {
|
|
|
|
return method
|
|
|
|
}
|
2019-06-18 18:51:52 +01:00
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
// assume method is Foo.Bar
|
|
|
|
mParts := strings.Split(method, ".")
|
|
|
|
if len(mParts) != 2 {
|
|
|
|
return method
|
|
|
|
}
|
2019-06-18 18:51:52 +01:00
|
|
|
|
2019-06-19 12:34:45 +01:00
|
|
|
if len(service) == 0 {
|
|
|
|
return fmt.Sprintf("/%s/%s", mParts[0], mParts[1])
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
// return /pkg.Foo/Bar
|
2019-06-18 18:51:52 +01:00
|
|
|
return fmt.Sprintf("/%s.%s/%s", service, mParts[0], mParts[1])
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
2022-11-16 23:10:27 +03:00
|
|
|
func newGRPCRequest(service, method string, request interface{}, contentType string, opts ...client.RequestOption) client.Request {
|
|
|
|
options := client.NewRequestOptions(opts...)
|
2019-06-03 18:44:43 +01:00
|
|
|
|
|
|
|
// set the content-type specified
|
2022-11-16 23:10:27 +03:00
|
|
|
if len(options.ContentType) > 0 {
|
|
|
|
contentType = options.ContentType
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &grpcRequest{
|
|
|
|
service: service,
|
|
|
|
method: method,
|
|
|
|
request: request,
|
|
|
|
contentType: contentType,
|
2022-11-16 23:10:27 +03:00
|
|
|
opts: options,
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-26 01:18:35 +03:00
|
|
|
func (g *grpcRequest) Codec() codec.Codec {
|
2019-06-08 19:40:44 +01:00
|
|
|
return g.codec
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcRequest) Body() interface{} {
|
|
|
|
return g.request
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcRequest) Stream() bool {
|
|
|
|
return g.opts.Stream
|
|
|
|
}
|