-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfolio_setup.py
More file actions
87 lines (71 loc) · 2.59 KB
/
Copy pathfolio_setup.py
File metadata and controls
87 lines (71 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
Build a FolioClient and fetch all reference-data UUIDs needed at startup.
"""
import os
import logging
from dotenv import load_dotenv
from folioclient import FolioClient
load_dotenv()
log = logging.getLogger(__name__)
STATISTICAL_CODE_DELETE_HOLDING = "delete-eresource-holding"
NOTE_CORAL_ID = "EResource Coral Identifier"
NOTE_PACKAGE_NAME = "EResource Package Name"
NOTE_PROVIDER = "EResource Provider Name"
NOTE_PROVIDER_CODE = "EResource Provider Code"
NOTE_ACCESS_METHOD = "EResource Access Method"
NOTE_ACCESS_METHOD_CODE = "EResource Access Method Code"
def build_client():
return FolioClient(
os.environ["FOLIO_OKAPI_URL"],
os.environ["FOLIO_TENANT"],
os.environ["FOLIO_USERNAME"],
os.environ["FOLIO_PASSWORD"],
)
def load_ref_data(fc):
"""
Fetch all UUIDs needed for holdings creation and suppression.
Returns a flat dict of logical-name -> UUID (or name->UUID sub-dict).
Raises ValueError if any required name is not found in FOLIO.
"""
ref = {}
ref["holdings_type_electronic"] = _lookup_id(
fc, "/holdings-types", "holdingsTypes", "Electronic"
)
ref["location_electronic"] = _lookup_id(fc, "/locations", "locations", "Electronic")
ref["call_number_type_other"] = _lookup_id(
fc, "/call-number-types", "callNumberTypes", "Other scheme"
)
ref["statistical_code_delete_h"] = _lookup_id(
fc,
"/statistical-codes",
"statisticalCodes",
STATISTICAL_CODE_DELETE_HOLDING,
field="code",
)
ref["holdings_source_folio"] = _lookup_id(
fc, "/holdings-sources", "holdingsRecordsSources", "FOLIO"
)
ref["ea_relationship_resource"] = _lookup_id(
fc,
"/electronic-access-relationships",
"electronicAccessRelationships",
"Resource",
)
ref["ill_policies"] = _lookup_all(fc, "/ill-policies", "illPolicies")
ref["holdings_note_types"] = _lookup_all(
fc, "/holdings-note-types", "holdingsNoteTypes"
)
log.info("Reference data loaded successfully.")
return ref
def _lookup_id(fc, path, key, value, field="name"):
items = fc.folio_get(path, key=key, query_params={"limit": 200})
for item in items:
if item.get(field, "").strip() == value:
return item["id"]
raise ValueError(
f"Required reference data not found in FOLIO: {field}={value!r} at {path}. "
"Create it in FOLIO settings before running."
)
def _lookup_all(fc, path, key):
items = fc.folio_get(path, key=key, query_params={"limit": 200})
return {item["name"].strip(): item["id"] for item in items}