Skip to content

Commit

Permalink
updated with develop for release 1.2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdash committed Dec 7, 2016
2 parents 13846de + 8ebc4f0 commit b560fad
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 6 deletions.
5 changes: 5 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.2.5 - 12/7/2016
- Add ability to supply key/value metadata (in addition to 'title', 'abstract', 'keywords', and 'metadata')
to createResource()
- Add getResourceMap (/hsapi/resource/\<pid\>map/)

# 1.2.4 - 9/2/2016
- Add support for async resource bag (BagIt archive) creation prior to downloading
the bag file in getResource()
Expand Down
15 changes: 14 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ To create a resource:
>>> rtype = 'GenericResource'
>>> fpath = '/path/to/a/file'
>>> metadata = '[{"coverage":{"type":"period", "value":{"start":"01/01/2000", "end":"12/12/2010"}}}, {"creator":{"name":"John Smith"}}, {"creator":{"name":"Lisa Miller"}}]'
>>> resource_id = hs.createResource(rtype, title, resource_file=fpath, keywords=keywords, abstract=abstract, metadata=metadata)
>>> extra_metadata = '{"key-1": "value-1", "key-2": "value-2"}'
>>> resource_id = hs.createResource(rtype, title, resource_file=fpath, keywords=keywords, abstract=abstract, metadata=metadata, extra_metadata=extra_metadata)

To make a resource public:

Expand Down Expand Up @@ -216,7 +217,19 @@ To delete a file from a resource:
>>> fname = 'somefile.txt'
>>> resource_id = hs.deleteResourceFile('ID OF RESOURCE GOES HERE', fname)

To get resource map xml data for a resource:

>>> from hs_restclient import HydroShare, HydroShareAuthBasic
>>> auth = HydroShareAuthBasic(username='myusername', password='mypassword')
>>> hs = HydroShare(auth=auth)
>>> resource_map_xml = hs.getResourceMap('ID OF RESOURCE GOES HERE')

To get science xml data for a resource:

>>> from hs_restclient import HydroShare, HydroShareAuthBasic
>>> auth = HydroShareAuthBasic(username='myusername', password='mypassword')
>>> hs = HydroShare(auth=auth)
>>> science_metadata_xml = hs.getScienceMetadata('ID OF RESOURCE GOES HERE')

Index
-----
Expand Down
89 changes: 86 additions & 3 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.2.4'
__version__ = '1.2.5'


import os
Expand Down Expand Up @@ -472,6 +472,85 @@ def getScienceMetadata(self, pid):

return str(r.content)

def getResourceMap(self, pid):
""" Get resource map metadata for a resource
:param pid: The HydroShare ID of the resource
:raises: HydroShareNotAuthorized if the user is not authorized to view the metadata.
:raises: HydroShareNotFound if the resource was not found.
:raises: HydroShareHTTPException to signal an HTTP error.
:return: A string representing the XML+RDF serialization of resource map metadata.
Example of data returned:
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:citoterms="http://purl.org/spar/cito/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:ore="http://www.openarchives.org/ore/terms/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs1="http://www.w3.org/2001/01/rdf-schema#"
>
<rdf:Description rdf:about="http://foresite-toolkit.googlecode.com/#pythonAgent">
<foaf:name>Foresite Toolkit (Python)</foaf:name>
<foaf:mbox>foresite@googlegroups.com</foaf:mbox>
</rdf:Description>
<rdf:Description rdf:about="http://www.hydroshare.org/terms/GenericResource">
<rdfs1:label>Generic</rdfs1:label>
<rdfs1:isDefinedBy>http://www.hydroshare.org/terms</rdfs1:isDefinedBy>
</rdf:Description>
<rdf:Description rdf:about="http://www.hydroshare.org/resource/03c862ccf17b4853ac4288d63ab2d766/data/contents/test.txt">
<ore:isAggregatedBy>http://www.hydroshare.org/resource/03c862ccf17b4853ac4288d63ab2d766/data/resourcemap.xml#aggregation</ore:isAggregatedBy>
<dc:format>text/plain</dc:format>
</rdf:Description>
<rdf:Description rdf:about="http://www.hydroshare.org/resource/03c862ccf17b4853ac4288d63ab2d766/data/resourcemap.xml">
<dc:identifier>03c862ccf17b4853ac4288d63ab2d766</dc:identifier>
<ore:describes rdf:resource="http://www.hydroshare.org/resource/03c862ccf17b4853ac4288d63ab2d766/data/resourcemap.xml#aggregation"/>
<rdf:type rdf:resource="http://www.openarchives.org/ore/terms/ResourceMap"/>
<dc:format>application/rdf+xml</dc:format>
<dc:creator rdf:resource="http://foresite-toolkit.googlecode.com/#pythonAgent"/>
<dcterms:created>2016-11-29T16:17:52Z</dcterms:created>
<dcterms:modified>2016-11-29T16:17:52Z</dcterms:modified>
</rdf:Description>
<rdf:Description rdf:about="http://www.openarchives.org/ore/terms/ResourceMap">
<rdfs1:isDefinedBy>http://www.openarchives.org/ore/terms/</rdfs1:isDefinedBy>
<rdfs1:label>ResourceMap</rdfs1:label>
</rdf:Description>
<rdf:Description rdf:about="http://www.hydroshare.org/resource/03c862ccf17b4853ac4288d63ab2d766/data/resourcemap.xml#aggregation">
<citoterms:isDocumentedBy>http://www.hydroshare.org/resource/03c862ccf17b4853ac4288d63ab2d766/data/resourcemetadata.xml</citoterms:isDocumentedBy>
<dc:title>Gen Res-1</dc:title>
<dcterms:type rdf:resource="http://www.hydroshare.org/terms/GenericResource"/>
<ore:isDescribedBy>http://www.hydroshare.org/resource/03c862ccf17b4853ac4288d63ab2d766/data/resourcemap.xml</ore:isDescribedBy>
<ore:aggregates rdf:resource="http://www.hydroshare.org/resource/03c862ccf17b4853ac4288d63ab2d766/data/resourcemetadata.xml"/>
<ore:aggregates rdf:resource="http://www.hydroshare.org/resource/03c862ccf17b4853ac4288d63ab2d766/data/contents/test.txt"/>
<rdf:type rdf:resource="http://www.openarchives.org/ore/terms/Aggregation"/>
</rdf:Description>
<rdf:Description rdf:about="http://www.hydroshare.org/resource/03c862ccf17b4853ac4288d63ab2d766/data/resourcemetadata.xml">
<ore:isAggregatedBy>http://www.hydroshare.org/resource/03c862ccf17b4853ac4288d63ab2d766/data/resourcemap.xml#aggregation</ore:isAggregatedBy>
<citoterms:documents>http://www.hydroshare.org/resource/03c862ccf17b4853ac4288d63ab2d766/data/resourcemap.xml#aggregation</citoterms:documents>
<dc:title>Dublin Core science metadata document describing the HydroShare resource</dc:title>
<dc:format>application/rdf+xml</dc:format>
</rdf:Description>
<rdf:Description rdf:about="http://www.openarchives.org/ore/terms/Aggregation">
<rdfs1:label>Aggregation</rdfs1:label>
<rdfs1:isDefinedBy>http://www.openarchives.org/ore/terms/</rdfs1:isDefinedBy>
</rdf:Description>
</rdf:RDF>
"""
url = "{url_base}/resource/{pid}/map/".format(url_base=self.url_base,
pid=pid)
r = self._request('GET', url)
if r.status_code != 200:
if r.status_code == 403:
raise HydroShareNotAuthorized(('GET', url))
elif r.status_code == 404:
raise HydroShareNotFound((pid,))
else:
raise HydroShareHTTPException((url, 'GET', r.status_code))

return str(r.content)

def getResource(self, pid, destination=None, unzip=False, wait_for_bag_creation=True):
""" Get a resource in BagIt format
Expand Down Expand Up @@ -592,7 +671,7 @@ def getResourceTypes(self):
def createResource(self, resource_type, title, resource_file=None, resource_filename=None,
abstract=None, keywords=None,
edit_users=None, view_users=None, edit_groups=None, view_groups=None,
metadata=None, progress_callback=None):
metadata=None, extra_metadata=None, progress_callback=None):
""" Create a new resource.
:param resource_type: string representing the a HydroShare resource type recognized by this
Expand All @@ -611,6 +690,7 @@ def createResource(self, resource_type, title, resource_file=None, resource_file
:param edit_groups: list of HydroShare group names that will be given edit permissions
:param view_groups: list of HydroShare group names that will be given view permissions
:param metadata: json string data for each of the metadata elements
:param extra_metadata: json string data for key/value pair metadata elements defined by user
:param progress_callback: user-defined function to provide feedback to the user about the progress
of the upload of resource_file. For more information, see:
http://toolbelt.readthedocs.org/en/latest/uploading-data.html#monitoring-your-streaming-multipart-upload
Expand All @@ -626,7 +706,7 @@ def createResource(self, resource_type, title, resource_file=None, resource_file

close_fd = False

if not resource_type in self.resource_types:
if resource_type not in self.resource_types:
raise HydroShareArgumentException("Resource type {0} is not among known resources: {1}".format(resource_type,
", ".join([r for r in self.resource_types])))

Expand All @@ -651,6 +731,9 @@ def createResource(self, resource_type, title, resource_file=None, resource_file
if metadata:
params['metadata'] = metadata

if extra_metadata:
params['extra_metadata'] = extra_metadata

if resource_file:
close_fd = self._prepareFileForUpload(params, resource_file, resource_filename)

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.2.4',
version='1.2.5',

description='HydroShare REST API client library',
long_description=long_description,
Expand Down
12 changes: 12 additions & 0 deletions tests/mocks/hydroshare.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ def scimeta_get(url, request):
return response(404, {}, HEADERS, None, 5, request)
return response(200, content, HEADERS, None, 5, request)

@urlmatch(netloc=NETLOC, method=GET)
def resourcemap_get(url, request):
file_path = url.netloc + url.path + 'resourcemap.xml'

try:
content = Resource(file_path).get()
except EnvironmentError:
# catch any environment errors (i.e. file does not exist) and return a
# 404.
return response(404, {}, HEADERS, None, 5, request)
return response(200, content, HEADERS, None, 5, request)

@urlmatch(netloc=NETLOC, method=GET)
def resourceFileList_get(url, request):
if url.query == '':
Expand Down
13 changes: 12 additions & 1 deletion tests/test_hs_restclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ def test_create_get_delete_resource(self):
{'creator': {'name': 'John Smith'}},
{'contributor': {'name': 'Lisa Miller'}}])

extra_metadata = json.dumps({'latitude': '40', 'longitude': '-111'})

with HTTMock(mocks.hydroshare.createResourceCRUD):
# Create
newres = hs.createResource(rtype, title, resource_file=fname, keywords=keywords,
abstract=abstract, metadata=metadata)
abstract=abstract, metadata=metadata, extra_metadata=extra_metadata)
self.assertIsNotNone(newres)
sysmeta = hs.getSystemMetadata(newres)
self.assertEqual(sysmeta['resource_id'], newres)
Expand Down Expand Up @@ -193,6 +195,15 @@ def test_get_scimeta(self):
self.assertTrue(scimeta.find("""<rdf:Description rdf:about="http://www.hydroshare.org/resource/6dbb0dfb8f3a498881e4de428cb1587c">""") != -1)


class TestGetResourceMap(unittest.TestCase):

@with_httmock(mocks.hydroshare.resourcemap_get)
def test_get_resourcemap(self):
hs = HydroShare()
resourcemap = hs.getResourceMap('6dbb0dfb8f3a498881e4de428cb1587c')
self.assertTrue(resourcemap.find("""<rdf:Description rdf:about="http://www.hydroshare.org/resource/6dbb0dfb8f3a498881e4de428cb1587c">""") != -1)


class TestGetResourceFileList(unittest.TestCase):

def setUp(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF PUBLIC "-//DUBLIN CORE//DCMES DTD 2002/07/31//EN"
"http://dublincore.org/documents/2002/07/31/dcmes-xml/dcmes-xml-dtd.dtd">
<rdf:RDF xmlns:dcterms="http://purl.org/dc/terms/" xmlns:hsterms="http://hydroshare.org/terms/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs1="http://www.w3.org/2001/01/rdf-schema#" xmlns:dc="http://purl.org/dc/elements/1.1/">
<rdf:Description rdf:about="http://www.hydroshare.org/resource/6dbb0dfb8f3a498881e4de428cb1587c">
<dc:title>RHESSys model of Dead Run 5 watershed, Baltimore County, Maryland, USA (with rain gardens)</dc:title>
<dc:type rdf:resource="http://www.hydroshare.org/terms/GenericResource"/>
<dc:description>
<rdf:Description>
<dcterms:abstract>3-m spatial resolution RHESSys model for Dead Run 5 watershed in Baltimore County, Maryland. This model contains example implementation of rain gardens.</dcterms:abstract>
</rdf:Description>
</dc:description>
<dc:creator>
<rdf:Description rdf:about="http://www.hydroshare.org/user/28/">
<hsterms:name>Brian Miles</hsterms:name>
<hsterms:creatorOrder>1</hsterms:creatorOrder>
<hsterms:email>brian_miles@unc.edu</hsterms:email>
</rdf:Description>
</dc:creator>
<dc:date>
<dcterms:created>
<rdf:value>2015-07-27T18:35:27.954135+00:00</rdf:value>
</dcterms:created>
</dc:date>
<dc:date>
<dcterms:modified>
<rdf:value>2015-08-07T13:44:44.757870+00:00</rdf:value>
</dcterms:modified>
</dc:date>
<dc:format>application/zip</dc:format>
<dc:identifier>
<rdf:Description>
<hsterms:hydroShareIdentifier>http://www.hydroshare.org/resource/6dbb0dfb8f3a498881e4de428cb1587c</hsterms:hydroShareIdentifier>
</rdf:Description>
</dc:identifier>
<dc:language>eng</dc:language>
<dc:rights>
<rdf:Description>
<hsterms:rightsStatement>This resource is shared under the Creative Commons Attribution CC BY.</hsterms:rightsStatement>
<hsterms:URL rdf:resource="http://creativecommons.org/licenses/by/4.0/"/>
</rdf:Description>
</dc:rights>
<dc:subject>RHESSys</dc:subject>
<dc:subject>Baltimore Ecosystem Study</dc:subject>
<dc:subject>green infrastructure</dc:subject>
</rdf:Description>
<rdf:Description rdf:about="http://www.hydroshare.org/terms/GenericResource">
<rdfs1:label>Generic</rdfs1:label>
<rdfs1:isDefinedBy>http://www.hydroshare.org/terms</rdfs1:isDefinedBy>
</rdf:Description>
</rdf:RDF>

0 comments on commit b560fad

Please sign in to comment.