Skip to content

Commit

Permalink
fix(carto): Add missing 'client' parameter to query requests (#8913)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Felix Palmer <[email protected]>
  • Loading branch information
donmccurdy and felixpalmer committed May 23, 2024
1 parent 0870a72 commit d541524
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
6 changes: 4 additions & 2 deletions modules/carto/src/api/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type UrlParameters = {q: string; queryParameters?: string};
export const query = async function (options: QueryOptions): Promise<QueryResult> {
const {
apiBaseUrl = SOURCE_DEFAULTS.apiBaseUrl,
clientId = SOURCE_DEFAULTS.clientId,
connectionName,
sqlQuery,
queryParameters
Expand All @@ -22,16 +23,17 @@ export const query = async function (options: QueryOptions): Promise<QueryResult

const baseUrl = buildQueryUrl({apiBaseUrl, connectionName});
const headers = {Authorization: `Bearer ${options.accessToken}`, ...options.headers};
const parameters = {client: clientId, ...urlParameters};

const errorContext: APIErrorContext = {
requestType: 'SQL',
connection: options.connectionName,
type: 'query',
source: JSON.stringify(urlParameters, undefined, 2)
source: JSON.stringify(parameters, undefined, 2)
};
return await requestWithParameters<QueryResult>({
baseUrl,
parameters: urlParameters,
parameters,
headers,
errorContext
});
Expand Down
26 changes: 26 additions & 0 deletions test/modules/carto/api/query.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {query} from '@deck.gl/carto';
import test from 'tape-catch';
import {withMockFetchMapsV3, QUERY_RESPONSE} from '../mock-fetch';

test('query', async t => {
await withMockFetchMapsV3(async calls => {
const response = await query({
connectionName: 'carto_dw',
clientId: 'CUSTOM_CLIENT',
accessToken: '<token>',
sqlQuery: 'SELECT * FROM a.b.h3_table'
});

t.is(calls.length, 1, 'calls fetch() x1');

const [queryCall] = calls;

t.match(queryCall.url, /v3\/sql\/carto_dw\/query/, 'connection');
t.match(queryCall.url, /q=SELECT%20\*%20FROM%20a\.b\.h3_table/, 'query');
t.match(queryCall.url, /client\=CUSTOM_CLIENT/, 'clientId');

t.ok(response, 'returns response');
t.deepEqual(response, QUERY_RESPONSE, 'correct response');
}).catch(t.fail);
t.end();
});
1 change: 1 addition & 0 deletions test/modules/carto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './api/carto-api-error.spec';
import './api/fetch-map.spec';
import './api/layer-map.spec';
import './api/parse-map.spec';
import './api/query.spec';
import './api/request-with-parameters.spec';
import './utils.spec';
import './layers/carto-vector-tile.spec';
Expand Down
5 changes: 5 additions & 0 deletions test/modules/carto/mock-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export const TILESTATS_RESPONSE = {
type: 'Number'
};

export const QUERY_RESPONSE = [{id: 1, value: 'string'}];

const createDefaultResponse = (
url: string,
headers: HeadersInit,
Expand All @@ -75,6 +77,9 @@ const createDefaultResponse = (
if (url.indexOf('stats') !== -1) {
return TILESTATS_RESPONSE;
}
if (url.indexOf('sql') !== -1) {
return QUERY_RESPONSE;
}
if (url.indexOf('query') !== -1 || url.indexOf('table')) {
return {
tilejson: {
Expand Down
2 changes: 2 additions & 0 deletions test/modules/carto/sources/h3-query-source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ test('h3QuerySource', async t => {
await withMockFetchMapsV3(async calls => {
const tilejson = await h3QuerySource({
connectionName: 'carto_dw',
clientId: 'CUSTOM_CLIENT',
accessToken: '<token>',
sqlQuery: 'SELECT * FROM a.b.h3_table',
aggregationExp: 'SUM(population) as pop'
Expand All @@ -20,6 +21,7 @@ test('h3QuerySource', async t => {
t.match(initCall.url, /spatialDataColumn=h3/, 'spatialDataColumn');
t.match(initCall.url, /spatialDataType=h3/, 'spatialDataType');
t.match(initCall.url, /q=SELECT%20\*%20FROM%20a\.b\.h3_table/, 'query');
t.match(initCall.url, /client\=CUSTOM_CLIENT/, 'clientId');

t.match(tilesetCall.url, /^https:\/\/xyz\.com\?format\=tilejson\&cache\=/, 'tileset URL');

Expand Down

0 comments on commit d541524

Please sign in to comment.