[ckan-dev] Example for API usage (e.g. member_create)
Heinrich Widmann
widmann at dkrz.de
Mon Apr 15 09:08:26 UTC 2013
Hi Vitor,
thank you again for your help !
Ok, now I can add datasets to a group.
But still I have problems with using CKAN APIs, especially with :
- removing datasets _really_ from CKAN data base : I know, that I can
set the state of a dataset to 'deleted',
but I want really remove it from CKAN, that it is not longer seen
anywhere ...
- update datasets : in our current approach we use the "extras" fields
to cover some specific meta data keys we need.
I mangaged to to create datasets (as well with the "extras"), but I
never get an existing dataset updated, i.e. overwrite
or add further key-value pairs.
For shure there are better approaches, but I implement it as simple
(POST or PUT) http requests.
Maybe it helps if I send you the corresponding python code :
................
def call_ckan(paramdict,apipath,type,jsondata,text):
print '\t|-- Calling %s at %s' % (text,apipath)
# Use the json module to dump the dictionary to a string for
posting.
param_string = urllib.quote(json.dumps(paramdict))
if ( type == 'PUT'): # PUT requests, e.g. update a data set
request = urllib2.Request(apipath,str(jsondata))
request.add_header('Authorization', api_key)
response = urllib2.urlopen(request,param_string)
return response.read()
else: # POST request, e.g. create a dataset
request = urllib2.Request(url=apipath,data=param_string, \
headers={'Content-type': 'application/json',
'Authorization': api_key, 'X-CKAN-API-Key': api_key })
response = urllib2.urlopen(request)
return response.read()
...............
api_url = "http://{host}/api/rest".format(host=ip_host) ## maybe
it's better to use api/3 ???
dsupdate_url = "{apiurl}/package_update".format(apiurl=api_action)
dscreate_url = "{apiurl}/package_create".format(apiurl=api_action)
## the "default" meta data, dataset is created with.
dataset_dict = {
'name': ds,
'title': jsondata["title"],
'notes': jsondata["notes"].replace('\t',''),
}
try:
call_ckan(dataset_dict,dscreate_url,'PUT','','CREATE') ##
create data set (only with "default" meta data)
..........
try:
dsid=call_ckan(objectJSONEncoder().encode(jsondata),dsupdate_url,'PUT','','UPDATE')
## update dataset with full set of meta data (encode as json dict.)
.............
For shure there are a lot of bugs in my code and for shure there are
much better implementations ...
Anyway it would be nice to have examples of simple "ready-to-run"
implementations (in python or curl commands or ...) of API usage
at least for dummies like me and for the basic actions as package_create
or package_delete (real remove !!).
Cheers,
Heinrich
Am 11.04.2013 18:01, schrieb Vitor Baptista:
> Hi Heinrich,
>
> 2013/4/11 Heinrich Widmann <widmann at dkrz.de <mailto:widmann at dkrz.de>>
>
> I checked that you can use the /group name/ for "id", but only
> the/id/ (of package or dataset) for "object" - even more confusing
> :-) .
>
>
> Yes, indeed. I've just opened a pull request that fixes this, both for
> member_create and member_delete in https://github.com/okfn/ckan/pull/754.
>
> (Actually : What's the difference between a package and a dataset
> in CKAN ?)
>
>
> They're the same. It's just because datasets used to be called
> packages, so there're many places in the code that they're still
> called like this.
>
>> The "capacity" value needs to be one of "public"
>> or "private" when adding a package to a group.
>>
> Actually at least for create_member as well "member" works .
>
>
> There's no validation on the capacity value yet. But, for packages, as
> far as I could see, the only recognized values are public and private.
>
> Cheers,
> Vítor Baptista.
>
>
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> http://lists.okfn.org/mailman/listinfo/ckan-dev
> Unsubscribe: http://lists.okfn.org/mailman/options/ckan-dev
--
-----------------------------\\---------------------------------------
Heinrich Widmann \\ Deutsches Klimarechenzentrum GmbH
Phone: +49 40 41173 282 \\ Abteilung Datenmanagement
FAX: +49 40 41173 476 \\ Bundesstr. 45a
Email: widmann at dkrz.de \\ D-20146 Hamburg
http://www.dkrz.de \\ Germany
-----------------------------------\\---------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20130415/d3f97bde/attachment-0001.html>
More information about the ckan-dev
mailing list