Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve node client example #7

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 57 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,73 @@ npm i @cs3org/node-cs3apis

## Example usage
```js
const util = require('util');
const grpc = require('grpc');
const { GatewayAPIClient } = require('@cs3org/node-cs3apis/cs3/gateway/v1beta1/gateway_api_grpc_pb');
const { AuthenticateRequest } = require('@cs3org/node-cs3apis/cs3/gateway/v1beta1/gateway_api_pb');
const { AuthenticateRequest, WhoAmIRequest } = require('@cs3org/node-cs3apis/cs3/gateway/v1beta1/gateway_api_pb');
const { ListReceivedOCMSharesRequest } = require('@cs3org/node-cs3apis/cs3/sharing/ocm/v1beta1/ocm_api_pb');

// Specifies the name of the Reva access token used during requests.
// Align this string with the server expects, in the case of revad see:
// https://github.com/cs3org/reva/blob/v1.11.0/pkg/token/token.go#L30
const TOKEN_HEADER = 'x-access-token';

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

const client = new GatewayAPIClient(TARGET, grpc.credentials.createInsecure());
function promisifyMethods(instance, methodNames) {
const result = {};
methodNames.forEach(methodName => {
result[methodName] = util.promisify(instance[methodName].bind(instance));
});
return result;
}

const client = promisifyMethods(new GatewayAPIClient(TARGET, grpc.credentials.createInsecure()), [
'authenticate',
'listReceivedOCMShares',
'whoAmI'
]);

let metadata = new grpc.Metadata();

function authenticate(authType, clientId, clientSecret) {
async function authenticate() {
const req = new AuthenticateRequest();
req.setType(authType);
req.setClientId(clientId);
req.setClientSecret(clientSecret);
return new Promise((resolve, reject) => {
client.authenticate(req, (err, response) => {
if (err) {
reject(err);
} else {
resolve(response);
}
});
});
req.setType('basic');
req.setClientId('einstein');
req.setClientSecret('relativity');
const res = await client.authenticate(req);

// See AuthenticateResponse https://github.com/cs3org/cs3apis/blob/a86e5cb6ac360/cs3/gateway/v1beta1/gateway_api.proto#L415
const user = res.getUser();
// * User https://github.com/cs3org/cs3apis/blob/a86e5cb6ac360/cs3/identity/user/v1beta1/resources.proto#L53
const displayName = user.getDisplayName();
console.log('DisplayName from AuthenticateResponse:', displayName);

// add the token to the metadata for subsequent client calls
const token = res.getToken();
metadata.add(TOKEN_HEADER, token);
// one exception is the 'WhoAmI' method, which takes the token as a request parameter
return token;
}

async function whoAmI(token) {
const req = new WhoAmIRequest();
req.setToken(token);
const res = await client.whoAmI(req /* , metadata */);
console.log('DisplayName from WhoAmIResponse:', res.getUser().getDisplayName());
}

async function listReceivedOCMShares() {
const req = new ListReceivedOCMSharesRequest();
// req.setToken(token);
const shares = await client.listReceivedOCMShares(req, metadata);
console.log('SharesList from ListReceivedOCMSharesResponse:', shares.getSharesList());
}

async function example() {
try {
const res = await authenticate('basic', 'einstein', 'relativity');
// See:
// * AuthenticateResponse https://github.com/cs3org/cs3apis/blob/a86e5cb6ac360/cs3/gateway/v1beta1/gateway_api.proto#L415
// * User https://github.com/cs3org/cs3apis/blob/a86e5cb6ac360/cs3/identity/user/v1beta1/resources.proto#L53
console.log(res.getUser().getDisplayName());
} catch (e) {
console.error(e);
}
const token = await authenticate();
await whoAmI(token);
await listReceivedOCMShares();
}

// ...
Expand Down
96 changes: 96 additions & 0 deletions cs3/admin/group/v1beta1/group_api_grpc_pb.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// package: cs3.admin.group.v1beta1
// file: cs3/admin/group/v1beta1/group_api.proto

import * as grpc from '@grpc/grpc-js';
import * as cs3_admin_group_v1beta1_group_api_pb from '../../../../cs3/admin/group/v1beta1/group_api_pb';
import * as cs3_identity_group_v1beta1_resources_pb from '../../../../cs3/identity/group/v1beta1/resources_pb';
import * as cs3_identity_user_v1beta1_resources_pb from '../../../../cs3/identity/user/v1beta1/resources_pb';
import * as cs3_rpc_v1beta1_status_pb from '../../../../cs3/rpc/v1beta1/status_pb';
import * as cs3_types_v1beta1_types_pb from '../../../../cs3/types/v1beta1/types_pb';

interface IGroupAPIService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {
createGroup: IGroupAPIService_ICreateGroup;
deleteGroup: IGroupAPIService_IDeleteGroup;
addUserToGroup: IGroupAPIService_IAddUserToGroup;
removeUserFromGroup: IGroupAPIService_IRemoveUserFromGroup;
}

interface IGroupAPIService_ICreateGroup extends grpc.MethodDefinition<cs3_admin_group_v1beta1_group_api_pb.CreateGroupRequest, cs3_admin_group_v1beta1_group_api_pb.CreateGroupResponse> {
path: '/cs3.admin.group.v1beta1.GroupAPI/CreateGroup'
requestStream: false
responseStream: false
requestSerialize: grpc.serialize<cs3_admin_group_v1beta1_group_api_pb.CreateGroupRequest>;
requestDeserialize: grpc.deserialize<cs3_admin_group_v1beta1_group_api_pb.CreateGroupRequest>;
responseSerialize: grpc.serialize<cs3_admin_group_v1beta1_group_api_pb.CreateGroupResponse>;
responseDeserialize: grpc.deserialize<cs3_admin_group_v1beta1_group_api_pb.CreateGroupResponse>;
}

interface IGroupAPIService_IDeleteGroup extends grpc.MethodDefinition<cs3_admin_group_v1beta1_group_api_pb.DeleteGroupRequest, cs3_admin_group_v1beta1_group_api_pb.DeleteGroupResponse> {
path: '/cs3.admin.group.v1beta1.GroupAPI/DeleteGroup'
requestStream: false
responseStream: false
requestSerialize: grpc.serialize<cs3_admin_group_v1beta1_group_api_pb.DeleteGroupRequest>;
requestDeserialize: grpc.deserialize<cs3_admin_group_v1beta1_group_api_pb.DeleteGroupRequest>;
responseSerialize: grpc.serialize<cs3_admin_group_v1beta1_group_api_pb.DeleteGroupResponse>;
responseDeserialize: grpc.deserialize<cs3_admin_group_v1beta1_group_api_pb.DeleteGroupResponse>;
}

interface IGroupAPIService_IAddUserToGroup extends grpc.MethodDefinition<cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupRequest, cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupResponse> {
path: '/cs3.admin.group.v1beta1.GroupAPI/AddUserToGroup'
requestStream: false
responseStream: false
requestSerialize: grpc.serialize<cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupRequest>;
requestDeserialize: grpc.deserialize<cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupRequest>;
responseSerialize: grpc.serialize<cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupResponse>;
responseDeserialize: grpc.deserialize<cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupResponse>;
}

interface IGroupAPIService_IRemoveUserFromGroup extends grpc.MethodDefinition<cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupRequest, cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupResponse> {
path: '/cs3.admin.group.v1beta1.GroupAPI/RemoveUserFromGroup'
requestStream: false
responseStream: false
requestSerialize: grpc.serialize<cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupRequest>;
requestDeserialize: grpc.deserialize<cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupRequest>;
responseSerialize: grpc.serialize<cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupResponse>;
responseDeserialize: grpc.deserialize<cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupResponse>;
}

export const GroupAPIService: IGroupAPIService;
export interface IGroupAPIServer extends grpc.UntypedServiceImplementation {
createGroup: grpc.handleUnaryCall<cs3_admin_group_v1beta1_group_api_pb.CreateGroupRequest, cs3_admin_group_v1beta1_group_api_pb.CreateGroupResponse>;
deleteGroup: grpc.handleUnaryCall<cs3_admin_group_v1beta1_group_api_pb.DeleteGroupRequest, cs3_admin_group_v1beta1_group_api_pb.DeleteGroupResponse>;
addUserToGroup: grpc.handleUnaryCall<cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupRequest, cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupResponse>;
removeUserFromGroup: grpc.handleUnaryCall<cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupRequest, cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupResponse>;
}

export interface IGroupAPIClient {
createGroup(request: cs3_admin_group_v1beta1_group_api_pb.CreateGroupRequest, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.CreateGroupResponse) => void): grpc.ClientUnaryCall;
createGroup(request: cs3_admin_group_v1beta1_group_api_pb.CreateGroupRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.CreateGroupResponse) => void): grpc.ClientUnaryCall;
createGroup(request: cs3_admin_group_v1beta1_group_api_pb.CreateGroupRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.CreateGroupResponse) => void): grpc.ClientUnaryCall;
deleteGroup(request: cs3_admin_group_v1beta1_group_api_pb.DeleteGroupRequest, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.DeleteGroupResponse) => void): grpc.ClientUnaryCall;
deleteGroup(request: cs3_admin_group_v1beta1_group_api_pb.DeleteGroupRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.DeleteGroupResponse) => void): grpc.ClientUnaryCall;
deleteGroup(request: cs3_admin_group_v1beta1_group_api_pb.DeleteGroupRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.DeleteGroupResponse) => void): grpc.ClientUnaryCall;
addUserToGroup(request: cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupRequest, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupResponse) => void): grpc.ClientUnaryCall;
addUserToGroup(request: cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupResponse) => void): grpc.ClientUnaryCall;
addUserToGroup(request: cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupResponse) => void): grpc.ClientUnaryCall;
removeUserFromGroup(request: cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupRequest, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupResponse) => void): grpc.ClientUnaryCall;
removeUserFromGroup(request: cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupResponse) => void): grpc.ClientUnaryCall;
removeUserFromGroup(request: cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupResponse) => void): grpc.ClientUnaryCall;
}

export class GroupAPIClient extends grpc.Client implements IGroupAPIClient {
constructor(address: string, credentials: grpc.ChannelCredentials, options?: Partial<grpc.ClientOptions>);
public createGroup(request: cs3_admin_group_v1beta1_group_api_pb.CreateGroupRequest, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.CreateGroupResponse) => void): grpc.ClientUnaryCall;
public createGroup(request: cs3_admin_group_v1beta1_group_api_pb.CreateGroupRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.CreateGroupResponse) => void): grpc.ClientUnaryCall;
public createGroup(request: cs3_admin_group_v1beta1_group_api_pb.CreateGroupRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.CreateGroupResponse) => void): grpc.ClientUnaryCall;
public deleteGroup(request: cs3_admin_group_v1beta1_group_api_pb.DeleteGroupRequest, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.DeleteGroupResponse) => void): grpc.ClientUnaryCall;
public deleteGroup(request: cs3_admin_group_v1beta1_group_api_pb.DeleteGroupRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.DeleteGroupResponse) => void): grpc.ClientUnaryCall;
public deleteGroup(request: cs3_admin_group_v1beta1_group_api_pb.DeleteGroupRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.DeleteGroupResponse) => void): grpc.ClientUnaryCall;
public addUserToGroup(request: cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupRequest, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupResponse) => void): grpc.ClientUnaryCall;
public addUserToGroup(request: cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupResponse) => void): grpc.ClientUnaryCall;
public addUserToGroup(request: cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.AddUserToGroupResponse) => void): grpc.ClientUnaryCall;
public removeUserFromGroup(request: cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupRequest, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupResponse) => void): grpc.ClientUnaryCall;
public removeUserFromGroup(request: cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupResponse) => void): grpc.ClientUnaryCall;
public removeUserFromGroup(request: cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: cs3_admin_group_v1beta1_group_api_pb.RemoveUserFromGroupResponse) => void): grpc.ClientUnaryCall;
}

2 changes: 1 addition & 1 deletion cs3/admin/group/v1beta1/group_api_grpc_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// or submit itself to any jurisdiction.
//
'use strict';
var grpc = require('grpc');
var grpc = require('@grpc/grpc-js');
var cs3_admin_group_v1beta1_group_api_pb = require('../../../../cs3/admin/group/v1beta1/group_api_pb.js');
var cs3_identity_group_v1beta1_resources_pb = require('../../../../cs3/identity/group/v1beta1/resources_pb.js');
var cs3_identity_user_v1beta1_resources_pb = require('../../../../cs3/identity/user/v1beta1/resources_pb.js');
Expand Down
Loading