Skip to content

Commit 0241571

Browse files
Add examples
1 parent c7666d0 commit 0241571

File tree

5 files changed

+278
-73
lines changed

5 files changed

+278
-73
lines changed

README.md

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,76 +11,5 @@ npm i @cs3org/node-cs3apis
1111
```
1212

1313
## Example usage
14-
```js
15-
const util = require('util');
16-
const grpc = require('grpc');
17-
const { GatewayAPIClient } = require('@cs3org/node-cs3apis/cs3/gateway/v1beta1/gateway_api_grpc_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';
25-
26-
const TARGET = process.env.TARGET || 'localhost:19000';
27-
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();
43-
44-
async function authenticate() {
45-
const req = new AuthenticateRequest();
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());
76-
}
77-
78-
async function example() {
79-
const token = await authenticate();
80-
await whoAmI(token);
81-
await listReceivedOCMShares();
82-
}
83-
84-
// ...
85-
example();
86-
```
14+
JavaScripters please see [./examples/example.js](./examples/example.js).
15+
TypeScripters please see [./examples/example.ts](./examples/example.ts).

examples/example.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const grpc = require('@grpc/grpc-js');
2+
const services = require('./cs3/gateway/v1beta1/gateway_api_grpc_pb');
3+
const messages = require('./cs3/gateway/v1beta1/gateway_api_pb');
4+
const { ListReceivedOCMSharesRequest } = require('./cs3/sharing/ocm/v1beta1/ocm_api_pb');
5+
6+
// Specifies the name of the Reva access token used during requests.
7+
// Align this string with the server expects, in the case of revad see:
8+
// https://github.com/cs3org/reva/blob/v1.11.0/pkg/token/token.go#L30
9+
const TOKEN_HEADER = 'x-access-token';
10+
11+
const TARGET = process.env.TARGET || 'localhost:19000';
12+
13+
14+
const client = new services.GatewayAPIClient(
15+
TARGET,
16+
grpc.credentials.createInsecure()
17+
);
18+
19+
let metadata = new grpc.Metadata();
20+
21+
async function authenticate() {
22+
const req = new messages.AuthenticateRequest();
23+
req.setType('basic');
24+
req.setClientId('einstein');
25+
req.setClientSecret('relativity');
26+
const res = await new Promise((resolve, reject) => client.authenticate(req, (err, res) => {
27+
if (err) {
28+
reject(err);
29+
}
30+
resolve(res)
31+
}));
32+
33+
// See AuthenticateResponse https://github.com/cs3org/cs3apis/blob/a86e5cb6ac360/cs3/gateway/v1beta1/gateway_api.proto#L415
34+
// * User https://github.com/cs3org/cs3apis/blob/a86e5cb6ac360/cs3/identity/user/v1beta1/resources.proto#L53
35+
const user = res.getUser();
36+
console.log('DisplayName from AuthenticateResponse:', user ? user.getDisplayName() : '?');
37+
38+
// add the token to the metadata for subsequent client calls
39+
const token = res.getToken();
40+
metadata.add(TOKEN_HEADER, token);
41+
// one exception is the 'WhoAmI' method, which takes the token as a request parameter
42+
return token;
43+
}
44+
45+
async function whoAmI(token) {
46+
const req = new messages.WhoAmIRequest();
47+
req.setToken(token);
48+
const res = await new Promise((resolve, reject) => client.whoAmI(req, (err, res) => {
49+
if (err) {
50+
reject(err);
51+
}
52+
resolve(res)
53+
}));
54+
const user = res.getUser();
55+
console.log('DisplayName from WhoAmIResponse:', user? user.getDisplayName() : '?');
56+
}
57+
58+
async function listReceivedOCMShares(token) {
59+
const req = new ListReceivedOCMSharesRequest();
60+
// req.setToken(token);
61+
const shares = await new Promise((resolve, reject) => client.listReceivedOCMShares(req, (err, res) => {
62+
if (err) {
63+
reject(err);
64+
}
65+
resolve(res)
66+
}));
67+
console.log('SharesList from ListReceivedOCMSharesResponse:', shares.getSharesList());
68+
}
69+
70+
async function example() {
71+
const token = await authenticate();
72+
await whoAmI(token);
73+
await listReceivedOCMShares(token);
74+
}
75+
76+
// ...
77+
example();

examples/example.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { Metadata, credentials, ServiceError } from '@grpc/grpc-js';
2+
import { GatewayAPIClient } from '../cs3/gateway/v1beta1/gateway_api_grpc_pb';
3+
import {
4+
AuthenticateRequest,
5+
AuthenticateResponse,
6+
WhoAmIRequest,
7+
WhoAmIResponse
8+
} from '../cs3/gateway/v1beta1/gateway_api_pb';
9+
import { ListReceivedOCMSharesRequest, ListReceivedOCMSharesResponse } from '../cs3/sharing/ocm/v1beta1/ocm_api_pb';
10+
11+
// Specifies the name of the Reva access token used during requests.
12+
// Align this string with the server expects, in the case of revad see:
13+
// https://github.com/cs3org/reva/blob/v1.11.0/pkg/token/token.go#L30
14+
const TOKEN_HEADER = 'x-access-token';
15+
16+
const TARGET = process.env.TARGET || 'localhost:19000';
17+
18+
19+
const client = new GatewayAPIClient(
20+
TARGET,
21+
credentials.createInsecure()
22+
);
23+
24+
let metadata = new Metadata();
25+
26+
async function authenticate() {
27+
const req = new AuthenticateRequest();
28+
req.setType('basic');
29+
req.setClientId('einstein');
30+
req.setClientSecret('relativity');
31+
const res: AuthenticateResponse = await new Promise((resolve, reject) => {
32+
client.authenticate(req, (err: ServiceError | null, res: AuthenticateResponse) => {
33+
if (err) {
34+
reject(err);
35+
}
36+
resolve(res)
37+
})
38+
});
39+
40+
// See AuthenticateResponse https://github.com/cs3org/cs3apis/blob/a86e5cb6ac360/cs3/gateway/v1beta1/gateway_api.proto#L415
41+
// * User https://github.com/cs3org/cs3apis/blob/a86e5cb6ac360/cs3/identity/user/v1beta1/resources.proto#L53
42+
const user = res.getUser();
43+
console.log('DisplayName from AuthenticateResponse:', user ? user.getDisplayName() : '?');
44+
45+
// add the token to the metadata for subsequent client calls
46+
const token = res.getToken();
47+
metadata.add(TOKEN_HEADER, token);
48+
// one exception is the 'WhoAmI' method, which takes the token as a request parameter
49+
return token;
50+
}
51+
52+
async function whoAmI(token: string) {
53+
const req = new WhoAmIRequest();
54+
req.setToken(token);
55+
const res: WhoAmIResponse = await new Promise((resolve, reject) => {
56+
client.whoAmI(req, (err: ServiceError | null, res: WhoAmIResponse) => {
57+
if (err) {
58+
reject(err);
59+
}
60+
resolve(res)
61+
})
62+
});
63+
const user = res.getUser();
64+
console.log('DisplayName from WhoAmIResponse:', user? user.getDisplayName() : '?');
65+
}
66+
67+
async function listReceivedOCMShares() {
68+
const req = new ListReceivedOCMSharesRequest();
69+
// req.setToken(token);
70+
const shares: ListReceivedOCMSharesResponse = await new Promise((resolve, reject) => {
71+
client.listReceivedOCMShares(req, (err: ServiceError | null, res: ListReceivedOCMSharesResponse) => {
72+
if (err) {
73+
reject(err);
74+
}
75+
resolve(res)
76+
})
77+
});
78+
console.log('SharesList from ListReceivedOCMSharesResponse:', shares.getSharesList());
79+
}
80+
81+
async function example() {
82+
const token = await authenticate();
83+
await whoAmI(token);
84+
await listReceivedOCMShares();
85+
}
86+
87+
// ...
88+
example();

0 commit comments

Comments
 (0)