1
- // Licensed to the Apache Software Foundation (ASF) under one
2
- // or more contributor license agreements. See the NOTICE file
3
- // distributed with this work for additional information
4
- // regarding copyright ownership. The ASF licenses this file
5
- // to you under the Apache License, Version 2.0 (the
6
- // "License"); you may not use this file except in compliance
7
- // with the License. You may obtain a copy of the License at
1
+ // Copyright 2022 The casbin-neo Authors. All Rights Reserved.
8
2
//
9
- // http://www.apache.org/licenses/LICENSE-2.0
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
10
6
//
11
- // Unless required by applicable law or agreed to in writing,
12
- // software distributed under the License is distributed on an
13
- // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- // KIND, either express or implied. See the License for the
15
- // specific language governing permissions and limitations
16
- // under the License.
7
+ // http://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.
17
14
18
- package main
15
+ package client
19
16
20
17
import (
21
18
"context"
@@ -29,23 +26,23 @@ import (
29
26
"time"
30
27
)
31
28
32
- type client struct {
29
+ type Client struct {
33
30
grpcClient command.CasbinMeshClient
34
31
}
35
32
36
33
var (
37
34
MarshalFailed = errors .New ("marshal failed" )
38
35
)
39
36
40
- func (c client ) ShowStats (ctx context.Context ) ([]byte , error ) {
37
+ func (c Client ) ShowStats (ctx context.Context ) ([]byte , error ) {
41
38
resp , err := c .grpcClient .ShowStats (ctx , & command.StatsRequest {})
42
39
if err != nil {
43
40
return nil , err
44
41
}
45
42
return resp .Payload , nil
46
43
}
47
44
48
- func (c client ) AddPolicies (ctx context.Context , namespace , sec , ptype string , rules [][]string ) ([][]string , error ) {
45
+ func (c Client ) AddPolicies (ctx context.Context , namespace , sec , ptype string , rules [][]string ) ([][]string , error ) {
49
46
payload := command.AddPoliciesPayload {
50
47
Sec : sec ,
51
48
PType : ptype ,
@@ -70,7 +67,7 @@ func (c client) AddPolicies(ctx context.Context, namespace, sec, ptype string, r
70
67
return command .ToStringArray (resp .EffectedRules ), nil
71
68
}
72
69
73
- func (c client ) RemovePolicies (ctx context.Context , namespace , sec , ptype string , rules [][]string ) ([][]string , error ) {
70
+ func (c Client ) RemovePolicies (ctx context.Context , namespace , sec , ptype string , rules [][]string ) ([][]string , error ) {
74
71
payload := command.RemovePoliciesPayload {
75
72
Sec : sec ,
76
73
PType : ptype ,
@@ -95,7 +92,7 @@ func (c client) RemovePolicies(ctx context.Context, namespace, sec, ptype string
95
92
return command .ToStringArray (resp .EffectedRules ), nil
96
93
}
97
94
98
- func (c client ) UpdatePolicies (ctx context.Context , namespace , sec , ptype string , old , new [][]string ) (bool , error ) {
95
+ func (c Client ) UpdatePolicies (ctx context.Context , namespace , sec , ptype string , old , new [][]string ) (bool , error ) {
99
96
log .Printf ("sec:%s,ptype:%s,or%v,nr:%v" , sec , ptype , old , new )
100
97
payload := command.UpdatePoliciesPayload {
101
98
Sec : sec ,
@@ -122,7 +119,7 @@ func (c client) UpdatePolicies(ctx context.Context, namespace, sec, ptype string
122
119
return resp .Effected , nil
123
120
}
124
121
125
- func (c client ) ListNamespaces (ctx context.Context ) ([]string , error ) {
122
+ func (c Client ) ListNamespaces (ctx context.Context ) ([]string , error ) {
126
123
resp , err := c .grpcClient .ListNamespaces (ctx , & command.ListNamespacesRequest {})
127
124
if err != nil {
128
125
return nil , err
@@ -133,15 +130,15 @@ func (c client) ListNamespaces(ctx context.Context) ([]string, error) {
133
130
return resp .Namespace , nil
134
131
}
135
132
136
- func (c client ) ListPolicies (ctx context.Context , namespace string ) ([][]string , error ) {
133
+ func (c Client ) ListPolicies (ctx context.Context , namespace string ) ([][]string , error ) {
137
134
resp , err := c .grpcClient .ListPolicies (ctx , & command.ListPoliciesRequest {Namespace : namespace })
138
135
if err != nil {
139
136
return nil , err
140
137
}
141
138
return command .ToStringArray (resp .Policies ), nil
142
139
}
143
140
144
- func (c client ) Enforce (ctx context.Context , namespace string , level command.EnforcePayload_Level , freshness int64 , params ... interface {}) (bool , error ) {
141
+ func (c Client ) Enforce (ctx context.Context , namespace string , level command.EnforcePayload_Level , freshness int64 , params ... interface {}) (bool , error ) {
145
142
var B [][]byte
146
143
for _ , p := range params {
147
144
b , err := json .Marshal (p )
@@ -170,7 +167,7 @@ func (c client) Enforce(ctx context.Context, namespace string, level command.Enf
170
167
return result .Ok , nil
171
168
}
172
169
173
- func (c client ) PrintModel (ctx context.Context , namespace string ) (string , error ) {
170
+ func (c Client ) PrintModel (ctx context.Context , namespace string ) (string , error ) {
174
171
resp , err := c .grpcClient .PrintModel (ctx , & command.PrintModelRequest {Namespace : namespace })
175
172
if err != nil {
176
173
return "" , err
@@ -181,31 +178,31 @@ func (c client) PrintModel(ctx context.Context, namespace string) (string, error
181
178
return resp .Model , nil
182
179
}
183
180
184
- type options struct {
185
- target string
186
- authType AuthType
187
- username string
188
- password string
181
+ type Options struct {
182
+ Target string
183
+ AuthType AuthType
184
+ Username string
185
+ Password string
189
186
}
190
187
191
- func NewClient (op options ) * client {
188
+ func NewClient (op Options ) * Client {
192
189
var opts []grpc.DialOption
193
190
ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
194
191
defer cancel ()
195
192
opts = append (opts , grpc .WithInsecure ())
196
193
opts = append (opts , grpc .WithBlock ())
197
194
198
- switch op .authType {
195
+ switch op .AuthType {
199
196
case Basic :
200
- opts = append (opts , grpc .WithUnaryInterceptor (grpc_middleware .ChainUnaryClient (BasicAuthor (op .username , op .password ))))
197
+ opts = append (opts , grpc .WithUnaryInterceptor (grpc_middleware .ChainUnaryClient (BasicAuthor (op .Username , op .Password ))))
201
198
}
202
199
203
- conn , err := grpc .DialContext (ctx , op .target , opts ... )
200
+ conn , err := grpc .DialContext (ctx , op .Target , opts ... )
204
201
if err != nil {
205
202
log .Fatalf ("fail to dial: %v" , err )
206
203
}
207
204
log .Println ("login success!" )
208
205
//defer conn.Close()
209
206
c := command .NewCasbinMeshClient (conn )
210
- return & client {grpcClient : c }
207
+ return & Client {grpcClient : c }
211
208
}
0 commit comments