Skip to content

Commit

Permalink
feat: Add input option for google discovery api [pg1/3] (#891)
Browse files Browse the repository at this point in the history
* feat: Add option for rest api

* fix lint

* add default rest options in client surface

* parameter change to transport

Co-authored-by: gcf-merge-on-green[bot] <60162190+gcf-merge-on-green[bot]@users.noreply.github.com>
  • Loading branch information
summer-ji-eng and gcf-merge-on-green[bot] committed Jun 17, 2021
1 parent 99e10c3 commit f94ae3f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ export class {{ service.name }}Client {
const servicePath = opts?.servicePath || opts?.apiEndpoint || staticMembers.{{ id.get("servicePath") }};
const port = opts?.port || staticMembers.{{ id.get("port") }};
const clientConfig = opts?.clientConfig ?? {};
{% if api.rest -%}
// Implicitely set 'rest' value for the apis use rest as transport (eg. googleapis-discovery apis).
if (!opts) {
opts = {fallback: 'rest'};
} else {
opts.fallback = opts.fallback ?? 'rest';
}
{% endif -%}
const fallback = opts?.fallback ?? (typeof window !== 'undefined' && typeof window?.fetch === 'function');
opts = Object.assign({servicePath, port, clientConfig, fallback}, opts);

Expand Down
9 changes: 9 additions & 0 deletions typescript/src/gapic-generator-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ yargs.describe(
'Set to true if GAPIC metadata generation is requested'
);
yargs.boolean('metadata');
yargs.describe(
'transport',
'Default transport is gRPC. Set transport=rest for an API requires HTTP transport, or Google Discovery API.'
);
yargs.describe('protoc', 'Path to protoc binary');
yargs.usage('Usage: $0 -I /path/to/googleapis');
yargs.usage(' --output_dir /path/to/output_directory');
Expand All @@ -100,6 +104,7 @@ export interface IArguments {
protoDirs?: string[];
commonProtoPath?: string;
descriptor?: string;
transport?: string;
_: string[];
$0: string;
}
Expand All @@ -115,6 +120,7 @@ const template = argv.template as string | undefined;
const gapicValidatorOut = argv.gapicValidatorOut as string | undefined;
const validation = (argv.validation as string | undefined) ?? 'true';
const metadata = argv.metadata as boolean | undefined;
const transport = argv.transport as string | undefined;
const protoc = (argv.protoc as string | undefined) ?? 'protoc';
const protoDirs: string[] = [];
if (argv.I) {
Expand Down Expand Up @@ -164,6 +170,9 @@ if (template) {
if (metadata) {
protocCommand.push('--typescript_gapic_opt="metadata"');
}
if (transport && transport === 'rest') {
protocCommand.push('--typescript_gapic_opt="transport=rest"');
}
protocCommand.push(...protoDirsArg);
protocCommand.push(...protoFiles);
protocCommand.push(`-I${commonProtoPath}`);
Expand Down
9 changes: 9 additions & 0 deletions typescript/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class Generator {
iamService?: boolean;
templates: string[];
metadata?: boolean;
rest?: boolean;

constructor() {
this.request = protos.google.protobuf.compiler.CodeGeneratorRequest.create();
Expand Down Expand Up @@ -165,6 +166,12 @@ export class Generator {
}
}

private readRest() {
if (this.paramMap['transport'] === 'rest') {
this.rest = true;
}
}

async initializeFromStdin() {
const inputBuffer = await getStdin();
this.request = protos.google.protobuf.compiler.CodeGeneratorRequest.decode(
Expand All @@ -178,6 +185,7 @@ export class Generator {
this.readPublishPackageName();
this.readMainServiceName();
this.readTemplates();
this.readRest();
}
}

Expand Down Expand Up @@ -215,6 +223,7 @@ export class Generator {
publishName: this.publishName,
mainServiceName: this.mainServiceName,
iamService: this.iamService,
rest: this.rest,
});
return api;
}
Expand Down
3 changes: 3 additions & 0 deletions typescript/src/schema/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class API {
mainServiceName: string;
uniqKeywords: string[];
packageName: string;
rest?: boolean;

static isIgnoredService(
fd: protos.google.protobuf.IFileDescriptorProto
Expand Down Expand Up @@ -84,6 +85,7 @@ export class API {
// users specify the actual package name, if not, set it to product name.
this.publishName =
options.publishName || this.naming.productName.toKebabCase();
this.rest = options.rest;

const [allResourceDatabase, resourceDatabase] = getResourceDatabase(
fileDescriptors
Expand Down Expand Up @@ -204,6 +206,7 @@ export class API {
hostname: this.hostName,
port: this.port,
services: this.services,
rest: this.rest,
});
}
}
Expand Down
1 change: 1 addition & 0 deletions typescript/src/schema/naming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface Options {
publishName?: string;
mainServiceName?: string;
iamService?: boolean;
rest?: boolean;
}

export class Naming {
Expand Down
4 changes: 4 additions & 0 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ interface AugmentMethodParameters {
allMessages: MessagesMap;
localMessages: MessagesMap;
service: ServiceDescriptorProto;
rest?: boolean;
}

function augmentMethod(
Expand Down Expand Up @@ -510,6 +511,7 @@ function augmentService(parameters: AugmentServiceParameters) {
allMessages: parameters.allMessages,
localMessages: parameters.localMessages,
service: augmentedService,
rest: parameters.options.rest,
},
method
)
Expand Down Expand Up @@ -641,6 +643,7 @@ export class Proto {
allMessages: MessagesMap = {};
localMessages: MessagesMap = {};
fileToGenerate = true;
rest?: boolean;
// TODO: need to store metadata? address?

// allResourceDatabase: resources that defined by `google.api.resource`
Expand All @@ -657,6 +660,7 @@ export class Proto {
map[`.${parameters.fd.package!}.${message.name!}`] = message;
return map;
}, {} as MessagesMap);
this.rest = parameters.options.rest;
const protopackage = parameters.fd.package;
// Allow to generate if a proto has no service and its package name is differ from its service's.
if (
Expand Down
34 changes: 34 additions & 0 deletions typescript/test/unit/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,38 @@ describe('src/schema/proto.ts', () => {
}, /rpc "google.cloud.showcase.v1beta1.Test" has google.longrunning.operation_info but is missing option google.longrunning.operation_info.metadata_type/);
});
});

describe('should add rest for Proto class', () => {
it('should be false when rest is not set', () => {
const fd = new protos.google.protobuf.FileDescriptorProto();
const proto = new Proto({
fd,
packageName: 'google.cloud.example.v1beta1',
allMessages: {},
allResourceDatabase: new ResourceDatabase(),
resourceDatabase: new ResourceDatabase(),
options: {
grpcServiceConfig: new protos.grpc.service_config.ServiceConfig(),
},
commentsMap: new CommentsMap([fd]),
});
assert.strictEqual(proto.rest, undefined);
});
it('should be true when rest is set', () => {
const fd = new protos.google.protobuf.FileDescriptorProto();
const proto = new Proto({
fd,
packageName: 'google.cloud.example.v1beta1',
allMessages: {},
allResourceDatabase: new ResourceDatabase(),
resourceDatabase: new ResourceDatabase(),
options: {
grpcServiceConfig: new protos.grpc.service_config.ServiceConfig(),
rest: true,
},
commentsMap: new CommentsMap([fd]),
});
assert.strictEqual(proto.rest, true);
});
});
});

0 comments on commit f94ae3f

Please sign in to comment.