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 Mar 25, 2020
2 parents 3f91886 + 2f54771 commit fbfe73a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 53 deletions.
20 changes: 8 additions & 12 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ To get a listing of public resources::

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

To authenticate using HTTP Basic authentication, and then get a list of resources you have access to::

from hs_restclient import HydroShare, HydroShareAuthBasic
auth = HydroShareAuthBasic(username='myusername', password='mypassword')
hs = HydroShare(auth=auth)
for resource in hs.getResourceList():
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
Expand All @@ -47,11 +47,11 @@ resources you have access to::
hs = HydroShare(auth=auth)

try:
for resource in hs.getResourceList():
for resource in hs.resources():
print(resource)
except TokenExpiredError as e:
hs = HydroShare(auth=auth)
for resource in hs.getResourceList():
for resource in hs.resources():
print(resource)

Note that currently the client does not handle token renewal, hence the need to catch TokenExpiredError.
Expand Down Expand Up @@ -84,13 +84,13 @@ access to::
token=token)
try:
hs = HydroShare(auth=auth)
for resource in hs.getResourceList():
for resource in hs.resources():
print(resource)
except:
# get_token() is a stand in for how you get a new token on your system.
token = get_token()
hs = HydroShare(auth=auth)
for resource in hs.getResourceList():
for resource in hs.resources():
print(resource)

Note that currently the client does not handle token renewal, hence the need to catch TokenExpiredError.
Expand All @@ -99,14 +99,14 @@ To connect to a development HydroShare server that uses a self-sign security cer

from hs_restclient import HydroShare
hs = HydroShare(hostname='mydev.mydomain.net', verify=False)
for resource in hs.getResourceList():
for resource in hs.resources():
print(resource)

To connect to a development HydroShare server that is not running HTTPS::

from hs_restclient import HydroShare
hs = HydroShare(hostname='mydev.mydomain.net', port=8000, use_https=False)
for resource in hs.getResourceList():
for resource in hs.resources():
print(resource)

Note that authenticated connections **must** use HTTPS.
Expand All @@ -117,7 +117,3 @@ Documentation
-------------

Complete installation and usage documentation is available at http://hs-restclient.readthedocs.org/en/latest/




44 changes: 23 additions & 21 deletions hs_restclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

__title__ = 'hs_restclient'
__version__ = '1.3.5'
__version__ = '1.3.6'


import os
Expand Down Expand Up @@ -239,7 +239,7 @@ def getSystemMetadata(self, pid):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'GET', r.status_code))
raise HydroShareHTTPException(r)

return r.json()

Expand Down Expand Up @@ -354,7 +354,7 @@ def getScienceMetadataRDF(self, pid):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'GET', r.status_code))
raise HydroShareHTTPException(r)

return str(r.content)

Expand Down Expand Up @@ -418,7 +418,7 @@ def getScienceMetadata(self, pid):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'GET', r.status_code))
raise HydroShareHTTPException(r)

return r.json()

Expand Down Expand Up @@ -482,7 +482,7 @@ def updateScienceMetadata(self, pid, metadata):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'PUT', r.status_code, metadata))
raise HydroShareHTTPException(r)

return r.json()

Expand Down Expand Up @@ -561,7 +561,7 @@ def getResourceMap(self, pid):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'GET', r.status_code))
raise HydroShareHTTPException(r)

return str(r.content)

Expand Down Expand Up @@ -634,7 +634,7 @@ def _getBagStream(self, pid, wait_for_bag_creation):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((bag_url, 'GET', r.status_code))
raise HydroShareHTTPException(r)
elif r.headers['content-type'] == 'application/json':
# this is the case of bag being generated by hydroshare and not ready for download
content = json.loads(r.content.decode('utf-8'))
Expand Down Expand Up @@ -663,7 +663,9 @@ def _getTaskStatus(self, task_id):
task_status_url = "{url_base}/taskstatus/{task_id}/"
task_status_url = task_status_url.format(url_base=self.url_base, task_id=task_id)
r = self._request('GET', task_status_url)
response_data = json.loads(r.content.decode('utf-8'))
if r.content is None:
return False
response_data = r.json()
return response_data['status']

def getResourceTypes(self):
Expand All @@ -677,7 +679,7 @@ def getResourceTypes(self):

r = self._request('GET', url)
if r.status_code != 200:
raise HydroShareHTTPException((url, 'GET', r.status_code))
raise HydroShareHTTPException(r)

resource_types = r.json()
return set([t['resource_type'] for t in resource_types])
Expand Down Expand Up @@ -766,7 +768,7 @@ def createResource(self, resource_type, title, resource_file=None, resource_file
if r.status_code == 403:
raise HydroShareNotAuthorized(('POST', url))
else:
raise HydroShareHTTPException((url, 'POST', r.status_code, params))
raise HydroShareHTTPException(r)

response = r.json()
new_resource_id = response['resource_id']
Expand All @@ -789,7 +791,7 @@ def deleteResource(self, pid):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'DELETE', r.status_code))
raise HydroShareHTTPException(r)

def setAccessRules(self, pid, public=False):
"""
Expand All @@ -809,7 +811,7 @@ def setAccessRules(self, pid, public=False):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'PUT', r.status_code, params))
raise HydroShareHTTPException(r)

resource = r.json()
assert(resource['resource_id'] == pid)
Expand Down Expand Up @@ -859,7 +861,7 @@ def addResourceFile(self, pid, resource_file, resource_filename=None, progress_c
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'POST', r.status_code))
raise HydroShareHTTPException(r)

response = r.json()
# assert(response['resource_id'] == pid)
Expand Down Expand Up @@ -898,7 +900,7 @@ def getResourceFile(self, pid, filename, destination=None):
elif r.status_code == 404:
raise HydroShareNotFound((pid, filename))
else:
raise HydroShareHTTPException((url, 'GET', r.status_code))
raise HydroShareHTTPException(r)

if destination is None:
return r.iter_content(STREAM_CHUNK_SIZE)
Expand Down Expand Up @@ -934,7 +936,7 @@ def deleteResourceFile(self, pid, filename):
elif r.status_code == 404:
raise HydroShareNotFound((pid, filename))
else:
raise HydroShareHTTPException((url, 'DELETE', r.status_code))
raise HydroShareHTTPException(r)

response = r.json()
assert(response['resource_id'] == pid)
Expand Down Expand Up @@ -1019,7 +1021,7 @@ def getResourceFolderContents(self, pid, pathname):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'GET', r.status_code))
raise HydroShareHTTPException(r)

return r.json()

Expand Down Expand Up @@ -1050,7 +1052,7 @@ def createResourceFolder(self, pid, pathname):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'PUT', r.status_code))
raise HydroShareHTTPException(r)

return r.json()

Expand Down Expand Up @@ -1081,7 +1083,7 @@ def deleteResourceFolder(self, pid, pathname):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'DELETE', r.status_code))
raise HydroShareHTTPException(r)

return r.json()

Expand Down Expand Up @@ -1125,7 +1127,7 @@ def createReferencedFile(self, pid, path, name, ref_url, validate):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'POST', r.status_code))
raise HydroShareHTTPException(r)

return r.json()

Expand Down Expand Up @@ -1170,7 +1172,7 @@ def updateReferencedFile(self, pid, path, name, ref_url):
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'POST', r.status_code))
raise HydroShareHTTPException(r)

return r.json()

Expand All @@ -1193,7 +1195,7 @@ def getUserInfo(self):

r = self._request('GET', url)
if r.status_code != 200:
raise HydroShareHTTPException((url, 'GET', r.status_code))
raise HydroShareHTTPException(r)

return r.json()

Expand Down
31 changes: 12 additions & 19 deletions hs_restclient/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from .compat import http_responses

class HydroShareException(Exception):
def __init__(self, args):
super(HydroShareException, self).__init__(args)
Expand All @@ -26,7 +24,7 @@ def __str__(self):
return msg.format(method=self.method, url=self.url)

def __unicode__(self):
return unicode(str(self))
return str(self)


class HydroShareNotFound(HydroShareException):
Expand All @@ -48,7 +46,7 @@ def __str__(self):
return msg

def __unicode__(self):
return unicode(str(self))
return str(self)


class HydroShareHTTPException(HydroShareException):
Expand All @@ -58,28 +56,23 @@ class HydroShareHTTPException(HydroShareException):
url and status_code are of type string, while the optional params argument
should be a dict.
"""
def __init__(self, args):
super(HydroShareHTTPException, self).__init__(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:
self.params = None
def __init__(self, response):
super(HydroShareHTTPException, self).__init__(response)
self.url = response.request.url
self.method = response.request.method
self.status_code = response.status_code
self.status_msg = response.text if response.text else "No status message"

def __str__(self):
msg = "Received status {status_code} {status_msg} when accessing {url} " + \
"with method {method} and params {params}."
"with method {method}."
return msg.format(status_code=self.status_code,
status_msg=http_responses[self.status_msg],
status_msg=self.status_msg,
url=self.url,
method=self.method,
params=self.params)
method=self.method)

def __unicode__(self):
return unicode(str(self))
return str(self)


class HydroShareAuthenticationException(HydroShareException):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
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.3.5',
version='1.3.6',

description='HydroShare REST API client library',
long_description=long_description,
Expand Down

0 comments on commit fbfe73a

Please sign in to comment.