API de datos de CKAN

Acceso ao recurso de datos mediante unha API web con servizo completo de consulta. Pode obter máis información na documentación principal da API de datos de CKAN e da extensión de «Almacén de datos» (DataStore).

Exemplos de código:

Obter 5 resultados que conteñan «jones» en calquera campo:
curl https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search \
  -H"Authorization:$API_TOKEN" -d '
{
  "resource_id": "b1dab96f-0e7b-45af-a418-d9229d25f120",
  "limit": 5,
  "q": "jones"
}'
const resp = await fetch(`https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search`, {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
        authorization: API_TOKEN
    },
    body: JSON.stringify({
        resource_id: 'b1dab96f-0e7b-45af-a418-d9229d25f120',
        limit: 5,
        q: 'jones'
    })
})
await resp.json()
$json = @'
{
  "resource_id": "b1dab96f-0e7b-45af-a418-d9229d25f120",
  "limit": 5,
  "q": "jones"
}
'@
$response = Invoke-RestMethod https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search`
  -Method Post -Body $json -Headers @{"Authorization"="$API_TOKEN"}
$response.result.records

(usando a biblioteca cliente ckanapi)

from ckanapi import RemoteCKAN

rc = RemoteCKAN('https://www.nasaharvest.xyz/gl_ES/', apikey=API_TOKEN)
result = rc.action.datastore_search(
    resource_id="b1dab96f-0e7b-45af-a418-d9229d25f120",
    limit=5,
    q="jones",
)
print(result['records'])
library(httr2)

req <- request("https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search")
result <- req %>% 
    req_headers(Authorization = API_TOKEN) %>% 
    req_body_json(list(
        resource_id = 'b1dab96f-0e7b-45af-a418-d9229d25f120',
        limit = 5,
        q = 'jones'))
    req_perform %>% 
    resp_body_json
Obter resultados con «conca hidrográfica» ou «enquisa» como asunto e «activo» como etapa:
curl https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search \
-H"Authorization:$API_TOKEN" -d '
{
"resource_id": "b1dab96f-0e7b-45af-a418-d9229d25f120",
  "filters": {
    "subject": ["watershed", "survey"],
    "stage": "active"
  }
}'
const resp = await fetch(`https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search`, {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
        authorization: API_TOKEN
    },
    body: JSON.stringify({resource_id: 'b1dab96f-0e7b-45af-a418-d9229d25f120', filters: {
        subject: ['watershed', 'survey'],
        stage: 'active'
    }})})
await resp.json()
$json = @'
{
  "resource_id": "b1dab96f-0e7b-45af-a418-d9229d25f120",
  "filters": {
    "subject": ["watershed", "survey"],
    "stage": "active"
  }
}
'@
$response = Invoke-RestMethod https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search`
  -Method Post -Body $json -Headers @{"Authorization"="$API_TOKEN"}
$response.result.records
from ckanapi import RemoteCKAN

rc = RemoteCKAN('https://www.nasaharvest.xyz/gl_ES/', apikey=API_TOKEN)
result = rc.action.datastore_search(
    resource_id="b1dab96f-0e7b-45af-a418-d9229d25f120",
    filters={
      "subject": ["watershed", "survey"],
      "stage": "active",
    },
)
print(result['records'])
library(httr2)

req <- request("https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search")
result <- req %>% 
    req_headers(Authorization = API_TOKEN) %>% 
    req_body_json(list(
        resource_id='b1dab96f-0e7b-45af-a418-d9229d25f120', 
        filters = list(
            subject = list("watershed", "survey"), 
            stage = "active")))
    req_perform %>% 
    resp_body_json
Exemplo de consulta SQL:
curl https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search_sql \
  -H"Authorization:$API_TOKEN" -d @- <<END
{
  "sql": "SELECT * FROM \"b1dab96f-0e7b-45af-a418-d9229d25f120\" WHERE title LIKE 'jones'"
}
END
const resp = await fetch(`https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search_sql`, {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
        authorization: API_TOKEN
    },
    body: JSON.stringify({
        sql: `SELECT * FROM "b1dab96f-0e7b-45af-a418-d9229d25f120" WHERE title LIKE 'jones'`
    })
})
await resp.json()
$json = @'
{
  "sql": "SELECT * from \"b1dab96f-0e7b-45af-a418-d9229d25f120\" WHERE title LIKE 'jones'"
}
'@
$response = Invoke-RestMethod https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search_sql`
  -Method Post -Body $json -Headers @{"Authorization"="$API_TOKEN"}
$response.result.records
from ckanapi import RemoteCKAN

rc = RemoteCKAN('https://www.nasaharvest.xyz/gl_ES/', apikey=API_TOKEN)
result = rc.action.datastore_search_sql(
    sql="""SELECT * from "b1dab96f-0e7b-45af-a418-d9229d25f120" WHERE title LIKE 'jones'"""
)
print(result['records'])
library(httr2)

req <- request("https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search_sql")
result <- req %>% 
    req_headers(Authorization = API_TOKEN) %>% 
    req_body_json(list(
        sql = "SELECT * FROM \"b1dab96f-0e7b-45af-a418-d9229d25f120\" WHERE title LIKE 'jones'"))
    req_perform %>% 
    resp_body_json

É posíbel acceder a algúns puntos de acceso á API mediante unha cadea de consulta GET.

Exemplo de consulta (primeiros 5 resultados)

https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search?resource_id=b1dab96f-0e7b-45af-a418-d9229d25f120&limit=5

Exemplo de consulta (resultados contendo «jones»)

https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search?resource_id=b1dab96f-0e7b-45af-a418-d9229d25f120&q=jones

Consulta exemplo (con sentencia SQL)

https://www.nasaharvest.xyz/gl_ES/api/action/datastore_search_sql?sql=SELECT * from "b1dab96f-0e7b-45af-a418-d9229d25f120" WHERE title LIKE 'jones'