import requests
import json
baseURL = 'http://localhost:8089'
user = 'admin'
password = 'secure_password'
repository = '101'
auth = requests.post(baseURL + '/users/' + user + '/login?password=' + password).json()
session = auth['session']
headers = {'X-ArchivesSpace-Session': session, 'Content_Type': 'application/json'}
record_type = 'top_containers'
endpoint = '/repositories/' + repository + '/' + record_type + '?all_ids=true'
print('This endpoint will gather every id for ' + record_type + ': ' + endpoint)
test_endpoint = requests.get(baseURL + endpoint, headers=headers)
if test_endpoint.status_code !=200:
print('That did not work. Do you have the correct endpoint? --> ' + endpoint)
quit()
else:
ids = requests.get(baseURL + endpoint, headers=headers).json()
print('There are ' + str(len(ids)) + ' records of the requested type in this repo')
print('Their ids are ' + str(ids))
records = []
for id in ids:
endpoint = '/repositories/' + repository + '/' + record_type + '/' + str(id)
output = requests.get(baseURL + endpoint, headers=headers).json()
records.append(output)
filename = record_type + '.json'
f = open(filename, 'w')
json.dump(records, f)
f.close()
print('The JSON for all ' + str(len(ids)) + ' records has been written to a file named ' + f.name + ' in the same directory where this script lives.')
API Documentation:
https://archivesspace.github.io/archivesspace/api/#get-a-list-of-subjects
# Final script all together.
# If you want to re-purpose this script, this is the block to use.
# Save it as name_of_file.py in order to run it as Python3 in your local environment.
# Disclaimer: This script is being provided as an example and may need to be modified for local use.
# Modifying and using this script is the responsibility of the individual using it.
# Do not test against Production!
# Authentication based on a script by ehanson8 (https://github.com/MITLibraries/archivesspace-api-python-scripts)
# Significant aspects:
# - How to set variables
# - How to get all ids of a single record type
# - How to check your status code (i.e. did it work?)
# - How to save an output to a local file
#Import the following libraries
import csv
import json
import requests
#Set your authentication info, baseurl, and repository info (if relevant)
baseURL = 'http://localhost:8089'
user = 'admin'
password = 'secure_password'
repository = '101'
#Authorize and store your session key in your header
auth = requests.post(baseURL + '/users/' + user + '/login?password=' + password).json()
session = auth['session']
headers = {'X-ArchivesSpace-Session': session, 'Content_Type': 'application/json'}
print('Your session key is: ' + session)
#Optional way to create your endpoint with variable below
record_type = 'resources'
endpoint = '/repositories/' + repository + '/' + record_type + '?all_ids=true'
#Note that the above endpoint includes repository, hence, it will not work for non-repo endpoints
#Note also that this endpoint is only gathering the record IDs, not the records themselves
#Those ids are later stored using the 'ids' variable below
print('This endpoint will gather every id for ' + record_type + ': ' + endpoint)
test_endpoint = requests.get(baseURL + endpoint, headers=headers) #Here we begin to test your endpoint, this is good to know
if test_endpoint.status_code !=200: #If the status code is NOT 200, your GET above did not work and the script stops
print('That did not work. Do you have the correct endpoint? --> ' + endpoint)
quit()
else: #If the status code IS 200, your GET above did work and the script continues
ids = requests.get(baseURL + endpoint, headers=headers).json()
print('There are ' + str(len(ids)) + ' records of the requested type in this repo')
print('Their ids are ' + str(ids))
records = [] #Open a empty list
for id in ids: #For each id in the id list....
endpoint = '/repositories/' + repository + '/' + record_type + '/' + str(id) #Create an endpoint per id...
output = requests.get(baseURL + endpoint, headers=headers).json() #GET that record...
records.append(output) #Append that record to the empty list
filename = record_type + '.json' #Set the filename
f = open(filename, 'w') #Create a new file
json.dump(records, f) #Encode the records list as JSON and save to that file
f.close() #Close the file
print('The JSON for all ' + str(len(ids)) + ' records has been written to a file named ' + f.name + ' in the same directory where this script lives.')