Skip to content

Commit

Permalink
API updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ehanson8 committed Jun 27, 2024
1 parent f654b4d commit 061a632
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 50 deletions.
12 changes: 8 additions & 4 deletions asaps/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,20 @@


@click.group()
@click.option("--url", envvar="ARCHIVESSPACE_URL")
@click.option("--url", envvar="AS_BASEURL")
@click.option(
"-u", "--username", prompt="Enter username", help="The username for authentication."
"-u",
"--username",
prompt="Enter username",
envvar="AS_USERNAME",
help="The username for authentication.",
)
@click.option(
"-p",
"--password",
prompt="Enter password",
hide_input=True,
envvar="DOCKER_PASS",
envvar="AS_PASSWORD",
help="The password for authentication.",
)
@click.pass_context
Expand Down Expand Up @@ -164,7 +168,7 @@ def find(ctx, dry_run, repo_id, rec_type, field, search, rpl_value):
skipped_arch_objs = []
if rec_type == "archival_object":
for uri in skipped_resources:
arch_obj_list = as_ops.get_arch_objs_for_resource(uri)
arch_obj_list = as_ops.get_archival_objects_for_resource(uri)
skipped_arch_objs.append(arch_obj_list)
skipped_uris = skipped_resources + skipped_arch_objs
for uri in as_ops.search(search, repo_id, rec_type, field):
Expand Down
22 changes: 5 additions & 17 deletions asaps/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ def get_all_records(self, endpoint):
ids = self.client.get(f"{endpoint}?all_ids=true").json()
return ids

def get_arch_objs_for_resource(self, uri):
def get_archival_objects_for_resource(self, uri):
"""Get archival objects associated with a resource."""
logger.info(f"Retrieving AOs for {uri}")
arch_obj_list = []
output = self.client.get(f"{uri}/tree").json()
for arch_obj_uri in find_key(output, "record_uri"):
if "archival_objects" in arch_obj_uri:
arch_obj_list.append(arch_obj_uri)
return arch_obj_list
ordered_records = self.client.get(f"{uri}/ordered_records").json()
return [
record["ref"] for record in ordered_records["uris"] if record["depth"] > 0
]

def get_record(self, uri):
"""Retrieve an individual record."""
Expand Down Expand Up @@ -213,16 +211,6 @@ def filter_note_type(rec_obj, note_type):
return (n for n in rec_obj["notes"] if n.get("type") == note_type)


def find_key(nest_dict, key):
"""Find all instances of a key in a nested dictionary."""
if key in nest_dict:
yield nest_dict[key]
children = nest_dict.get("children")
if isinstance(children, list):
for child in children:
yield from find_key(child, key)


def string_to_uri(agent_links, string, uri_dict, role, relator=""):
"""Creates an agent link from a dict of URIs and labels."""
uri_found = False
Expand Down
4 changes: 2 additions & 2 deletions asaps/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def link_dig_obj(arch_obj, dig_obj_uri):
instance["instance_type"] = "digital_object"
instance["jsonmodel_type"] = "instance"
instance["digital_object"] = {"ref": dig_obj_uri}
instance["is_representative"] = True
instance["is_representative"] = False
arch_obj["instances"].append(instance)
return arch_obj

Expand Down Expand Up @@ -202,7 +202,7 @@ def update_dig_obj_link(dig_obj, link):
file_version = {}
file_version["file_uri"] = link
file_version["publish"] = True
file_version["is_representative"] = True
file_version["is_representative"] = False
file_version["jsonmodel_type"] = "file_version"
dig_obj["file_versions"] = [file_version]
file_version["xlink_actuate_attribute"] = "onRequest"
Expand Down
20 changes: 14 additions & 6 deletions asaps/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
def export_metadata(client, resource, file_identifier, repo_id):
"""Export ArchivesSpace metadata as dicts for each archival object."""
resource_uri = f"/repositories/{repo_id}/resources/{resource}"
arch_obj_list = client.get_arch_objs_for_resource(resource_uri)
for uri in arch_obj_list:
rec_obj = client.get_record(uri)
archival_object_list = client.get_archival_objects_for_resource(resource_uri)
for uri in archival_object_list:
record_object = client.get_record(uri)
if file_identifier == "uri":
repo_id = record_object["uri"].replace("/repositories/", "")
repo_id = repo_id[: repo_id.index("/archival_objects/")]
rec_id_split = record_object["uri"].rindex("/archival_objects/")
rec_id = record_object["uri"][rec_id_split + 18 :]
file_identifier_value = f"{repo_id.zfill(2)}-{rec_id.zfill(9)}"
else:
file_identifier_value = record_object.get(file_identifier)
report_dict = {
"uri": rec_obj["uri"],
"title": rec_obj["display_string"],
"file_identifier": rec_obj.get(file_identifier),
"uri": record_object["uri"],
"title": record_object["display_string"],
"file_identifier": file_identifier_value,
}
yield report_dict

Expand Down
14 changes: 8 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ def as_mock():
m.get("/repositories/0/resources/756", json=res_json_2)
upd_json = {"post": "Success"}
m.post("/repositories/0/resources/423", json=upd_json)
tree_json = {
"record_uri": "/repositories/0/archival_objects/1234",
"children": [{"record_uri": "/repositories/0/archival_objects/5678"}],
ordered_records_json = {
"uris": [
{"ref": "/repositories/0/resources/423", "depth": 0},
{"ref": "/repositories/0/archival_objects/1234", "depth": 1},
]
}
m.get("/repositories/2/resources/423/tree", json=tree_json)
m.get(
"/repositories/0/resources/423/ordered_records", json=ordered_records_json
)
crtd_json = {"status": "Created"}
m.post("/repositories/0/resources", json=crtd_json)
search_json = [{"uri": "/repositories/0/archival_objects/1234"}]
m.get("/repositories/0/search?", json=search_json)
tree_json = {"record_uri": "/repositories/0/archival_objects/1234"}
m.get("/repositories/0/resources/423/tree", json=tree_json)
ao_json = {
"uri": "/repositories/0/archival_objects/1234",
"ref_id": "a2b2c2",
Expand Down
19 changes: 4 additions & 15 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ def test_get_all_records(as_ops):
assert ids == [423, 756]


def test_get_arch_objs_for_resource(as_ops):
"""Test get_arch_objs_for_resource method."""
resource = "/repositories/2/resources/423"
arch_obj_list = as_ops.get_arch_objs_for_resource(resource)
assert "/repositories/0/archival_objects/5678" in arch_obj_list
def test_get_archival_objects_for_resource(as_ops):
"""Test get_archival_objects_for_resource method."""
resource = "/repositories/0/resources/423"
arch_obj_list = as_ops.get_archival_objects_for_resource(resource)
assert "/repositories/0/archival_objects/1234" in arch_obj_list


Expand Down Expand Up @@ -196,16 +195,6 @@ def test_filter_note_type():
assert note["note_type"] == "acqinfo"


def test_find_key():
"""Test find_key function."""
nest_dict = {"children": [{"publish": True, "children": [{"publish": True}]}]}
keys = models.find_key(nest_dict, "children")
key_count = 0
for key in keys:
key_count += 1
assert key_count == 2


def test_string_to_uri():
agent_links = []
string = "Smith, N."
Expand Down

0 comments on commit 061a632

Please sign in to comment.