|
| 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 = "username@test.system.edu" |
| 28 | +DEFAULT_PASSWORD = "password" |
| 29 | + |
| 30 | +USER_UUID = "a5e21324-7d42-4688-947d-6ab76c4673b4" |
| 31 | +GROUP_UUID = "11ba756f-9539-496b-a9de-83a5376df4fc" |
| 32 | +SEARCH_EMAIL = "username@test.system.edu" |
| 33 | +SEARCH_PERSON_QUERY = "Stefan" |
| 34 | +SEARCH_GROUP_QUERY = "Administrator" |
| 35 | + |
| 36 | +# Configuration from environment variables |
| 37 | +URL = os.environ.get("DSPACE_API_ENDPOINT", DEFAULT_URL) |
| 38 | +USERNAME = os.environ.get("DSPACE_API_USERNAME", DEFAULT_USERNAME) |
| 39 | +PASSWORD = os.environ.get("DSPACE_API_PASSWORD", DEFAULT_PASSWORD) |
| 40 | + |
| 41 | +# Instantiate DSpace client |
| 42 | +# Note the 'fake_user_agent' setting here -- this will set a string like the following, |
| 43 | +# to get by Cloudfront: |
| 44 | +# Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) \ |
| 45 | +# Chrome/39.0.2171.95 Safari/537.36 |
| 46 | +# The default is to *not* fake the user agent, and instead use the default of |
| 47 | +# DSpace-Python-REST-Client/x.y.z. |
| 48 | +# To specify a custom user agent, set the USER_AGENT env variable and leave/set |
| 49 | +# fake_user_agent as False |
| 50 | +d = DSpaceClient( |
| 51 | + api_endpoint=URL, username=USERNAME, password=PASSWORD, fake_user_agent=True |
| 52 | +) |
| 53 | + |
| 54 | +# Authenticate against the DSpace client |
| 55 | +authenticated = d.authenticate() |
| 56 | +if not authenticated: |
| 57 | + print("Error logging in! Giving up.") |
| 58 | + sys.exit(1) |
| 59 | + |
| 60 | +# --- PERSON FUNCTIONS --- |
| 61 | + |
| 62 | +# Get all users with pagination |
| 63 | +logging.info("Fetching all users...") |
| 64 | +users = d.get_users(page=0, size=10) |
| 65 | +for user in users: |
| 66 | + print(f"User: {user.uuid}, Name: {user.name}, Email: {user.email}") |
| 67 | + |
| 68 | +# Get a user by UUID |
| 69 | +logging.info(f"Fetching user by UUID: {USER_UUID}") |
| 70 | +user = d.get_user_by_uuid(USER_UUID) |
| 71 | +if user: |
| 72 | + print(f"Fetched User: {user.uuid}, Name: {user.name}, Email: {user.email}") |
| 73 | + |
| 74 | +# Search for a user by email |
| 75 | +logging.info(f"Searching user by email: {SEARCH_EMAIL}") |
| 76 | +user = d.search_user_by_email(SEARCH_EMAIL) |
| 77 | +if user: |
| 78 | + print(f"Found User: {user.uuid}, Name: {user.name}, Email: {user.email}") |
| 79 | + |
| 80 | +# Search users by metadata |
| 81 | +logging.info(f"Searching users by metadata: {SEARCH_PERSON_QUERY}") |
| 82 | +users = d.search_users_by_metadata(query=SEARCH_PERSON_QUERY) |
| 83 | +for user in users: |
| 84 | + print(f"Matched User: {user.uuid}, Name: {user.name}, Email: {user.email}") |
| 85 | + |
| 86 | +# --- GROUP FUNCTIONS --- |
| 87 | + |
| 88 | +# Get all groups with pagination |
| 89 | +logging.info("Fetching all groups...") |
| 90 | +groups = d.get_groups(page=0, size=10) |
| 91 | +for group in groups: |
| 92 | + print(f"Group: {group.uuid}, Name: {group.name}") |
| 93 | + |
| 94 | +# Get a group by UUID |
| 95 | +logging.info(f"Fetching group by UUID: {GROUP_UUID}") |
| 96 | +group = d.get_group_by_uuid(GROUP_UUID) |
| 97 | +if group: |
| 98 | + print(f"Fetched Group: {group.uuid}, Name: {group.name}") |
| 99 | + |
| 100 | +# Search groups by metadata |
| 101 | +logging.info(f"Searching groups by metadata: {SEARCH_GROUP_QUERY}") |
| 102 | +groups = d.search_groups_by_metadata(SEARCH_GROUP_QUERY) |
| 103 | +for group in groups: |
| 104 | + print(f"Matched Group: {group.uuid}, Name: {group.name}") |
| 105 | + |
| 106 | +# Get subgroups of a group |
| 107 | +logging.info(f"Fetching subgroups for group: {GROUP_UUID}") |
| 108 | +subgroups = d.get_subgroups(GROUP_UUID) |
| 109 | +for subgroup in subgroups: |
| 110 | + print(f"Subgroup: {subgroup.uuid}, Name: {subgroup.name}") |
| 111 | + |
| 112 | +# Get EPersons in a group |
| 113 | +logging.info(f"Fetching EPersons in group: {GROUP_UUID}") |
| 114 | +epersons = d.get_epersons_in_group(GROUP_UUID) |
| 115 | +for eperson in epersons: |
| 116 | + print(f"EPerson: {eperson.uuid}, Name: {eperson.name}, Email: {eperson.email}") |
0 commit comments