Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sblack-usu committed Nov 6, 2018
2 parents ad2cc1d + 865bb00 commit 0b0b08c
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 46 deletions.
30 changes: 29 additions & 1 deletion docs/index.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ To authenticate using HTTP Basic authentication, and then get system metadata fo
>>> for resource in hs.resources():
>>> print(resource)

To authenticate using HTTP Basic authentication, with input prompt for username and password:

>>> from hs_restclient import HydroShare
>>> hs = HydroShare()
>>> for resource in hs.resources():
>>> print(resource)

To authenticate using OAuth2 authentication (using a user and password supplied by the user), and then get a list of
resources you have access to:

Expand Down Expand Up @@ -320,7 +327,8 @@ To unzip a resource file or folder:
>>> hs = HydroShare(auth=auth)
>>> options = {
"zip_with_rel_path": "/source/path/file.zip",
"remove_original_zip": True
"remove_original_zip": True,
"overwrite": False
}
>>> result = hs.resource('ID OF RESOURCE GOES HERE').functions.unzip(options)

Expand Down Expand Up @@ -350,6 +358,26 @@ To upload files to a specific resource folder:
}
>>> result = hs.resource('ID OF RESOURCE GOES HERE').files(options)

To create a referenced content file:

>>> from hs_restclient import HydroShare, HydroShareAuthBasic
>>> auth = HydroShareAuthBasic(username='myusername', password='mypassword')
>>> hs = HydroShare(auth=auth)
>>> path = "data/contents"
>>> name = "file_name"
>>> ref_url = "https://www.hydroshare.org"
>>> response_json = hs.createReferencedFile('ID OF RESOURCE GOES HERE', path, name, ref_url)

To update a referenced content file:

>>> from hs_restclient import HydroShare, HydroShareAuthBasic
>>> auth = HydroShareAuthBasic(username='myusername', password='mypassword')
>>> hs = HydroShare(auth=auth)
>>> path = "data/contents"
>>> name = "file_name"
>>> ref_url = "https://www.cuahsi.org"
>>> response_json = hs.updateReferencedFile('ID OF RESOURCE GOES HERE', path, name, ref_url)

To set resource flags:

>>> from hs_restclient import HydroShare, HydroShareAuthBasic
Expand Down
69 changes: 68 additions & 1 deletion hs_restclient/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class HydroShare(object):
:param use_https: Boolean, if True, HTTPS will be used (HTTP cannot be used when auth is specified)
:param verify: Boolean, if True, security certificates will be verified
:param auth: Concrete instance of AbstractHydroShareAuth (e.g. HydroShareAuthBasic)
:param prompt_auth: Boolean, default True, prompts user/pass if no auth is given
:raises: HydroShareAuthenticationException if auth is not a known authentication type.
:raises: HydroShareAuthenticationException if auth is specified by use_https is False.
Expand All @@ -59,14 +60,20 @@ class HydroShare(object):


def __init__(self, hostname=DEFAULT_HOSTNAME, port=None, use_https=True, verify=True,
auth=None):
auth=None, prompt_auth=True):
self.hostname = hostname
self.verify = verify

self.session = None
self.auth = None
if auth:
self.auth = auth
elif prompt_auth:
import getpass
username = raw_input("Username: ").strip()
password = getpass.getpass("Password for {}: ".format(username))
auth = HydroShareAuthBasic(username=username, password=password)
self.auth = auth

if use_https:
self.scheme = 'https'
Expand Down Expand Up @@ -1075,6 +1082,66 @@ def deleteResourceFolder(self, pid, pathname):

return r.json()

def createReferencedFile(self, pid, path, name, ref_url):
"""Create a Referenced Content File (.url)
:param pid: The HydroShare ID of the resource for which the file should be created
:param path: Folder path for the file to be created in
:param name: Filename for the referenced file
:param ref_url: url to be used in the referenced file
:return: JsonResponse on success or HttpResponse with error status code on error
:raises: HydroShareNotAuthorized if user is not authorized to perform action.
:raises: HydroShareNotFound if the resource or resource file was not found.
:raises: HydroShareHTTPException if an unexpected HTTP response code is encountered.
"""

url = "{url_base}/resource/data-store-add-reference/".format(url_base=self.url_base)

data = {'res_id': pid, 'curr_path': path, 'ref_name': name, 'ref_url': ref_url}

r = self._request('POST', url, data=data)

if r.status_code != 200:
if r.status_code == 403:
raise HydroShareNotAuthorized(('POST', url))
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'POST', r.status_code))

return r.json()

def updateReferencedFile(self, pid, path, name, ref_url):
"""Update a Referenced Content File (.url)
:param pid: The HydroShare ID of the resource for which the file should be updated
:param path: Folder path for the file to be updated in
:param name: Filename for the referenced file
:param ref_url: url to be updated in the referenced file
:return: JsonResponse on success or HttpResponse with error status code on error
:raises: HydroShareNotAuthorized if user is not authorized to perform action.
:raises: HydroShareNotFound if the resource or resource file was not found.
:raises: HydroShareHTTPException if an unexpected HTTP response code is encountered.
"""

url = "{url_base}/resource/data_store_edit_reference_url/".format(url_base=self.url_base)

data = {'res_id': pid, 'curr_path': path, 'url_filename': name, 'new_ref_url': ref_url}

r = self._request('POST', url, data=data)

if r.status_code != 200:
if r.status_code == 403:
raise HydroShareNotAuthorized(('POST', url))
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'POST', r.status_code))

return r.json()

def getUserInfo(self):
"""
Query the GET /hsapi/userInfo/ REST end point of the HydroShare server.
Expand Down
12 changes: 7 additions & 5 deletions hs_restclient/endpoints/resources.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ def set_file_type(self, payload):
:param payload:
file_path: string (relative path of the file to be set to a specific file type)
hs_file_type: string (one of the supported files types: NetCDF, GeoRaster and GeoFeature)
hs_file_type: string (one of the supported files types: SingleFile, NetCDF, GeoRaster,
RefTimeseries, TimeSeries and GeoFeature)
:return: (object)
message: string
"""
Expand Down Expand Up @@ -286,7 +287,8 @@ def __init__(self, hs, **kwargs):
"""
Query the GET /hsapi/resource/ REST end point of the HydroShare server.
:param creator: Filter results by the HydroShare user name of resource creators
:param creator: DEPRECATED - use author
:param author: Filter results by the HydroShare user name of resource authors
:param owner: Filter results by the HydroShare user name of resource owners
:param user: Filter results by the HydroShare user name of resource users (i.e. owner, editor, viewer, public
resource)
Expand Down Expand Up @@ -319,7 +321,7 @@ def __init__(self, hs, **kwargs):
>>> for resource in hs.getResourceList():
>>>> print resource
{u'bag_url': u'http://www.hydroshare.org/static/media/bags/e62a438bec384087b6c00ddcd1b6475a.zip',
u'creator': u'B Miles',
u'author': u'B Miles',
u'date_created': u'05-05-2015',
u'date_last_updated': u'05-05-2015',
u'resource_id': u'e62a438bec384087b6c00ddcd1b6475a',
Expand All @@ -334,7 +336,7 @@ def __init__(self, hs, **kwargs):
u'science_metadata_url': u'http://www.hydroshare.org/hsapi/scimeta/e62a438bec384087b6c00ddcd1b6475a/',
u'public': True}
{u'bag_url': u'http://www.hydroshare.org/static/media/bags/hr3hy35y5ht4y54hhthrtg43w.zip',
u'creator': u'B Miles',
u'author': u'B Miles',
u'date_created': u'01-02-2015',
u'date_last_updated': u'05-13-2015',
u'resource_id': u'hr3hy35y5ht4y54hhthrtg43w',
Expand All @@ -355,7 +357,7 @@ def __init__(self, hs, **kwargs):
/hsapi/resourceList/?from_date=2015-05-03&to_date=2015-05-06
/hsapi/resourceList/?user=admin
/hsapi/resourceList/?owner=admin
/hsapi/resourceList/?creator=admin
/hsapi/resourceList/?author=admin
/hsapi/resourceList/?group=groupname
/hsapi/resourceList/?types=GenericResource&types=RasterResource
Expand Down
3 changes: 2 additions & 1 deletion hs_restclient/exceptions.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(self, args):
self.url = args[0]
self.method = args[1]
self.status_code = args[2]
self.status_msg = args[3]
if len(args) >= 4:
self.params = args[3]
else:
Expand All @@ -72,7 +73,7 @@ def __str__(self):
msg = "Received status {status_code} {status_msg} when accessing {url} " + \
"with method {method} and params {params}."
return msg.format(status_code=self.status_code,
status_msg=http_responses[self.status_code],
status_msg=http_responses[self.status_msg],
url=self.url,
method=self.method,
params=self.params)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='1.2.12',
version='1.3.0',

description='HydroShare REST API client library',
long_description=long_description,
Expand Down
10 changes: 10 additions & 0 deletions tests/mocks/hydroshare.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,13 @@ def resourceSetFileType_post(url, request):
# 404.
return response(404, {}, HEADERS, None, 5, request)
return response(202, content, HEADERS, None, 5, request)


@urlmatch(netloc=NETLOC, method=POST)
def resourceUpdateReferenceURL_post(url, request):
return response(200, '{"status": "success"}', HEADERS, None, 5, request)


@urlmatch(netloc=NETLOC, method=POST)
def resourceCreateReferenceURL_post(url, request):
return response(200, '{"status": "success"}', HEADERS, None, 5, request)
Loading

0 comments on commit 0b0b08c

Please sign in to comment.