Skip to content

Commit

Permalink
the-library-code#18: Allow embed=[] in get_bitstreams and embed forma…
Browse files Browse the repository at this point in the history
…t in response

_embedded in HALResource
new BitstreamFormat model
get_bitstreams takes e.g. (embeds=['format'])
  • Loading branch information
kshepherd committed Jun 11, 2024
1 parent 77a76b3 commit 27b9444
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
10 changes: 8 additions & 2 deletions dspace_rest_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""
import json
import logging
import pprint

import requests
from requests import Request
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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):
Expand Down
56 changes: 55 additions & 1 deletion dspace_rest_client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -28,6 +28,7 @@ class HALResource:
"""
links = {}
type = None
embedded = dict()

def __init__(self, api_resource=None):
"""
Expand All @@ -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}}

Expand Down Expand Up @@ -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):
"""
Expand Down

0 comments on commit 27b9444

Please sign in to comment.