@@ -12,40 +12,73 @@ npm i @cs3org/node-cs3apis
12
12
13
13
## Example usage
14
14
``` js
15
+ const util = require (' util' );
15
16
const grpc = require (' grpc' );
16
17
const { GatewayAPIClient } = require (' @cs3org/node-cs3apis/cs3/gateway/v1beta1/gateway_api_grpc_pb' );
17
- const { AuthenticateRequest } = require (' @cs3org/node-cs3apis/cs3/gateway/v1beta1/gateway_api_pb' );
18
+ const { AuthenticateRequest , WhoAmIRequest } = require (' @cs3org/node-cs3apis/cs3/gateway/v1beta1/gateway_api_pb' );
19
+ const { ListReceivedOCMSharesRequest } = require (' @cs3org/node-cs3apis/cs3/sharing/ocm/v1beta1/ocm_api_pb' );
20
+
21
+ // Specifies the name of the Reva access token used during requests.
22
+ // Align this string with the server expects, in the case of revad see:
23
+ // https://github.com/cs3org/reva/blob/v1.11.0/pkg/token/token.go#L30
24
+ const TOKEN_HEADER = ' x-access-token' ;
18
25
19
26
const TARGET = process .env .TARGET || ' localhost:19000' ;
20
27
21
- const client = new GatewayAPIClient (TARGET , grpc .credentials .createInsecure ());
28
+ function promisifyMethods (instance , methodNames ) {
29
+ const result = {};
30
+ methodNames .forEach (methodName => {
31
+ result[methodName] = util .promisify (instance[methodName].bind (instance));
32
+ });
33
+ return result;
34
+ }
35
+
36
+ const client = promisifyMethods (new GatewayAPIClient (TARGET , grpc .credentials .createInsecure ()), [
37
+ ' authenticate' ,
38
+ ' listReceivedOCMShares' ,
39
+ ' whoAmI'
40
+ ]);
41
+
42
+ let metadata = new grpc.Metadata ();
22
43
23
- function authenticate (authType , clientId , clientSecret ) {
44
+ async function authenticate () {
24
45
const req = new AuthenticateRequest ();
25
- req .setType (authType);
26
- req .setClientId (clientId);
27
- req .setClientSecret (clientSecret);
28
- return new Promise ((resolve , reject ) => {
29
- client .authenticate (req, (err , response ) => {
30
- if (err) {
31
- reject (err);
32
- } else {
33
- resolve (response);
34
- }
35
- });
36
- });
46
+ req .setType (' basic' );
47
+ req .setClientId (' einstein' );
48
+ req .setClientSecret (' relativity' );
49
+ const res = await client .authenticate (req);
50
+
51
+ // See AuthenticateResponse https://github.com/cs3org/cs3apis/blob/a86e5cb6ac360/cs3/gateway/v1beta1/gateway_api.proto#L415
52
+ const user = res .getUser ();
53
+ // * User https://github.com/cs3org/cs3apis/blob/a86e5cb6ac360/cs3/identity/user/v1beta1/resources.proto#L53
54
+ const displayName = user .getDisplayName ();
55
+ console .log (' DisplayName from AuthenticateResponse:' , displayName);
56
+
57
+ // add the token to the metadata for subsequent client calls
58
+ const token = res .getToken ();
59
+ metadata .add (TOKEN_HEADER , token);
60
+ // one exception is the 'WhoAmI' method, which takes the token as a request parameter
61
+ return token;
62
+ }
63
+
64
+ async function whoAmI (token ) {
65
+ const req = new WhoAmIRequest ();
66
+ req .setToken (token);
67
+ const res = await client .whoAmI (req /* , metadata */ );
68
+ console .log (' DisplayName from WhoAmIResponse:' , res .getUser ().getDisplayName ());
69
+ }
70
+
71
+ async function listReceivedOCMShares () {
72
+ const req = new ListReceivedOCMSharesRequest ();
73
+ // req.setToken(token);
74
+ const shares = await client .listReceivedOCMShares (req, metadata);
75
+ console .log (' SharesList from ListReceivedOCMSharesResponse:' , shares .getSharesList ());
37
76
}
38
77
39
78
async function example () {
40
- try {
41
- const res = await authenticate (' basic' , ' einstein' , ' relativity' );
42
- // See:
43
- // * AuthenticateResponse https://github.com/cs3org/cs3apis/blob/a86e5cb6ac360/cs3/gateway/v1beta1/gateway_api.proto#L415
44
- // * User https://github.com/cs3org/cs3apis/blob/a86e5cb6ac360/cs3/identity/user/v1beta1/resources.proto#L53
45
- console .log (res .getUser ().getDisplayName ());
46
- } catch (e) {
47
- console .error (e);
48
- }
79
+ const token = await authenticate ();
80
+ await whoAmI (token);
81
+ await listReceivedOCMShares ();
49
82
}
50
83
51
84
// ...
0 commit comments