diff --git a/dspace_rest_client/client.py b/dspace_rest_client/client.py index c651bff..29b1cc7 100644 --- a/dspace_rest_client/client.py +++ b/dspace_rest_client/client.py @@ -16,6 +16,7 @@ """ import json import logging +import pprint import requests from requests import Request @@ -586,7 +587,7 @@ def create_bundle(self, parent=None, name='ORIGINAL'): return Bundle(api_resource=parse_json(self.api_post(url, params=None, json={'name': name, 'metadata': {}}))) # PAGINATION - def get_bitstreams(self, uuid=None, bundle=None, page=0, size=20, sort=None): + def get_bitstreams(self, uuid=None, bundle=None, page=0, size=20, sort=None, embeds=None): """ Get a specific bitstream UUID, or all bitstreams for a specific bundle @param uuid: UUID of a specific bitstream to retrieve @@ -595,6 +596,8 @@ def get_bitstreams(self, uuid=None, bundle=None, page=0, size=20, sort=None): @param size: Size of results per page (default: 20) @return: list of python Bitstream objects """ + if embeds is None: + embeds = [] url = f'{self.API_ENDPOINT}/core/bitstreams/{uuid}' if uuid is None and bundle is None: return list() @@ -612,12 +615,15 @@ def get_bitstreams(self, uuid=None, bundle=None, page=0, size=20, sort=None): params['page'] = page if sort is not None: params['sort'] = sort + if len(embeds) > 0: + params['embed'] = ','.join(embeds) r_json = self.fetch_resource(url, params=params) if '_embedded' in r_json: if 'bitstreams' in r_json['_embedded']: bitstreams = list() for bitstream_resource in r_json['_embedded']['bitstreams']: - bitstreams.append(Bitstream(bitstream_resource)) + bitstream = Bitstream(bitstream_resource) + bitstreams.append(bitstream) return bitstreams def create_bitstream(self, bundle=None, name=None, path=None, mime=None, metadata=None, retry=False): diff --git a/dspace_rest_client/models.py b/dspace_rest_client/models.py index de463ac..a869b01 100644 --- a/dspace_rest_client/models.py +++ b/dspace_rest_client/models.py @@ -19,7 +19,7 @@ from uuid import UUID __all__ = ['DSpaceObject', 'HALResource', 'ExternalDataObject', 'SimpleDSpaceObject', 'Community', - 'Collection', 'Item', 'Bundle', 'Bitstream', 'User', 'Group'] + 'Collection', 'Item', 'Bundle', 'Bitstream', 'BitstreamFormat', 'User', 'Group'] class HALResource: @@ -28,6 +28,7 @@ class HALResource: """ links = {} type = None + embedded = dict() def __init__(self, api_resource=None): """ @@ -39,6 +40,8 @@ def __init__(self, api_resource=None): self.type = api_resource['type'] if '_links' in api_resource: self.links = api_resource['_links'].copy() + if '_embedded' in api_resource: + self.embedded = api_resource['_embedded'].copy() else: self.links = {'self': {'href': None}} @@ -385,6 +388,57 @@ def as_dict(self): 'sequenceId': self.sequenceId} return {**dso_dict, **bitstream_dict} +class BitstreamFormat(AddressableHALResource): + """ + Bitstream format: https://github.com/DSpace/RestContract/blob/main/bitstreamformats.md + example: + { + "shortDescription": "XML", + "description": "Extensible Markup Language", + "mimetype": "text/xml", + "supportLevel": "KNOWN", + "internal": false, + "extensions": [ + "xml" + ], + "type": "bitstreamformat" + } + """ + shortDescription = None + description = None + mimetype = None + supportLevel = None + internal = False + extensions = [] + type = 'bitstreamformat' + + def __init__(self, api_resource): + super(BitstreamFormat, self).__init__(api_resource) + if 'shortDescription' in api_resource: + self.shortDescription = api_resource['shortDescription'] + if 'description' in api_resource: + self.description = api_resource['description'] + if 'mimetype' in api_resource: + self.mimetype = api_resource['mimetype'] + if 'supportLevel' in api_resource: + self.supportLevel = api_resource['supportLevel'] + if 'internal' in api_resource: + self.internal = api_resource['internal'] + if 'extensions' in api_resource: + self.extensions = api_resource['extensions'].copy() + + def as_dict(self): + parent_dict = super(BitstreamFormat, self).as_dict() + dict = { + 'shortDescription': self.shortDescription, + 'description': self.description, + 'mimetype': self.mimetype, + 'supportLevel': self.supportLevel, + 'internal': self.internal, + 'extensions': self.extensions, + 'type': self.type + } + return {**parent_dict, **dict} class Group(DSpaceObject): """