github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/client/grpc/request.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-micro/v3/client/grpc/request.go 16 17 package grpc 18 19 import ( 20 "fmt" 21 "strings" 22 23 "github.com/tickoalcantara12/micro/v3/service/client" 24 "github.com/tickoalcantara12/micro/v3/util/codec" 25 ) 26 27 type grpcRequest struct { 28 service string 29 method string 30 contentType string 31 request interface{} 32 opts client.RequestOptions 33 codec codec.Codec 34 } 35 36 // service Struct.Method /service.Struct/Method 37 func methodToGRPC(service, method string) string { 38 // no method or already grpc method 39 if len(method) == 0 || method[0] == '/' { 40 return method 41 } 42 43 // assume method is Foo.Bar 44 mParts := strings.Split(method, ".") 45 if len(mParts) != 2 { 46 return method 47 } 48 49 if len(service) == 0 { 50 return fmt.Sprintf("/%s/%s", mParts[0], mParts[1]) 51 } 52 53 // return /pkg.Foo/Bar 54 return fmt.Sprintf("/%s.%s/%s", service, mParts[0], mParts[1]) 55 } 56 57 func newGRPCRequest(service, method string, request interface{}, contentType string, reqOpts ...client.RequestOption) client.Request { 58 var opts client.RequestOptions 59 for _, o := range reqOpts { 60 o(&opts) 61 } 62 63 // set the content-type specified 64 if len(opts.ContentType) > 0 { 65 contentType = opts.ContentType 66 } 67 68 return &grpcRequest{ 69 service: service, 70 method: method, 71 request: request, 72 contentType: contentType, 73 opts: opts, 74 } 75 } 76 77 func (g *grpcRequest) ContentType() string { 78 return g.contentType 79 } 80 81 func (g *grpcRequest) Service() string { 82 return g.service 83 } 84 85 func (g *grpcRequest) Method() string { 86 return g.method 87 } 88 89 func (g *grpcRequest) Endpoint() string { 90 return g.method 91 } 92 93 func (g *grpcRequest) Codec() codec.Writer { 94 return g.codec 95 } 96 97 func (g *grpcRequest) Body() interface{} { 98 return g.request 99 } 100 101 func (g *grpcRequest) Stream() bool { 102 return g.opts.Stream 103 }