ckan / ckanapi

A command line interface and Python module for accessing the CKAN Action API
Other
176 stars 74 forks source link

datastore_create TypeError for file in resource dict #147

Closed CameronTStark closed 4 years ago

CameronTStark commented 4 years ago

The function ckanext.datastore.logic.action.datastore_create gives the ability to pass a dict to its resource parameter with the parameters from ckan.logic.action.create.resource_create however I get an error when passing an open file as a _io.BufferedReader type in the resource_create dict.

Here's an example of my code.

package_id = '01abcdef-0123-4567-0123456789abc'
name = "data name"
description = "new extra shiny data"
format = "CSV"
primary_key = "datetime"                             
aliases = ["my_alias"]
**with open("data.csv", "rb") as f:**
    resource_create_dict = dict(
        package_id=package_id,
        format=format,
        name=name, 
        description=description,
        **upload=f**
    )
    ckan.action.datastore_create(resource=resource_create_dict, primary_key=primary_key)

The error I get is: TypeError: Object of type BufferedReader is not JSON serializable

I believe this is due to the different types of data within the dict but I imagine it's possible if the function is expecting this possibility.

Is there a way to prepare the file for the dictionary so I don't get this issue? I'm not fluent in the serialization methods and needs for POSTing through the API but maybe someone here is.

Thanks!

wardi commented 4 years ago

That's not how the datastore_create API call works. The ckan API only accepts files at the top level because it uses multipart form data to attach files, but only the resource_create API call accepts files for upload. Please look at the API documentation for more information. https://docs.ckan.org/en/2.8/maintaining/datastore.html#ckanext.datastore.logic.action.datastore_create