11package client
22
33import (
4+ "context"
5+
6+ "connectrpc.com/connect"
47 compress "github.com/klauspost/connect-compress/v2"
8+ v1 "github.com/metal-stack/masterdata-api/api/v1"
59 "github.com/metal-stack/masterdata-api/api/v1/apiv1connect"
610)
711
@@ -16,12 +20,12 @@ type (
1620 }
1721
1822 client struct {
19- config DialConfig
23+ config * Config
2024 }
2125)
2226
2327// GRPCClient is a Client implementation with grpc transport.
24- func New (config DialConfig ) Client {
28+ func New (config * Config ) Client {
2529 return & client {
2630 config : config ,
2731 }
@@ -42,6 +46,7 @@ func (c client) ProjectMember() apiv1connect.ProjectMemberServiceClient {
4246 c .config .HttpClient (),
4347 c .config .BaseURL ,
4448 compress .WithAll (compress .LevelBalanced ),
49+ connect .WithInterceptors (NamespaceInterceptor (c .config .Namespace )),
4550 )
4651}
4752
@@ -51,6 +56,7 @@ func (c client) Tenant() apiv1connect.TenantServiceClient {
5156 c .config .HttpClient (),
5257 c .config .BaseURL ,
5358 compress .WithAll (compress .LevelBalanced ),
59+ connect .WithInterceptors (NamespaceInterceptor (c .config .Namespace )),
5460 )
5561}
5662
@@ -60,6 +66,7 @@ func (c client) TenantMember() apiv1connect.TenantMemberServiceClient {
6066 c .config .HttpClient (),
6167 c .config .BaseURL ,
6268 compress .WithAll (compress .LevelBalanced ),
69+ connect .WithInterceptors (NamespaceInterceptor (c .config .Namespace )),
6370 )
6471}
6572
@@ -70,3 +77,41 @@ func (c client) Version() apiv1connect.VersionServiceClient {
7077 compress .WithAll (compress .LevelBalanced ),
7178 )
7279}
80+
81+ func NamespaceInterceptor (namespace string ) connect.UnaryInterceptorFunc {
82+ return func (uf connect.UnaryFunc ) connect.UnaryFunc {
83+ return func (ctx context.Context , ar connect.AnyRequest ) (connect.AnyResponse , error ) {
84+ switch r := ar .Any ().(type ) {
85+ case * v1.TenantMemberCreateRequest :
86+ if r .TenantMember .Namespace == "" {
87+ r .TenantMember .Namespace = namespace
88+ }
89+ case * v1.ProjectMemberCreateRequest :
90+ if r .ProjectMember .Namespace == "" {
91+ r .ProjectMember .Namespace = namespace
92+ }
93+ case * v1.TenantMemberFindRequest :
94+ if r .Namespace == "" {
95+ r .Namespace = namespace
96+ }
97+ case * v1.ProjectMemberFindRequest :
98+ if r .Namespace == "" {
99+ r .Namespace = namespace
100+ }
101+ case * v1.FindParticipatingProjectsRequest :
102+ if r .Namespace == "" {
103+ r .Namespace = namespace
104+ }
105+ case * v1.FindParticipatingTenantsRequest :
106+ if r .Namespace == "" {
107+ r .Namespace = namespace
108+ }
109+ case * v1.ListTenantMembersRequest :
110+ if r .Namespace == "" {
111+ r .Namespace = namespace
112+ }
113+ }
114+ return uf (ctx , ar )
115+ }
116+ }
117+ }
0 commit comments