Creating a CKAN package/dataset with resources using ckanapi and Python

Multi tool use
CKAN provides the ckanapi
package for accessing the CKAN API via Python or the command line.
I can use it to download metadata, create resources, etc. But I can't create a package and upload resources to it in a single API call. (A package is also referred to as a dataset.)
Internally, ckanapi
scans all keys moving any file-like parameters into a separate dict
, which it passes to the requests.session.post(files=..)
parameter.
This is the closest I can get but CKAN returns an HTTP 500 error (copied from this guide to requests
):
I've also tried resources=open('path/file')
, files=open('file')
, shorter or longer tuples, but get the same 500 error.
The requests
documentation says:
I can't pass ckanapi
resources={'filename': open('file')}
as ckanapi
doesn't detect the file, attempts to pass it to requests
as a normal parameter, and fails ("BufferedReader is not JSON serializable" as it attempts to make the file a POST
parameter). I get the same if I try to pass a list of files. But the API is able to create a package and add a number of resources in a single call.
So how do I create a package and multiple resources with a single ckanapi
call?
I was curious about this and thought I'd put something together to test it. Unfortunately I haven't played with the CLI you mentioned. But I hope this will help you and others stumbling across this.
I am not positive but I'm guessing your resource dict isn't formatted properly. The resources needs to be a list of dictionaries.
Here's a ruby script to do the single api call insert (my preferred language at this time):
And here's a plain python script to do the same thing (thank you CKAN docs for this template I modified)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.