hydroshare / hs_restclient

Python client for the https://www.hydroshare.org REST API
BSD 3-Clause "New" or "Revised" License
6 stars 8 forks source link

CreateResource documentation or debugging needed #62

Open ChristinaB opened 7 years ago

ChristinaB commented 7 years ago

@pkdash @Castronova Do you mind giving a bit more direction on how to create a new resource using the REST client? We will need to update all of the notebooks, and get other working notebooks going. It would be great to get a functional example beyond what is in the code (is that up to date) ?

Here is the error we get with version 1.2.6 followed by the previous working version. What is a 401 status code error? We also can't find that in the code.

We would like to add 'derived from' or to use the new format for adding metadata, did 'derived from' get deprecated? Thanks!

image

pkdash commented 7 years ago

@ChristinaB

What is a 401 status code error?

It means authentication failed - in this specific case user who is trying to create resource has not provided Hydroshare user account information when using rest client.

We would like to add 'derived from' or to use the new format for adding metadata, did 'derived from' get deprecated?

You can find the supported relation types here (seems we are not supporting 'derived from'): https://github.com/hydroshare/hydroshare/blob/develop/hs_core/models.py#L626

Do you mind giving a bit more direction on how to create a new resource using the REST client?

I think @Castronova should be able to give you some example code for creating a resource. If not I can. How soon you need it?

Castronova commented 7 years ago

@ChristinaB

It looks like you may have a few issues here:

1. you are calling createResource with a HydroShare instance that has not been authenticated with a username:

hsres = hsrc.HydroShare().createResource(rtype, title, abstract=abstract)

This is the same as:

hs = hsrc.HydroShare()
hsres = hs.createResource(rtype, title, abstract=abstract)

As @pkdash pointed out, the unauthenticated user does not have access to create a resources, so what you need to do instead is instantiate the rest client with user credentials (see https://github.com/hydroshare/hs_restclient):

from hs_restclient import HydroShare, HydroShareAuthBasic
auth = HydroShareAuthBasic(username='myusername', password='mypassword')
hs = HydroShare(auth=auth)
hsres = hs.createResource(rtype, title, abstract=abstract)

2. It seems that you are mixing up the hs_restclient and the jupyterhub hydroshare library (which uses the hs_restclient)

The following is a function defined by the jupyterhub hydroshare library which is a wrapper around the hs_restlclient.createResource to provide additional functionality:

resource_id = hs.createHydroShareResource(abstract, title, derivedFromId=None, keywords=keywords, resource_type='genericresource', content_files=files,public=False)

To learn more about this function, issue the following command inside your notebook:

help(hs.createHydroShareResource)

this tells us a little about the createHydroShareResource function

createHydroShareResource(abstract, title, derivedFromId, keywords=[], resource_type='GenericResource', content_files=[], public=False) method of utilities.hydroshare.hydroshare.hydroshare instance

This function takes 3 positional arguments (abstract, title, and derivedFromId) along with several optional parameters.