Skip to content

Commit 4b880de

Browse files
Improve node client example
The existing example didn't show how to set the `x-access-token` header, so it was not really usable. See also cs3org/cs3apis#140.
1 parent 2df0412 commit 4b880de

File tree

1 file changed

+57
-24
lines changed

1 file changed

+57
-24
lines changed

README.md

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,73 @@ npm i @cs3org/node-cs3apis
1212

1313
## Example usage
1414
```js
15+
const util = require('util');
1516
const grpc = require('grpc');
1617
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';
1825

1926
const TARGET = process.env.TARGET || 'localhost:19000';
2027

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();
2243

23-
function authenticate(authType, clientId, clientSecret) {
44+
async function authenticate() {
2445
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());
3776
}
3877

3978
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();
4982
}
5083

5184
// ...

0 commit comments

Comments
 (0)