title: Creating a CKAN package/dataset with resources using ckanapi and Python
body: <p>CKAN provides the <a href="https://github.com/ckan/ckanapi#file-uploads" rel="nofollow noreferrer"><code>ckanapi</code></a> package for accessing <a href="http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.package_create" rel="nofollow noreferrer">the CKAN API</a> via Python or the command line.</p>
  
  <p>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.)</p>
  
  <p>Internally, <code>ckanapi</code> <a href="https://github.com/ckan/ckanapi/blob/master/ckanapi/common.py#L39" rel="nofollow noreferrer">scans all keys moving any file-like parameters into a separate <code>dict</code></a>, which it <a href="https://github.com/ckan/ckanapi/blob/master/ckanapi/remoteckan.py#L86" rel="nofollow noreferrer">passes to the <code>requests.session.post(files=..)</code> parameter</a>.</p>
  
  <p>This is the closest I can get but CKAN returns an HTTP 500 error (copied from <a href="http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file" rel="nofollow noreferrer">this guide to <code>requests</code></a>):</p>
  
  <pre><code>with ckanapi.RemoteCKAN('http://myckan.example.com', apikey='real-key', user_agent=ua, username='joe', password='pwd') as ckan:
      ckan.action.package_create(name='joe_data',
                                 resources=('report.xls',
                                            open('/path/to/file.xlsx', 'rb'),
                                            'application/vnd.ms-excel',
                                            {'Expires': '0'}))
  </code></pre>
  
  <p>I've also tried <code>resources=open('path/file')</code>, <code>files=open('file')</code>, shorter or longer tuples, but get the same 500 error.</p>
  
  <p>The <code>requests</code> documentation says:</p>
  
  <pre><code>:param files: (optional) Dictionary of ``'filename': file-like-objects``
      for multipart encoding upload.
  </code></pre>
  
  <p>I can't pass <code>ckanapi</code> <code>resources={'filename': open('file')}</code> as <code>ckanapi</code> doesn't detect the file, attempts to pass it to <code>requests</code> as a normal parameter, and fails ("BufferedReader is not JSON serializable" as it attempts to make the file a <code>POST</code> parameter). I get the same if I try to pass a list of files. But <a href="http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.package_create" rel="nofollow noreferrer">the API is able to</a> create a package and add a number of resources in a single call.</p>
  
  <p>So how do I create a package and multiple resources with a single <code>ckanapi</code> call?</p>