From dfa349e7d97f125a526aa20e79fff92e571dfcf2 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Sat, 1 Jun 2019 00:53:36 +0000 Subject: [PATCH] feat: support apiEndpoint override (#713) --- package.json | 2 +- src/agent/controller.ts | 7 ++----- src/client/stackdriver/debug.ts | 17 +++++++++++++---- test/test-controller.ts | 1 + 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 9265de7e..7403842e 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "predocs-test": "npm run docs" }, "dependencies": { - "@google-cloud/common": "^1.0.0", + "@google-cloud/common": "^2.0.0", "@sindresorhus/is": "^0.17.1", "acorn": "^6.0.0", "coffeescript": "^2.0.0", diff --git a/src/agent/controller.ts b/src/agent/controller.ts index 5053f147..b0778aaa 100644 --- a/src/agent/controller.ts +++ b/src/agent/controller.ts @@ -29,9 +29,6 @@ import {Debug} from '../client/stackdriver/debug'; import {Debuggee} from '../debuggee'; import * as stackdriver from '../types/stackdriver'; -/** @const {string} Cloud Debug API endpoint */ -const API = 'https://clouddebugger.googleapis.com/v2/controller'; - export class Controller extends ServiceObject { private nextWaitToken: string | null; @@ -47,10 +44,10 @@ export class Controller extends ServiceObject { /** @private {string} */ this.nextWaitToken = null; - this.apiUrl = API; + this.apiUrl = `https://${debug.apiEndpoint}/v2/controller`; if (config && config.apiUrl) { - this.apiUrl = config.apiUrl + new URL(API).pathname; + this.apiUrl = config.apiUrl + new URL(this.apiUrl).pathname; } } diff --git a/src/client/stackdriver/debug.ts b/src/client/stackdriver/debug.ts index b132478c..adc02213 100644 --- a/src/client/stackdriver/debug.ts +++ b/src/client/stackdriver/debug.ts @@ -21,8 +21,16 @@ export interface PackageInfo { version: string; } +export interface DebugOptions extends GoogleAuthOptions { + /** + * The API endpoint of the service used to make requests. + * Defaults to `clouddebugger.googleapis.com`. + */ + apiEndpoint?: string; +} + export class Debug extends Service { - options!: GoogleAuthOptions; + options!: DebugOptions; packageInfo!: PackageInfo; /** @@ -49,7 +57,7 @@ export class Debug extends Service { * @param options - [Authentication options](#/docs) */ constructor( - options: GoogleAuthOptions, + options: DebugOptions = {}, packageJson: { name: string; version: string; @@ -58,10 +66,11 @@ export class Debug extends Service { if (new.target !== Debug) { return new Debug(options, packageJson); } - + options.apiEndpoint = options.apiEndpoint || 'clouddebugger.googleapis.com'; const config = { projectIdRequired: false, - baseUrl: 'https://clouddebugger.googleapis.com/v2', + apiEndpoint: options.apiEndpoint, + baseUrl: `https://${options.apiEndpoint}/v2`, scopes: ['https://www.googleapis.com/auth/cloud_debugger'], packageJson, }; diff --git a/test/test-controller.ts b/test/test-controller.ts index d25a2add..6e714d84 100644 --- a/test/test-controller.ts +++ b/test/test-controller.ts @@ -30,6 +30,7 @@ delete process.env.GCLOUD_PROJECT; import {Controller} from '../src/agent/controller'; // TODO: Fix fakeDebug to actually implement Debug. const fakeDebug = ({ + apiEndpoint: `clouddebugger.googleapis.com`, request: (options: r.Options, cb: r.RequestCallback) => { teenyRequest(options, (err, r) => { cb(err, r ? r.body : undefined, r);