-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoUpdate.py
99 lines (82 loc) · 3.98 KB
/
doUpdate.py
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
88
89
90
91
92
93
94
95
96
97
98
99
#/usr/bin/python
# File: doUpdate.py
# Author: Michael Cummings, Assistant Museum Librarian, Systems and Information Technology
# Thomas J. Watson Library, The Metropolitan Museam of Art
# June, 2018
# Editor: Halima Rahman, Intern
# Gist: This Sierra API script is performed after sqlBuildDict.py was run.
# The purpose is to update patron records that were output to the
# todolist.txt file, if any. This script modifies the expiration
# date, barcode, and note field of the patron record.
# Usage: $python doUpdate.py
import json
import requests
import base64
from requests import Request, Session
# -------------------------------
# Set Up Info for Sierra API Host
# -------------------------------
SIERRA_API_HOST = 'HOST NAME HERE'
SIERRA_API_KEY = 'API KEY HERE'
SIERRA_API_KEY_SECRET = 'API SECRET HERE'
AUTH_URI = '/iii/sierra-api/v5/token'
VALIDATE_URI = '/iii/sierra-api/v5/patrons/validate'
PATRONS_URI = '/iii/sierra-api/v5/patrons/'
logFile = '/home/Halima/projects/fellows/logFile.txt'
# ----------------------------------------------
# Prepare URL, custom headers, and body for auth
# ----------------------------------------------
# Create URL for auth endpoint
auth_url = SIERRA_API_HOST + AUTH_URI
# Base64 encode the API key and secret separated by a ':' (colon)
encoded_key = base64.b64encode(SIERRA_API_KEY + ':' + SIERRA_API_KEY_SECRET)
auth_headers = {'Accept': 'application/json', 'Authorization': 'Basic ' + encoded_key,'Content-Type': 'application/x-www-form-urlencoded'}
# Set grant type request for HTTP body
grant_type = 'client_credentials' # Request a client credentials grant authorization
# Make the call to the Auth endpoint to get a bearer token
auth_response = requests.post(auth_url, headers = auth_headers, data = grant_type)
access_token = auth_response.json()['access_token']
# Create headers for making subsequent calls to the API
request_headers = {'Accept': 'application/json', 'Authorization': 'Bearer ' + access_token,'Content-Type': 'application/json'}
# -----------------------------------------------------------------
# The registration form puts the date in MM-DD-YYYY form. Change it.
# -----------------------------------------------------------------
def yyyymmdd(expiredDate):
# Change order of strings of date e.g., '12-31/2017' to '2017-12-31'
if expiredDate:
revisedDate =expiredDate[6:10]+"-"+expiredDate[0:2]+"-"+expiredDate[3:5]
return revisedDate
else:
return "2020-09-01"
# -------------------------------------------------------------
# The shell script that calls this python script has determined
# that the todolist exists.
# Open the file of pending updates generated by sqlBuildDict.py
# -------------------------------------------------------------
with open('/home/Halima/projects/fellows/todolist.txt','r') as f:
resultlist = json.load(f)
#
# -------------------------------------------------------------
# Parse the sql results; assign Python variables; construct
# the 'payload' JSON object
# -------------------------------------------------------------
for item in resultlist:
PATRON_RECORD_ID=str(item[0])
my_patron_name =str(item[4])+", "+str(item[5])
payload = {
"expirationDate": ""+str(yyyymmdd(item[3]))+"",
"barcodes": [""+str(item[2])+""],
"addresses": [ { "lines": [ " " ], "type": "h" } ]
}
# Add this patron's id to the end of the URL for endpoint
patrons_url = SIERRA_API_HOST + PATRONS_URI + PATRON_RECORD_ID
# UPDATE the patron record
patrons_response = requests.put(patrons_url, headers = request_headers, data = json.dumps(payload))
# log the result
with open(logFile, 'a') as l:
l.write("PATRON ID:" + PATRON_RECORD_ID + "\n")
l.write("PATRON NAME:" + my_patron_name + "\n")
if patrons_response.status_code == 204:
l.write("SUCCESSFULLY UPDATED \n")
else:
l.write("****** ERROR ******* \n" + patrons_response_status_code)