Skip to content

Instantly share code, notes, and snippets.

@edigiacomo
Last active September 13, 2021 11:36
Show Gist options
  • Select an option

  • Save edigiacomo/e75df58cf8140b622b74 to your computer and use it in GitHub Desktop.

Select an option

Save edigiacomo/e75df58cf8140b622b74 to your computer and use it in GitHub Desktop.
Simple example of upserting records in a CKAN Datastore
import requests # http://docs.python-requests.org/en/latest/
import json
def upsert_records(records, ckan_url, resource_id, ckan_api_key=None):
"""Send a list of records to CKAN Datastore and return a requests.Response
object (see http://docs.python-requests.org/en/latest/api/#requests.Response).
Example:
url = "http://mysite.org/ckan"
resource_id = "7d33cafe-0118-4b9a-8d8d-e82d1935e2ea"
api_key = "5b647d18-3a64-4b6f-8b80-fa412ba3f5d9"
upsert_records([{
"name": "Emanuele",
"surname": "Di Giacomo",
}, {
"name": "Ciccio",
"surname": "Riccio",
}], url, resource_id, api_key)
"""
u = ckan_url + "/api/action/datastore_upsert"
r = requests.post(u, headers={
"X-CKAN-API-Key": ckan_api_key,
"Accept": "application/json",
'Content-Type': 'application/json',
}, data=json.dumps({
"resource_id": resource_id,
"method": "upsert",
"records": records,
}))
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment