Created
March 19, 2021 13:18
-
-
Save sekjal/b1065f0483ee18c65b79d3d6560996ed to your computer and use it in GitHub Desktop.
Rebuild FOLIO Instance from MARC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import uuid | |
from datetime import datetime, timedelta | |
import os | |
from folioclient.FolioClient import FolioClient | |
def handle(event, context): | |
if event.method != 'POST': | |
return { | |
"statusCode": 405, | |
"body" : "Method not allowed" | |
} | |
body = json.loads(event.body) | |
secret_key = body['context'] | |
if not (secret_key): | |
return { | |
"statusCode": 401, | |
"body" : "No Context Given" | |
} | |
with open('/var/openfaas/secrets/secret-api-key') as f: | |
secrets = json.load(f) | |
if not (secret_key in secrets): | |
return { | |
"statusCode": 401, | |
"body": "Could not find FOLIO tenant" | |
} | |
if not ('okapi_url' in secrets[secret_key] and 'tenant_id' in secrets[secret_key] and 'admin_user' in secrets[secret_key] and 'admin_pw' in secrets[secret_key]): | |
return { | |
"statusCode": 401, | |
"body": "FOLIO tenant misconfigured" | |
} | |
folio_client = FolioClient(secrets[secret_key]['okapi_url'],secrets[secret_key]['tenant_id'],secrets[secret_key]['admin_user'],secrets[secret_key]['admin_pw']) | |
get_url = folio_client.okapi_url + "/records-editor/records?instanceId=" + body['id'] | |
r = requests.get(get_url, headers=folio_client.okapi_headers) | |
instance = {} | |
if r.status_code == requests.codes.ok: | |
srs_record = r.json() | |
if 'suppress' in body: | |
if (body['suppress'] == 'true'): | |
srs_record['suppressDiscovery'] = True | |
put_url = folio_client.okapi_url + "/records-editor/records/" + srs_record['parsedRecordId'] | |
r2 = requests.put(put_url, | |
data=json.dumps(srs_record, ensure_ascii=False).encode('utf-8'), | |
headers=folio_client.okapi_headers) | |
if r2.status_code == requests.codes.accepted: | |
return { | |
"statusCode": 200, | |
# "body": "Instance " + body['id'] + " touched!" | |
"body": json.dumps(srs_record, ensure_ascii=False) | |
} | |
else: | |
return { | |
"statusCode": r2.status_code, | |
"body": "PUT phase error: " + r2.text | |
} | |
else: | |
return { | |
"statusCode": r.status_code, | |
"body": "GET phase error: " + r.text | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requests | |
uuid | |
folioclient |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment