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

feat: accept apiEndpoint override #455

Merged
merged 3 commits into from
May 28, 2019
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"types": "dtsd bigquery v2 > ./src/types.d.ts"
},
"dependencies": {
"@google-cloud/common": "^1.0.0",
"@google-cloud/common": "^2.0.0",
"@google-cloud/paginator": "^1.0.0",
"@google-cloud/promisify": "^1.0.0",
"arrify": "^2.0.0",
Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ export interface BigQueryOptions extends common.GoogleAuthOptions {
autoRetry?: boolean;
maxRetries?: number;
location?: string;
/**
* The API endpoint of the service used to make requests.
* Defaults to `www.googleapis.com`.
*/
apiEndpoint?: string;
}

/**
Expand Down Expand Up @@ -238,10 +243,11 @@ export class BigQuery extends common.Service {
getDatasetsStream: (options?: GetDatasetsOptions) => ResourceStream<Dataset>;
getJobsStream: (options?: GetJobsOptions) => ResourceStream<Job>;

constructor(options?: BigQueryOptions) {
options = options || {};
constructor(options: BigQueryOptions = {}) {
options.apiEndpoint = options.apiEndpoint || 'www.googleapis.com';
const config = {
baseUrl: 'https://www.googleapis.com/bigquery/v2',
apiEndpoint: options.apiEndpoint,
baseUrl: `https://${options.apiEndpoint}/bigquery/v2`,
scopes: ['https://www.googleapis.com/auth/bigquery'],
packageJson: require('../../package.json'),
};
Expand Down
4 changes: 3 additions & 1 deletion src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,9 @@ class Table extends common.ServiceObject {
} as {},
request: {
uri: format('{base}/{projectId}/jobs', {
base: 'https://www.googleapis.com/upload/bigquery/v2/projects',
base: `https://${
this.bigQuery.apiEndpoint
}/upload/bigquery/v2/projects`,
projectId: this.bigQuery.projectId,
}),
},
Expand Down
15 changes: 13 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import * as proxyquire from 'proxyquire';
import * as sinon from 'sinon';
import * as uuid from 'uuid';

import {BigQueryDate, Dataset, Job, Query, Table} from '../src';
import {BigQueryDate, Dataset, Job, Table} from '../src';
import {JobOptions} from '../src/job';
import {TableField} from '../src/table';

Expand Down Expand Up @@ -186,12 +186,23 @@ describe('BigQuery', () => {
);
});

it('should allow overriding the apiEndpoint', () => {
const apiEndpoint = 'not.real.local';
bq = new BigQuery({
apiEndpoint,
});
const calledWith = bq.calledWith_[0];
assert.strictEqual(
calledWith.baseUrl,
`https://${apiEndpoint}/bigquery/v2`
);
});

it('should capture any user specified location', () => {
const bq = new BigQuery({
projectId: PROJECT_ID,
location: LOCATION,
});

assert.strictEqual(bq.location, LOCATION);
});

Expand Down
2 changes: 1 addition & 1 deletion test/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
JobLoadMetadata,
Table,
TableMetadata,
TableOptions,
ViewDefinition,
} from '../src/table';
import bigquery from '../src/types';
Expand Down Expand Up @@ -113,6 +112,7 @@ describe('BigQuery/Table', () => {
job: (id: string) => {
return {id};
},
apiEndpoint: 'www.googleapis.com',
request: util.noop,
},
};
Expand Down