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: v9 API cleanup #8225

Merged
merged 32 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
dfbae57
Remove formatTiles prop
felixpalmer Oct 24, 2023
d6f1dee
Remove fetchLayerData
felixpalmer Oct 24, 2023
f06e78b
Revert "CARTO: Support v3.3 spatialDataType & spatialDataColumn (#8168)"
felixpalmer Oct 24, 2023
ad9a2c3
PoC using source in _fetchMapDataset
felixpalmer Oct 24, 2023
7b93fe4
Support all types of quadbin sources
felixpalmer Oct 24, 2023
448647c
Support vector layers
felixpalmer Oct 24, 2023
7d934c2
Tileset sources
felixpalmer Oct 24, 2023
c5db24a
Better options merging
felixpalmer Oct 24, 2023
29b5737
Support queryParameters
felixpalmer Oct 24, 2023
16b9304
Pass in cache for all map types
felixpalmer Oct 24, 2023
0392ebe
Remove unused code
felixpalmer Oct 24, 2023
0d6c20f
Use requestWithParameters for tilestats
felixpalmer Oct 24, 2023
dfc3d8b
Use requestWithParameters for public map
felixpalmer Oct 24, 2023
24426fc
Remove old request functions
felixpalmer Oct 24, 2023
70fe44d
Tidy fetchMap
felixpalmer Oct 24, 2023
304b161
Dataset type
felixpalmer Oct 24, 2023
17793ee
Add If-Modified-Since header to autorefresh
felixpalmer Oct 24, 2023
2cea69b
Lint
felixpalmer Oct 24, 2023
a1f75d0
Reinstate iframe
felixpalmer Oct 25, 2023
edd73ea
Remove set/getDefaultCredentials
felixpalmer Oct 25, 2023
d3aa3ce
Consolidate types
felixpalmer Oct 25, 2023
0036a0d
Improve fetchMap types
felixpalmer Oct 25, 2023
9aa13b7
Remove duplicate assert function
felixpalmer Oct 25, 2023
7fc374e
Move buildApiEndpoint
felixpalmer Oct 25, 2023
ad23489
Tidy
felixpalmer Oct 25, 2023
80bc967
Move requestWithParameters
felixpalmer Oct 25, 2023
7d03740
Rename maps-v3-client to fetch-map
felixpalmer Oct 25, 2023
576d205
parseMap -> parse-map
felixpalmer Oct 25, 2023
359f6ee
Create sources/types
felixpalmer Oct 25, 2023
8d676f7
Update imports for source types
felixpalmer Oct 25, 2023
61673ce
Move maps-api-common to common
felixpalmer Oct 25, 2023
7cbf3ea
Merge branch 'master' into felix/carto-v9-cleanup
felixpalmer Oct 25, 2023
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 .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@f1f314fca9dfce2769ece7d933488f076716723e # v1.4.6
with:
node-version: '18.x'
node-version: '16.x'

- name: Publish release
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: actions/setup-node@f1f314fca9dfce2769ece7d933488f076716723e # v1.4.6
with:
# headless GL 6.0.1 needs recent version of Node 16.
node-version: '18.18'
node-version: '16.19'

- name: Install dependencies
run: |
Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
- name: Install dependencies
run: |
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.1/install.sh | bash
nvm install 18.18 && nvm use 18.18
nvm install 16.9 && nvm use 16.9
cd bindings/pydeck
make setup-env
make init
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@f1f314fca9dfce2769ece7d933488f076716723e # v1.4.6
with:
node-version: '18.x'
node-version: '16.x'

- name: Get version
id: get-version
Expand Down
10 changes: 1 addition & 9 deletions modules/carto/src/api/carto-api-error.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {MapType, RequestType} from './maps-api-common';

export type APIErrorContext = {
requestType: RequestType;
mapId?: string;
connection?: string;
source?: string;
type?: MapType;
};
import type {APIErrorContext} from './types';

/**
*
Expand Down
14 changes: 14 additions & 0 deletions modules/carto/src/api/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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 MAX_GET_LENGTH = 8192;

export const DEFAULT_PARAMETERS = {
client: DEFAULT_CLIENT,
v: V3_MINOR_VERSION
};

export const DEFAULT_HEADERS = {
Accept: 'application/json',
'Content-Type': 'application/json'
};
34 changes: 34 additions & 0 deletions modules/carto/src/api/endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {MapType} from './types';

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

function buildUrlFromBase(apiBaseUrl: string, endpoint: V3Endpoint) {
let suffix = `/v3/${endpoint}`;
if (apiBaseUrl.endsWith('/')) {
suffix = suffix.substring(1);
}

return `${apiBaseUrl}${suffix}`;
}

export function buildMapsUrlFromBase(apiBaseUrl: string): string {
return buildUrlFromBase(apiBaseUrl, 'maps');
}

export function buildStatsUrlFromBase(apiBaseUrl: string): string {
return buildUrlFromBase(apiBaseUrl, 'stats');
}

export function buildSourceUrl({
apiBaseUrl,
connectionName,
endpoint,
mapsUrl
}: {
apiBaseUrl: string;
connectionName: string;
endpoint: MapType;
mapsUrl?: string;
}): string {
return `${mapsUrl || buildMapsUrlFromBase(apiBaseUrl)}/${connectionName}/${endpoint}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
/**
* Maps API Client for Carto 3
*/
import {buildMapsUrlFromBase, buildStatsUrlFromBase} from '../config';
import {Format, MapType, MAP_TYPES, QueryParameters} from './maps-api-common';

import {CartoAPIError, APIErrorContext} from './carto-api-error';

import {parseMap} from './parseMap';
import {assert} from '../utils';
import {CartoAPIError} from './carto-api-error';
import {DEFAULT_API_BASE_URL, DEFAULT_CLIENT} from './common';
import {buildMapsUrlFromBase, buildStatsUrlFromBase} from './endpoints';
import {
GeojsonResult,
JsonResult,
SOURCE_DEFAULTS,
TilejsonResult,
h3QuerySource,
h3TableSource,
Expand All @@ -22,7 +17,10 @@ import {
vectorTableSource,
vectorTilesetSource
} from '../sources';
import {requestWithParameters} from '../sources/utils';
import {parseMap} from './parse-map';
import {requestWithParameters} from './request-with-parameters';
import {assert} from '../utils';
import type {APIErrorContext, Format, MapType, QueryParameters} from './types';

type Dataset = {
id: string;
Expand Down Expand Up @@ -157,15 +155,18 @@ async function fillInMapDatasets(
return await Promise.all(promises);
}

async function fillInTileStats({datasets, keplerMapConfig, token}, apiBaseUrl: string) {
async function fillInTileStats(
{datasets, keplerMapConfig, token}: {datasets: Dataset[]; keplerMapConfig: any; token: string},
apiBaseUrl: string
) {
const attributes: {attribute?: string; dataset?: any}[] = [];
const {layers} = keplerMapConfig.config.visState;
for (const layer of layers) {
for (const channel of Object.keys(layer.visualChannels)) {
const attribute = layer.visualChannels[channel]?.name;
if (attribute) {
const dataset = datasets.find(d => d.id === layer.config.dataId);
if (dataset.data.tilestats && dataset.type !== MAP_TYPES.TILESET) {
if (dataset && dataset.type !== 'tileset' && (dataset.data as TilejsonResult).tilestats) {
// Only fetch stats for QUERY & TABLE map types
attributes.push({attribute, dataset});
}
Expand All @@ -190,24 +191,26 @@ async function fillInTileStats({datasets, keplerMapConfig, token}, apiBaseUrl: s
return await Promise.all(promises);
}

export type FetchMapOptions = {
apiBaseUrl?: string;
cartoMapId: string;
clientId?: string;
headers?: Record<string, string>;
mapsUrl?: string;
autoRefresh?: number;
onNewData?: (map: any) => void;
};

/* eslint-disable max-statements */
export async function fetchMap({
apiBaseUrl = SOURCE_DEFAULTS.apiBaseUrl,
apiBaseUrl = DEFAULT_API_BASE_URL,
cartoMapId,
clientId = SOURCE_DEFAULTS.clientId,
clientId = DEFAULT_CLIENT,
headers = {},
mapsUrl,
autoRefresh,
onNewData
}: {
apiBaseUrl: string;
cartoMapId: string;
clientId: string;
headers: Record<string, string>;
mapsUrl?: string;
autoRefresh?: number;
onNewData?: (map: any) => void;
}) {
}: FetchMapOptions) {
assert(cartoMapId, 'Must define CARTO map id: fetchMap({cartoMapId: "XXXX-XXXX-XXXX"})');
assert(apiBaseUrl, 'Must define apiBaseUrl');

Expand Down
7 changes: 3 additions & 4 deletions modules/carto/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export {CartoAPIError} from './carto-api-error';
export type {APIErrorContext} from './carto-api-error';
export {FORMATS, TILE_FORMATS, MAP_TYPES, API_VERSIONS} from './maps-api-common';
export type {QueryParameters} from './maps-api-common';
export {fetchMap} from './maps-v3-client';
export {fetchMap} from './fetch-map';
export type {FetchMapOptions} from './fetch-map';
export type {APIErrorContext, Format, MapType, RequestType, QueryParameters} from './types';
13 changes: 3 additions & 10 deletions modules/carto/src/api/layer-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import H3TileLayer from '../layers/h3-tile-layer';
import QuadbinTileLayer from '../layers/quadbin-tile-layer';
import RasterTileLayer from '../layers/raster-tile-layer';
import VectorTileLayer from '../layers/vector-tile-layer';
import {MapType, TILE_FORMATS, TileFormat} from './maps-api-common';
import {MapType} from './types';
import {assert, createBinaryProxy, scaleIdentity} from '../utils';
import {
CustomMarkersRange,
Expand Down Expand Up @@ -202,7 +202,6 @@ export function getLayer(
}

export function layerFromTileDataset(
formatTiles: TileFormat | null = TILE_FORMATS.MVT,
scheme: string,
type?: MapType
): typeof VectorTileLayer | typeof H3TileLayer | typeof QuadbinTileLayer {
Expand All @@ -223,22 +222,16 @@ function getTileLayer(dataset: MapDataset, basePropMap) {
const {
aggregationExp,
aggregationResLevel,
data: {
scheme,
tiles: [tileUrl]
}
data: {scheme}
} = dataset;
/* global URL */
const formatTiles = new URL(tileUrl).searchParams.get('formatTiles') as TileFormat;

return {
Layer: layerFromTileDataset(formatTiles, scheme),
Layer: layerFromTileDataset(scheme),
propMap: basePropMap,
defaultProps: {
...defaultProps,
...(aggregationExp && {aggregationExp}),
...(aggregationResLevel && {aggregationResLevel}),
formatTiles,
uniqueIdProperty: 'geoid'
}
};
Expand Down
95 changes: 0 additions & 95 deletions modules/carto/src/api/maps-api-common.ts

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import {APIErrorContext, CartoAPIError} from '../api/carto-api-error';
import {encodeParameter, MapType} from '../api/maps-api-common';
import {CartoAPIError} from './carto-api-error';
import {DEFAULT_HEADERS, DEFAULT_PARAMETERS, MAX_GET_LENGTH} from './common';
import {buildMapsUrlFromBase} from '../config';
import type {APIErrorContext} from './types';

/**
* Simple encode parameter
*/
function encodeParameter(name: string, value: string | boolean | number): string {
return `${name}=${encodeURIComponent(value)}`;
}

const REQUEST_CACHE = new Map();
export async function requestWithParameters<T = any>({
Expand Down Expand Up @@ -55,17 +61,3 @@ export async function requestWithParameters<T = any>({
throw new CartoAPIError(error as Error, errorContext);
}
}

export function buildApiEndpoint({
apiBaseUrl,
connectionName,
endpoint,
mapsUrl
}: {
apiBaseUrl: string;
connectionName: string;
endpoint: MapType;
mapsUrl?: string;
}): string {
return `${mapsUrl || buildMapsUrlFromBase(apiBaseUrl)}/${connectionName}/${endpoint}`;
}
Loading
Loading