github.com/cloudwego/kitex@v0.9.0/client/streamclient/option.go (about) 1 /* 2 * Copyright 2023 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package streamclient 18 19 import ( 20 "context" 21 "fmt" 22 23 "github.com/cloudwego/kitex/client" 24 "github.com/cloudwego/kitex/pkg/endpoint" 25 "github.com/cloudwego/kitex/pkg/utils" 26 ) 27 28 // WithRecvMiddleware adds middleware for client to handle response. 29 // It's used for intercepting stream.RecvMsg (called by Recv or CloseAndRecv) calls 30 func WithRecvMiddleware(mw endpoint.RecvMiddleware) Option { 31 mwb := func(ctx context.Context) endpoint.RecvMiddleware { 32 return mw 33 } 34 return Option{F: func(o *client.Options, di *utils.Slice) { 35 di.Push(fmt.Sprintf("WithRecvMiddleware(%+v)", utils.GetFuncName(mw))) 36 o.Streaming.RecvMiddlewareBuilders = append(o.Streaming.RecvMiddlewareBuilders, mwb) 37 }} 38 } 39 40 // WithRecvMiddlewareBuilder adds middleware that depend on a per-client context for client to handle response 41 func WithRecvMiddlewareBuilder(mwb endpoint.RecvMiddlewareBuilder) Option { 42 return Option{F: func(o *client.Options, di *utils.Slice) { 43 di.Push(fmt.Sprintf("WithRecvMiddlewareBuilder(%+v)", utils.GetFuncName(mwb))) 44 o.Streaming.RecvMiddlewareBuilders = append(o.Streaming.RecvMiddlewareBuilders, mwb) 45 }} 46 } 47 48 // WithSendMiddleware adds middleware for client to handle request. 49 // It's used for intercepting stream.SendMsg (called by Send or SendAndClose) calls 50 func WithSendMiddleware(mw endpoint.SendMiddleware) Option { 51 mwb := func(ctx context.Context) endpoint.SendMiddleware { 52 return mw 53 } 54 return Option{F: func(o *client.Options, di *utils.Slice) { 55 di.Push(fmt.Sprintf("WithSendMiddleware(%+v)", utils.GetFuncName(mw))) 56 o.Streaming.SendMiddlewareBuilders = append(o.Streaming.SendMiddlewareBuilders, mwb) 57 }} 58 } 59 60 // WithSendMiddlewareBuilder adds middleware that depend on a per-client context for client to handle request 61 func WithSendMiddlewareBuilder(mwb endpoint.SendMiddlewareBuilder) Option { 62 return Option{F: func(o *client.Options, di *utils.Slice) { 63 di.Push(fmt.Sprintf("WithSendMiddlewareBuilder(%+v)", utils.GetFuncName(mwb))) 64 o.Streaming.SendMiddlewareBuilders = append(o.Streaming.SendMiddlewareBuilders, mwb) 65 }} 66 }