|
| 1 | +# This software is licenced under the BSD 3-Clause licence |
| 2 | +# available at https://opensource.org/licenses/BSD-3-Clause |
| 3 | +# and described in the LICENCE file in the root of this project |
| 4 | + |
| 5 | +""" |
| 6 | +Example Python 3 application using the dspace.py API client library to retrieve basic person |
| 7 | +and group information in a DSpace repository |
| 8 | +""" |
| 9 | + |
| 10 | +import logging |
| 11 | +import os |
| 12 | +import sys |
| 13 | + |
| 14 | +from dspace_rest_client.client import DSpaceClient |
| 15 | + |
| 16 | +# Import models as below if needed |
| 17 | +# from dspace_rest_d.models import Community, Collection, Item, Bundle, Bitstream |
| 18 | + |
| 19 | +# Example variables needed for authentication and basic API requests |
| 20 | +# SET THESE TO MATCH YOUR TEST SYSTEM BEFORE RUNNING THE EXAMPLE SCRIPT |
| 21 | +# You can also leave them out of the constructor and set environment variables instead: |
| 22 | +# DSPACE_API_ENDPOINT= |
| 23 | +# DSPACE_API_USERNAME= |
| 24 | +# DSPACE_API_PASSWORD= |
| 25 | +# USER_AGENT= |
| 26 | +DEFAULT_URL = "http://localhost:8080/server/api" |
| 27 | +DEFAULT_USERNAME = "[email protected]" |
| 28 | +DEFAULT_PASSWORD = "password" |
| 29 | + |
| 30 | +GROUP_UUID = "11ba756f-9539-496b-a9de-83a5376df4fc" |
| 31 | +NEW_GROUP_NAME = "New Test Group" |
| 32 | +UPDATED_GROUP_NAME = "Updated Test Group" |
| 33 | +PARENT_GROUP_UUID = "4abf266c-7ced-4e63-9e63-ab1db257a32a" |
| 34 | +CHILD_GROUP_UUID = "cf3c9c26-68fa-4924-bf65-fc0a8a717d37" |
| 35 | +EPERSON_UUID = "379b47ea-42b1-4151-81fc-7eabd5cfcec3" |
| 36 | +QUERY = "Administrator" |
| 37 | + |
| 38 | +# Configuration from environment variables |
| 39 | +URL = os.environ.get("DSPACE_API_ENDPOINT", DEFAULT_URL) |
| 40 | +USERNAME = os.environ.get("DSPACE_API_USERNAME", DEFAULT_USERNAME) |
| 41 | +PASSWORD = os.environ.get("DSPACE_API_PASSWORD", DEFAULT_PASSWORD) |
| 42 | + |
| 43 | +# Instantiate DSpace client |
| 44 | +# Note the 'fake_user_agent' setting here -- this will set a string like the following, |
| 45 | +# to get by Cloudfront: |
| 46 | +# Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) \ |
| 47 | +# Chrome/39.0.2171.95 Safari/537.36 |
| 48 | +# The default is to *not* fake the user agent, and instead use the default of |
| 49 | +# DSpace-Python-REST-Client/x.y.z. |
| 50 | +# To specify a custom user agent, set the USER_AGENT env variable and leave/set |
| 51 | +# fake_user_agent as False |
| 52 | +d = DSpaceClient( |
| 53 | + api_endpoint=URL, username=USERNAME, password=PASSWORD, fake_user_agent=True |
| 54 | +) |
| 55 | + |
| 56 | +# Authenticate against the DSpace client |
| 57 | +authenticated = d.authenticate() |
| 58 | +if not authenticated: |
| 59 | + print("Error logging in! Giving up.") |
| 60 | + sys.exit(1) |
| 61 | + |
| 62 | +# --- USER FUNCTIONS --- |
| 63 | + |
| 64 | +# Get users with pagination |
| 65 | +logging.info("Fetching users...") |
| 66 | +users = d.get_users(page=0, size=5) |
| 67 | +for user in users: |
| 68 | + print(f"User: {user.uuid}, Name: {user.name}, Email: {user.email}") |
| 69 | + |
| 70 | +# Get users using an iterator |
| 71 | +logging.info("Fetching users via iterator...") |
| 72 | +user_iter = d.get_users_iter() |
| 73 | +for user in user_iter: |
| 74 | + print(f"Iterated User: {user.uuid}, Name: {user.name}, Email: {user.email}") |
| 75 | + |
| 76 | +# --- GROUP FUNCTIONS --- |
| 77 | + |
| 78 | +# Get groups with pagination |
| 79 | +logging.info("Fetching groups...") |
| 80 | +groups = d.get_groups(page=0, size=5) |
| 81 | +for group in groups: |
| 82 | + print(f"Group: {group.uuid}, Name: {group.name}") |
| 83 | + |
| 84 | +# Get a group by UUID |
| 85 | +logging.info(f"Fetching group by UUID: {GROUP_UUID}") |
| 86 | +group = d.get_group_by_uuid(GROUP_UUID) |
| 87 | +if group: |
| 88 | + print(f"Fetched Group: {group.uuid}, Name: {group.name}") |
| 89 | + |
| 90 | +# Create a new group |
| 91 | +logging.info("Creating a new group...") |
| 92 | +new_group = d.create_group({"name": NEW_GROUP_NAME}) |
| 93 | +if new_group: |
| 94 | + print(f"Created Group: {new_group.uuid}, Name: {new_group.name}") |
| 95 | + |
| 96 | +# Update group name |
| 97 | +new_group_uuid = new_group.uuid |
| 98 | +logging.info(f"Updating group name for {new_group_uuid}...") |
| 99 | +updated_group = d.update_group_name(new_group_uuid, UPDATED_GROUP_NAME) |
| 100 | +if updated_group: |
| 101 | + print(f"Updated Group: {updated_group.uuid}, Name: {updated_group.name}") |
| 102 | + |
| 103 | +# Add a subgroup |
| 104 | +logging.info(f"Adding subgroup {CHILD_GROUP_UUID} to {PARENT_GROUP_UUID}...") |
| 105 | +if d.add_subgroup(PARENT_GROUP_UUID, CHILD_GROUP_UUID): |
| 106 | + print(f"Subgroup {CHILD_GROUP_UUID} added to {PARENT_GROUP_UUID}") |
| 107 | + |
| 108 | +# Remove a subgroup |
| 109 | +logging.info(f"Removing subgroup {CHILD_GROUP_UUID} from {PARENT_GROUP_UUID}...") |
| 110 | +if d.remove_subgroup(PARENT_GROUP_UUID, CHILD_GROUP_UUID): |
| 111 | + print(f"Subgroup {CHILD_GROUP_UUID} removed from {PARENT_GROUP_UUID}") |
| 112 | + |
| 113 | +# Fetch subgroups |
| 114 | +logging.info(f"Fetching subgroups of {PARENT_GROUP_UUID}...") |
| 115 | +subgroups = d.get_subgroups(PARENT_GROUP_UUID) |
| 116 | +for subgroup in subgroups: |
| 117 | + print(f"Subgroup: {subgroup.uuid}, Name: {subgroup.name}") |
| 118 | + |
| 119 | +# Search groups by metadata |
| 120 | +logging.info(f"Searching groups by metadata: {QUERY}") |
| 121 | +found_groups = d.search_groups_by_metadata(QUERY) |
| 122 | +for group in found_groups: |
| 123 | + print(f"Matched Group: {group.uuid}, Name: {group.name}") |
| 124 | + |
| 125 | +# Get EPersons in a group |
| 126 | +logging.info(f"Fetching EPersons in group {GROUP_UUID}...") |
| 127 | +epersons = d.get_epersons_in_group(GROUP_UUID) |
| 128 | +for eperson in epersons: |
| 129 | + print(f"EPerson: {eperson.uuid}, Name: {eperson.name}, Email: {eperson.email}") |
| 130 | + |
| 131 | +# Add an EPerson to a group |
| 132 | +logging.info(f"Adding EPerson {EPERSON_UUID} to group {GROUP_UUID}...") |
| 133 | +if d.add_eperson_to_group(GROUP_UUID, EPERSON_UUID): |
| 134 | + print(f"EPerson {EPERSON_UUID} added to group {GROUP_UUID}") |
| 135 | + |
| 136 | +# Remove an EPerson from a group |
| 137 | +logging.info(f"Removing EPerson {EPERSON_UUID} from group {GROUP_UUID}...") |
| 138 | +if d.remove_eperson_from_group(GROUP_UUID, EPERSON_UUID): |
| 139 | + print(f"EPerson {EPERSON_UUID} removed from group {GROUP_UUID}") |
0 commit comments