Skip to content

Conversation

wardi
Copy link
Contributor

@wardi wardi commented May 8, 2023

Fixes #7573

Proposed fixes:

Improve CKAN Data API dialog with syntax highlighting, multiple client languages and jinja2 blocks for expansion

Features:

ckan-data-api

  • includes tests covering changes
  • includes updated documentation
  • includes user-visible changes
  • includes API changes
  • includes bugfix for possible backport

@wardi wardi changed the title 7573 data api extensions Data API dialog Improvements May 8, 2023
@smotornyuk
Copy link
Member

smotornyuk commented May 10, 2023

JS examples, without jQuery. These examples can be executed:

  • in the browser's JS console
  • in nodeJS interpreter starting from v18
  • in pre-v18 nodeJS interpreter if node-fetch polyfill installed(npm i node-fetch) and imported into script(fetch = require("node-fetch"))
  • in normal JS script. But it has to be wrapped into an async function(async function run() { <EXAMPLE> }), because, the same as Python, JS can execute asynchronous code only inside the event loop. browser's JS console and nodeJS interpreter are designed for debugging and have implicit scheduling and execution of async code. That's why they don't need such a wrapper. I suspect that people who are using JS examples have some minimal JS knowledge, so I haven't included wrapper into examples.
const params = new URLSearchParams({resource_id: '123-123-123-123'})
const resp = await fetch(
    `http://0.0.0.0:5010/api/action/datastore_search?${params}`, {
        headers: {authorization: '<API_TOKEN>'}
    }
)
await resp.json()
const resp = await fetch(`http://0.0.0.0:5010/api/action/datastore_search`, {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
        authorization: '<API_TOKEN>'
    },
    body: JSON.stringify({resource_id: '123-123-123-123', filters: {
        subject: ['watershed', 'survey'],
        stage: 'active'
    }})
})
await resp.json()
const resp = await fetch(`http://0.0.0.0:5010/api/action/datastore_search_sql`, {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
        authorization: '<API_TOKEN>'
    },
    body: JSON.stringify({sql: `SELECT * from "123-123-123-123" WHERE title LIKE 'jones'`})
})
await resp.json()

Do we want an R example? I suspect that datastore must be especially popular among R users

library(httr2)

req <- request("http://0.0.0.0:5010/api/action/")
result <- req %>% 
    req_url_path_append('datastore_search') %>% 
    req_headers(Authorization = '<API_TOKEN>') %>% 
    req_url_query(resource_id = '123-123-123-123', limit = 5, q = 'jones') %>% 
    req_perform %>% 
    resp_body_json
library(httr2)

req <- request("http://0.0.0.0:5010/api/action/")
result <- req %>% 
    req_url_path_append('datastore_search') %>% 
    req_headers(Authorization = '<API_TOKEN>') %>% 
    req_body_json(list(
        resource_id='_table_metadata', 
        filters = list(
            subject = list("watershed", "survey"), 
            stage = "active"))) %>% 
    req_perform %>% 
    resp_body_json
library(httr2)

req <- request("http://0.0.0.0:5010/api/action/")
result <- req %>% 
    req_url_path_append('datastore_search_sql') %>%
    req_headers(Authorization = '<API_TOKEN>') %>% 
    req_body_json(list(sql="SELECT * from \"123-123-123-123\" WHERE title LIKE 'jones'")) %>% 
    req_perform %>% 
    resp_body_json

@wardi
Copy link
Contributor Author

wardi commented May 10, 2023

very nice. Thank you @smotornyuk

@wardi
Copy link
Contributor Author

wardi commented May 10, 2023

@smotornyuk I've added your JS and R examples and found an.. interesting.. way of separating the code examples into their own files with jinja2 to hopefully make this easier to maintain and add languages in the future

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CKAN Data API extensions
3 participants