Skip to content

Commit dcf8b21

Browse files
committed
construct Item object as a return of get_item function
1 parent 0d644df commit dcf8b21

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

dspace_rest_client/client.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,18 +1149,47 @@ def get_item(self, uuid, embeds=None):
11491149
"""
11501150
Get an item, given its UUID
11511151
@param uuid: the UUID of the item
1152-
@param embeds: Optional list of resources to embed in response JSON
1153-
@return: the raw API response
1152+
@param embeds: Optional list of resources to embed in response JSON
1153+
@return: the constructed Item object or None if an error occurs
11541154
"""
1155-
# TODO - return constructed Item object instead, handling errors here?
11561155
url = f"{self.API_ENDPOINT}/core/items"
11571156
try:
1158-
id = UUID(uuid).version
1157+
# Validate the UUID format
1158+
id_version = UUID(uuid).version
11591159
url = f"{url}/{uuid}"
1160-
return self.api_get(url, parse_params(embeds=embeds), None)
1160+
1161+
# Make API GET request
1162+
response = self.api_get(url, parse_params(embeds=embeds), None)
1163+
1164+
# Handle successful response
1165+
if response.status_code == 200:
1166+
# Parse the response JSON into an Item object
1167+
return self._construct_item(response.json())
1168+
else:
1169+
logging.error(
1170+
"Failed to retrieve item. Status code: %s", response.status_code
1171+
)
1172+
logging.error("Response: %s", response.text)
1173+
return None
11611174
except ValueError:
11621175
logging.error("Invalid item UUID: %s", uuid)
11631176
return None
1177+
except Exception as e:
1178+
logging.error("An unexpected error occurred: %s", str(e))
1179+
return None
1180+
1181+
def _construct_item(self, item_data):
1182+
"""
1183+
Construct an Item object from API response data
1184+
@param item_data: The raw JSON data from the API
1185+
@return: An Item object
1186+
"""
1187+
try:
1188+
# Create an Item instance, using the API response data
1189+
return Item(api_resource=item_data)
1190+
except KeyError as e:
1191+
logging.error("Missing expected key in item data: %s", str(e))
1192+
return None
11641193

11651194
def get_items(self, embeds=None):
11661195
"""

0 commit comments

Comments
 (0)