diff --git a/NEWS.md b/NEWS.md index b6b00e20..9806f7da 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,15 @@ +1.13.1 -- 2014-mm-dd +-------------------- + + +1.13.0 -- 2014-07-30 +-------------------- + +New features: + - Support for postgresql schemas + - Use public user from redis + - Support for several auth tokens + 1.12.1 -- 2014-06-24 -------------------- diff --git a/app.js b/app.js index 4792334d..c420ab61 100755 --- a/app.js +++ b/app.js @@ -29,6 +29,7 @@ var _ = require('underscore'); // set environment specific variables global.environment = require(__dirname + '/config/environments/' + ENV); +global.environment.api_hostname = require('os').hostname().split('.')[0]; global.log4js = require('log4js') log4js_config = { diff --git a/docs/Map-API-internal.md b/docs/Map-API-internal.md new file mode 100644 index 00000000..0956f271 --- /dev/null +++ b/docs/Map-API-internal.md @@ -0,0 +1,111 @@ +# Kind of maps + +Windshaft-CartoDB supports these kind of maps: + + - [Temporary maps](#temporary-maps) (created by anyone) + - [Detached maps](#detached-maps) + - [Inline maps](#inline-maps) (legacy) + - [Persistent maps](#peristent-maps) (created by CartDB user) + - [Template maps](#template-maps) + - [Table maps](#table-maps) (legacy, deprecated) + +## Temporary maps + +Temporary maps have no owners and are anonymous in nature. +There are two kind of temporary maps: + + - Detached maps (aka MultiLayer-API) + - Inline maps + +### Detached maps + +Detached maps are maps which are configured with a request +obtaining a temporary token and then used by referencing +the obtained token. The token expires automatically when unused. + +Anyone can create detached maps, but users will need read access +to the data source of the map layers. + +The configuration format is a [MapConfig] +(http://github.com/CartoDB/Windshaft/wiki/MapConfig-specification) document. + +The HTTP endpoints for creating the map and using it are described [here] +(http://github.com/CartoDB/Windshaft-cartodb/wiki/MultiLayer-API) + +*TODO* cleanup the referenced document + +### Inline maps + +Inline maps are maps that only exist for a single request, +being the request for a specific map resource (tile). + +Inline maps are always bound to a table, and can only be +obtained by those having read access to the that table. +Additionally, users need to have access to any datasource +specified as part of the configuration. + +Inline maps only support PNG and UTF8GRID tiles. + +The configuration consist in a set of parameters, to be +specified in the query string of the tile request: + + * sql - the query to run as datasource, can be an array + * style - the CartoCSS style for the datasource, can be an array + * style_version - version of the CartoCSS style, can be an array + * interactivity - only for fetching UTF8GRID, + +If the style is not provided, style of the associated table is +used; if the sql is not provided, all records of the associated +table are used as the datasource; the two possibilities result +in a mix between _inline_ maps and [Table maps][]. + +*TODO* specify (or link) api endpoints + +## Persistent maps + +Persistent maps can only be created by a CartoDB user who has full +responsibility over editing and deleting them. There are two +kind of persistent maps: + + - Template maps + - Table maps (legacy, deprecated) + +### Templated maps + +Templated maps are templated [MapConfig] +(http://github.com/CartoDB/Windshaft/wiki/MapConfig-specification) documents +associated with an authorization certificate. + +The authorization certificate determines who can instanciate the +template and use the resulting map. Authorized users of the instanciated +maps will have the same database access privilege of the template owner. + +The HTTP endpoints for creating and using templated maps are described [here] +(http://github.com/CartoDB/Windshaft-cartodb/wiki/Template-maps). + +*TODO* cleanup the referenced document + +### Table maps + +Table maps are maps associated with a table. +Configuration of such maps is limited to the CartoCSS style. + + * style - the CartoCSS style for the datasource, can be an array + * style_version - version of the CartoCSS style, can be an array + +You can only fetch PNG or UTF8GRID tiles from these maps. + +Access method is the same as the one for [Inline maps](#inline-maps) + +# Endpoints description + +- **/api/maps/** (same interface than https://github.com/CartoDB/Windshaft/wiki/Multilayer-API) +- **/api/maps/named** (same interface than https://github.com/CartoDB/Windshaft-cartodb/wiki/Template-maps) + + +NOTE: in case Multilayer-API does not contain this info yet, the + endpoint for fetching attributes is this: + +- **/api/maps/:map_id/:layer_index/attributes/:feature_id** + - would return { c: 1, d: 2 } + diff --git a/docs/Map-API.md b/docs/Map-API.md index 0956f271..db593e36 100644 --- a/docs/Map-API.md +++ b/docs/Map-API.md @@ -1,111 +1,638 @@ -# Kind of maps +## Maps API -Windshaft-CartoDB supports these kind of maps: +The CartoDB Maps API allows you to generate maps based on data hosted in your CartoDB account and style them using CartoCSS. The API generates a XYZ based URL to fetch Web Mercator projected tiles using web clients like Leaflet, Google Maps, OpenLayers. - - [Temporary maps](#temporary-maps) (created by anyone) - - [Detached maps](#detached-maps) - - [Inline maps](#inline-maps) (legacy) - - [Persistent maps](#peristent-maps) (created by CartDB user) - - [Template maps](#template-maps) - - [Table maps](#table-maps) (legacy, deprecated) +You can create two types of maps with the Maps API: -## Temporary maps +- **Anonymous maps** + Maps that can be created using your CartoDB public data. Any client can change the read-only SQL and CartoCSS parameters that generate the map tiles. These maps can be created from a JavaScript application alone and no authenticated calls are needed. See [this CartoDB.js example]({{ '/cartodb-platform/cartodb-js.html' | prepend: site.baseurl }}). -Temporary maps have no owners and are anonymous in nature. -There are two kind of temporary maps: +- **Named maps** + Maps that access to your private data. These maps require an owner to setup and modify any SQL and CartoCSS parameters and are not modifiable without new setup calls. - - Detached maps (aka MultiLayer-API) - - Inline maps +## Quickstart -### Detached maps +### Anonymous maps -Detached maps are maps which are configured with a request -obtaining a temporary token and then used by referencing -the obtained token. The token expires automatically when unused. +Here is an example of how to create an anonymous map with JavaScript: -Anyone can create detached maps, but users will need read access -to the data source of the map layers. +{% highlight javascript %} +var mapconfig = { + "version": "1.0.1", + "layers": [{ + "type": "cartodb", + "options": { + "cartocss_version": "2.1.1", + "cartocss": "#layer { polygon-fill: #FFF; }", + "sql": "select * from european_countries_e" + } + }] +} -The configuration format is a [MapConfig] -(http://github.com/CartoDB/Windshaft/wiki/MapConfig-specification) document. +$.ajax({ + crossOrigin: true, + type: 'POST', + dataType: 'json', + contentType: 'application/json', + url: 'http://documentation.cartodb.com/api/v1/map', + data: JSON.stringify(mapconfig), + success: function(data) { + var templateUrl = 'http://documentation.cartodb.com/api/v1/map/' + data.layergroupid + '{z}/{x}/{y}.png' + console.log(templateUrl); + } +}) +{% endhighlight %} -The HTTP endpoints for creating the map and using it are described [here] -(http://github.com/CartoDB/Windshaft-cartodb/wiki/MultiLayer-API) +### Named maps -*TODO* cleanup the referenced document +Let's create a named map using some private tables in a CartoDB account. +The following API call creates a map of European countries that have a white fill color: -### Inline maps +{% highlight javascript %} +// mapconfig.json +{ + "version": "0.0.1" + "name": "test", + "auth": { + "method": "open" + }, + "layergroup": { + "layers": [{ + "type": "cartodb", + "options": { + "cartocss_version": "2.1.1", + "cartocss": "#layer { polygon-fill: #FFF; }", + "sql": "select * from european_countries_e" + } + }] + } +} +{% endhighlight %} -Inline maps are maps that only exist for a single request, -being the request for a specific map resource (tile). +The map config needs to be sent to CartoDB's Map API using an authenticated call. Here we use a command line tool called `curl`. For more info about this tool see [this blog post](http://quickleft.com/blog/command-line-tutorials-curl) or type ``man curl`` in bash. Using `curl` the call would look like: -Inline maps are always bound to a table, and can only be -obtained by those having read access to the that table. -Additionally, users need to have access to any datasource -specified as part of the configuration. +
+{% highlight bash %} +curl 'https://{account}.cartodb.com/api/v1/map/named?api_key=APIKEY' -H 'Content-Type: application/json' -d @mapconfig.json +{% endhighlight %} -Inline maps only support PNG and UTF8GRID tiles. +To get the `URL` to fetch the tiles you need to instantiate the map. -The configuration consist in a set of parameters, to be -specified in the query string of the tile request: + +{% highlight bash %} +curl 'http://{account}.cartodb.com/api/v1/map/named/test' -H 'Content-Type: application/json' +{% endhighlight %} - * sql - the query to run as datasource, can be an array - * style - the CartoCSS style for the datasource, can be an array - * style_version - version of the CartoCSS style, can be an array - * interactivity - only for fetching UTF8GRID, +The response will return JSON with properties for the `layergroupid` and the timestamp (`last_updated`) of the last data modification. -If the style is not provided, style of the associated table is -used; if the sql is not provided, all records of the associated -table are used as the datasource; the two possibilities result -in a mix between _inline_ maps and [Table maps][]. +Here is an example response: -*TODO* specify (or link) api endpoints +{% highlight javascript %} +{ + "layergroupid": "c01a54877c62831bb51720263f91fb33:0", + "last_updated": "1970-01-01T00:00:00.000Z" +} +{% endhighlight %} -## Persistent maps +You can use the `layergroupid` to instantiate a URL template for accessing tiles on the client. Here we use the `layergroupid` from the example response above in this URL template: -Persistent maps can only be created by a CartoDB user who has full -responsibility over editing and deleting them. There are two -kind of persistent maps: +{% highlight bash %} +http://documentation.cartodb.com/tiles/layergroup/c01a54877c62831bb51720263f91fb33:0/{z}/{x}/{y}.png +{% endhighlight %} - - Template maps - - Table maps (legacy, deprecated) +## General Concepts -### Templated maps +The following concepts are the same for every endpoint in the API except when it's noted explicitly. -Templated maps are templated [MapConfig] -(http://github.com/CartoDB/Windshaft/wiki/MapConfig-specification) documents -associated with an authorization certificate. +### Auth -The authorization certificate determines who can instanciate the -template and use the resulting map. Authorized users of the instanciated -maps will have the same database access privilege of the template owner. +By default, users do not have access to private tables in CartoDB. In order to instantiate a map from private table data an API Key is required. Additionally, to include some endpoints an API Key must be included (e.g. creating a named map). -The HTTP endpoints for creating and using templated maps are described [here] -(http://github.com/CartoDB/Windshaft-cartodb/wiki/Template-maps). +To execute an authorized request, api_key=YOURAPIKEY should be added to the request URL. The param can be also passed as POST param. We **strongly advise** using HTTPS when you are performing requests that include your `api_key`. -*TODO* cleanup the referenced document +### Errors -### Table maps +Errors are reported using standard HTTP codes and extended information encoded in JSON with this format: -Table maps are maps associated with a table. -Configuration of such maps is limited to the CartoCSS style. +{% highlight javascript %} +{ + "errors": [ + "access forbidden to table TABLE" + ] +} +{% endhighlight %} - * style - the CartoCSS style for the datasource, can be an array - * style_version - version of the CartoCSS style, can be an array +If you use JSONP, the 200 HTTP code is always returned so the JavaScript client can receive errors from the JSON object. -You can only fetch PNG or UTF8GRID tiles from these maps. +### CORS support -Access method is the same as the one for [Inline maps](#inline-maps) +All the endpoints which might be accessed using a web browser add CORS headers and allow OPTIONS method. -# Endpoints description +## Anonymous Maps -- **/api/maps/** (same interface than https://github.com/CartoDB/Windshaft/wiki/Multilayer-API) -- **/api/maps/named** (same interface than https://github.com/CartoDB/Windshaft-cartodb/wiki/Template-maps) +Anonymous maps allows you to instantiate a map given SQL and CartoCSS. It also allows you to add interaction capabilities using [UTF Grid.](https://github.com/mapbox/utfgrid-spec) +### Instantiate -NOTE: in case Multilayer-API does not contain this info yet, the - endpoint for fetching attributes is this: +#### Definition -- **/api/maps/:map_id/:layer_index/attributes/:feature_id** - - would return { c: 1, d: 2 } + +{% highlight html %} +POST /api/v1/map +{% endhighlight %} +#### Params + +{% highlight javascript %} +{ + "version": "1.0.1", + "layers": [{ + "type": "cartodb", + "options": { + "cartocss_version": "2.1.1", + "cartocss": "#layer { polygon-fill: #FFF; }", + "sql": "select * from european_countries_e", + "interactivity": ["cartodb_id", "iso3"] + } + }] +} +{% endhighlight %} + +Should be a [Mapconfig](https://github.com/CartoDB/Windshaft/blob/0.19.1/doc/MapConfig-1.1.0.md). + +#### Response + +The response includes: + +- **layergroupid** + The ID for that map, used to compose the URL for the tiles. The final URL is: + + {% highlight html %} + http://{account}.cartodb.com/api/v1/map/:layergroupid/{z}/{x}/{y}.png + {% endhighlight %} + +- **updated_at** + The ISO date of the last time the data involved in the query was updated. + +- **metadata** *(optional)* + Includes information about the layers. Some layers may not have metadata. + +- **cdn_url** + URLs to fetch the data using the best CDN for your zone. + +#### Example + +