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

CARTO: API v3.3 reenable #8265

Merged
merged 10 commits into from
Nov 8, 2023
2 changes: 1 addition & 1 deletion modules/carto/src/api/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const DEFAULT_API_BASE_URL = 'https://gcp-us-east1.api.carto.com';
export const DEFAULT_CLIENT = 'deck-gl-carto';
export const V3_MINOR_VERSION = '3.2';
export const V3_MINOR_VERSION = '3.3';
export const MAX_GET_LENGTH = 8192;

export const DEFAULT_PARAMETERS = {
Expand Down
6 changes: 5 additions & 1 deletion modules/carto/src/api/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {MapType} from './types';

export type V3Endpoint = 'maps' | 'stats';
export type V3Endpoint = 'maps' | 'stats' | 'sql';

function buildUrlFromBase(apiBaseUrl: string, endpoint: V3Endpoint) {
let suffix = `/v3/${endpoint}`;
Expand All @@ -19,6 +19,10 @@ export function buildStatsUrlFromBase(apiBaseUrl: string): string {
return buildUrlFromBase(apiBaseUrl, 'stats');
}

export function buildQueryUrlFromBase(apiBaseUrl: string): string {
return buildUrlFromBase(apiBaseUrl, 'sql');
}

export function buildSourceUrl({
apiBaseUrl,
connectionName,
Expand Down
2 changes: 2 additions & 0 deletions modules/carto/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export {CartoAPIError} from './carto-api-error';
export {fetchMap} from './fetch-map';
export type {FetchMapOptions} from './fetch-map';
export type {APIErrorContext, Format, MapType, RequestType, QueryParameters} from './types';
export {query} from './query';
export type {QueryOptions} from './query';
34 changes: 34 additions & 0 deletions modules/carto/src/api/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {SOURCE_DEFAULTS} from '../sources';
import type {SourceOptions, QuerySourceOptions, QueryResult} from '../sources/types';
import {buildQueryUrlFromBase} from './endpoints';
import {requestWithParameters} from './request-with-parameters';
import {APIErrorContext} from './types';

export type QueryOptions = SourceOptions & Omit<QuerySourceOptions, 'spatialDataColumn'>;
type UrlParameters = {q: string; queryParameters?: string};

export const query = async function (options: QueryOptions): Promise<QueryResult> {
const {apiBaseUrl, connectionName, sqlQuery, queryParameters} = options;
const urlParameters: UrlParameters = {q: sqlQuery};

if (queryParameters) {
urlParameters.queryParameters = JSON.stringify(queryParameters);
}

const queryUrl = buildQueryUrlFromBase(apiBaseUrl || SOURCE_DEFAULTS.apiBaseUrl);
const baseUrl = `${queryUrl}/${connectionName}/query`;
const headers = {Authorization: `Bearer ${options.accessToken}`, ...options.headers};

const errorContext: APIErrorContext = {
requestType: 'SQL',
connection: options.connectionName,
type: 'query',
source: JSON.stringify(urlParameters, undefined, 2)
};
return await requestWithParameters<QueryResult>({
baseUrl,
parameters: urlParameters,
headers,
errorContext
});
};
2 changes: 1 addition & 1 deletion modules/carto/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {SCALE_TYPE} from './layer-map';

export type Format = 'json' | 'geojson' | 'tilejson';
export type MapType = 'query' | 'table' | 'tileset' | 'raster';
export type RequestType = 'Map data' | 'Map instantiation' | 'Public map' | 'Tile stats';
export type RequestType = 'Map data' | 'Map instantiation' | 'Public map' | 'Tile stats' | 'SQL';
export type APIErrorContext = {
requestType: RequestType;
mapId?: string;
Expand Down
5 changes: 3 additions & 2 deletions modules/carto/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ export {default as BASEMAP} from './basemap';
export {default as colorBins} from './style/color-bins-style';
export {default as colorCategories} from './style/color-categories-style';
export {default as colorContinuous} from './style/color-continuous-style';
export {CartoAPIError, fetchMap} from './api';
export {CartoAPIError, fetchMap, query} from './api';
export type {
APIErrorContext,
FetchMapOptions,
Format,
MapType,
RequestType,
QueryParameters
QueryParameters,
QueryOptions
} from './api';

import {
Expand Down
31 changes: 19 additions & 12 deletions modules/carto/src/sources/h3-query-source.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
/* eslint-disable camelcase */
import {baseSource} from './base-source';
import type {AggregationOptions, QuerySourceOptions, SourceOptions, TilejsonSource} from './types';
import type {
AggregationOptions,
QuerySourceOptions,
SourceOptions,
SpatialDataType,
TilejsonResult
} from './types';

export type H3QuerySourceOptions = SourceOptions & QuerySourceOptions & AggregationOptions;
type UrlParameters = {
aggregationExp: string;
aggregationResLevel?: string;
geo_column?: string;
spatialDataType: SpatialDataType;
spatialDataColumn?: string;
q: string;
queryParameters?: string;
};

const h3QuerySource: TilejsonSource<H3QuerySourceOptions> = async function (
export const h3QuerySource = async function (
options: H3QuerySourceOptions
): Promise<any> {
): Promise<TilejsonResult> {
const {
aggregationExp,
aggregationResLevel = 4,
sqlQuery,
spatialDataColumn = 'h3:h3',
spatialDataColumn = 'h3',
queryParameters
} = options;
const urlParameters: UrlParameters = {aggregationExp, q: sqlQuery};
const urlParameters: UrlParameters = {
aggregationExp,
spatialDataColumn,
spatialDataType: 'h3',
felixpalmer marked this conversation as resolved.
Show resolved Hide resolved
q: sqlQuery
};

if (aggregationResLevel) {
urlParameters.aggregationResLevel = String(aggregationResLevel);
}
if (spatialDataColumn) {
urlParameters.geo_column = spatialDataColumn;
}
if (queryParameters) {
urlParameters.queryParameters = JSON.stringify(queryParameters);
}
return baseSource<UrlParameters>('query', options, urlParameters);
return baseSource<UrlParameters>('query', options, urlParameters) as Promise<TilejsonResult>;
};

export {h3QuerySource};
31 changes: 19 additions & 12 deletions modules/carto/src/sources/h3-table-source.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
/* eslint-disable camelcase */
import {baseSource} from './base-source';
import type {AggregationOptions, SourceOptions, TableSourceOptions, TilejsonSource} from './types';
import type {
AggregationOptions,
SourceOptions,
SpatialDataType,
TableSourceOptions,
TilejsonResult
} from './types';

export type H3TableSourceOptions = SourceOptions & TableSourceOptions & AggregationOptions;

type UrlParameters = {
aggregationExp: string;
aggregationResLevel?: string;
columns?: string;
geo_column?: string;
spatialDataType: SpatialDataType;
spatialDataColumn?: string;
name: string;
};

const h3TableSource: TilejsonSource<H3TableSourceOptions> = async function (
export const h3TableSource = async function (
options: H3TableSourceOptions
): Promise<any> {
): Promise<TilejsonResult> {
const {
aggregationExp,
aggregationResLevel = 4,
columns,
spatialDataColumn = 'h3:h3',
spatialDataColumn = 'h3',
tableName
} = options;
const urlParameters: UrlParameters = {aggregationExp, name: tableName};
const urlParameters: UrlParameters = {
aggregationExp,
name: tableName,
spatialDataColumn,
spatialDataType: 'h3'
};

if (aggregationResLevel) {
urlParameters.aggregationResLevel = String(aggregationResLevel);
}
if (columns) {
urlParameters.columns = columns.join(',');
}
if (spatialDataColumn) {
urlParameters.geo_column = spatialDataColumn;
}
return baseSource<UrlParameters>('table', options, urlParameters);
return baseSource<UrlParameters>('table', options, urlParameters) as Promise<TilejsonResult>;
};

export {h3TableSource};
10 changes: 4 additions & 6 deletions modules/carto/src/sources/h3-tileset-source.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import {baseSource} from './base-source';
import type {SourceOptions, TilesetSourceOptions, TilejsonSource} from './types';
import type {SourceOptions, TilejsonResult, TilesetSourceOptions} from './types';

export type H3TilesetSourceOptions = SourceOptions & TilesetSourceOptions;
type UrlParameters = {name: string};

const h3TilesetSource: TilejsonSource<H3TilesetSourceOptions> = async function (
export const h3TilesetSource = async function (
options: H3TilesetSourceOptions
): Promise<any> {
): Promise<TilejsonResult> {
const {tableName} = options;
const urlParameters: UrlParameters = {name: tableName};

return baseSource<UrlParameters>('tileset', options, urlParameters);
return baseSource<UrlParameters>('tileset', options, urlParameters) as Promise<TilejsonResult>;
};

export {h3TilesetSource};
31 changes: 19 additions & 12 deletions modules/carto/src/sources/quadbin-query-source.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
/* eslint-disable camelcase */
import {baseSource} from './base-source';
import type {AggregationOptions, QuerySourceOptions, SourceOptions, TilejsonSource} from './types';
import type {
AggregationOptions,
QuerySourceOptions,
SourceOptions,
SpatialDataType,
TilejsonResult
} from './types';

export type QuadbinQuerySourceOptions = SourceOptions & QuerySourceOptions & AggregationOptions;

type UrlParameters = {
aggregationExp: string;
aggregationResLevel?: string;
geo_column?: string;
spatialDataType: SpatialDataType;
spatialDataColumn?: string;
q: string;
queryParameters?: string;
};

const quadbinQuerySource: TilejsonSource<QuadbinQuerySourceOptions> = async function (
export const quadbinQuerySource = async function (
options: QuadbinQuerySourceOptions
): Promise<any> {
): Promise<TilejsonResult> {
const {
aggregationExp,
aggregationResLevel = 6,
sqlQuery,
spatialDataColumn = 'quadbin:quadbin',
spatialDataColumn = 'quadbin',
queryParameters
} = options;
const urlParameters: UrlParameters = {aggregationExp, q: sqlQuery};
const urlParameters: UrlParameters = {
aggregationExp,
q: sqlQuery,
spatialDataColumn,
spatialDataType: 'quadbin'
felixpalmer marked this conversation as resolved.
Show resolved Hide resolved
};

if (aggregationResLevel) {
urlParameters.aggregationResLevel = String(aggregationResLevel);
}
if (spatialDataColumn) {
urlParameters.geo_column = spatialDataColumn;
}
if (queryParameters) {
urlParameters.queryParameters = JSON.stringify(queryParameters);
}
return baseSource<UrlParameters>('query', options, urlParameters);
return baseSource<UrlParameters>('query', options, urlParameters) as Promise<TilejsonResult>;
};

export {quadbinQuerySource};
32 changes: 20 additions & 12 deletions modules/carto/src/sources/quadbin-table-source.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
/* eslint-disable camelcase */
import {baseSource} from './base-source';
import type {AggregationOptions, SourceOptions, TableSourceOptions, TilejsonSource} from './types';
import type {
AggregationOptions,
SourceOptions,
SpatialDataType,
TableSourceOptions,
TilejsonResult
} from './types';

export type QuadbinTableSourceOptions = SourceOptions & TableSourceOptions & AggregationOptions;

type UrlParameters = {
aggregationExp: string;
aggregationResLevel?: string;
columns?: string;
geo_column?: string;
spatialDataType: SpatialDataType;
spatialDataColumn?: string;
name: string;
};

const quadbinTableSource: TilejsonSource<QuadbinTableSourceOptions> = async function (
export const quadbinTableSource = async function (
options: QuadbinTableSourceOptions
): Promise<any> {
): Promise<TilejsonResult> {
const {
aggregationExp,
aggregationResLevel = 6,
columns,
spatialDataColumn = 'quadbin:quadbin',
spatialDataColumn = 'quadbin',
tableName
} = options;
const urlParameters: UrlParameters = {aggregationExp, name: tableName};

const urlParameters: UrlParameters = {
aggregationExp,
name: tableName,
spatialDataColumn,
spatialDataType: 'quadbin'
felixpalmer marked this conversation as resolved.
Show resolved Hide resolved
};

if (aggregationResLevel) {
urlParameters.aggregationResLevel = String(aggregationResLevel);
}
if (columns) {
urlParameters.columns = columns.join(',');
}
if (spatialDataColumn) {
urlParameters.geo_column = spatialDataColumn;
}
return baseSource<UrlParameters>('table', options, urlParameters);
return baseSource<UrlParameters>('table', options, urlParameters) as Promise<TilejsonResult>;
};

export {quadbinTableSource};
10 changes: 4 additions & 6 deletions modules/carto/src/sources/quadbin-tileset-source.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import {baseSource} from './base-source';
import type {SourceOptions, TilesetSourceOptions, TilejsonSource} from './types';
import type {SourceOptions, TilejsonResult, TilesetSourceOptions} from './types';

export type QuadbinTilesetSourceOptions = SourceOptions & TilesetSourceOptions;
type UrlParameters = {name: string};

const quadbinTilesetSource: TilejsonSource<QuadbinTilesetSourceOptions> = async function (
export const quadbinTilesetSource = async function (
options: QuadbinTilesetSourceOptions
): Promise<any> {
): Promise<TilejsonResult> {
const {tableName} = options;
const urlParameters: UrlParameters = {name: tableName};

return baseSource<UrlParameters>('tileset', options, urlParameters);
return baseSource<UrlParameters>('tileset', options, urlParameters) as Promise<TilejsonResult>;
};

export {quadbinTilesetSource};
10 changes: 3 additions & 7 deletions modules/carto/src/sources/raster-source.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import {baseSource} from './base-source';
import type {SourceOptions, TilesetSourceOptions, TypedSource} from './types';
import type {SourceOptions, TilejsonResult, TilesetSourceOptions} from './types';

export type RasterSourceOptions = SourceOptions & TilesetSourceOptions;
type UrlParameters = {name: string};

const rasterSource: TypedSource<RasterSourceOptions> = async function (
options: RasterSourceOptions
): Promise<any> {
export const rasterSource = async function (options: RasterSourceOptions): Promise<TilejsonResult> {
const {tableName} = options;
const urlParameters: UrlParameters = {name: tableName};

return baseSource<UrlParameters>('raster', options, urlParameters);
return baseSource<UrlParameters>('raster', options, urlParameters) as Promise<TilejsonResult>;
};

export {rasterSource};
Loading
Loading