Forked from rayvoelker/patron_api_create_display_update_example.py
Created
March 13, 2018 12:30
-
-
Save craigboman/2feae6a4218b77661151616996328964 to your computer and use it in GitHub Desktop.
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
from configparser import ConfigParser | |
from base64 import b64encode | |
import requests | |
import json | |
import re | |
# get the api paramaters from the configuration file | |
# config file looks like this: | |
# [api] | |
# base_url = https://sierra-test.library.org/iii/sierra-api/v5 | |
# client_key = PUT_CLIENT_KEY_HERE | |
# client_secret = PUT_CLIENT_SECRET_HERE | |
config = ConfigParser() | |
config.read('api_info.ini') | |
base_url = config['api']['base_url'] | |
client_key = config['api']['client_key'] | |
client_secret = config['api']['client_secret'] | |
# we need to base64 encode our key and secret for the basic auth done for getting the token | |
# base64 expects the string to be in ascii format, and we have to decode it back to utf-8 for the header | |
auth_string = b64encode( | |
(client_key + ':' + client_secret).encode('ascii') | |
).decode('utf-8') | |
# create dictionary for the headers, and add our auth_string to it | |
headers = {} | |
headers['authorization'] = 'basic ' + auth_string | |
# get the response from the token endpoint | |
r = requests.request('POST', base_url + '/token', headers=headers) | |
# convert the json response into a python object | |
json_data = json.loads(r.text) | |
# note: we could also use the following .json() method built into requests .. but it may not work for older versions | |
# json_data = r.json() | |
# print('token: ' + json_data['access_token']) | |
# reset the headers and prepare it with the bearer token we received from the authorization request | |
headers = {} | |
headers['authorization'] = 'bearer ' + json_data['access_token'] | |
headers['content-type'] = 'application/json' | |
headers['accept'] = 'application/json' | |
# this is optional ... this will return some information about our token | |
# send the next request for token info with our bearer token | |
r = requests.request("GET", base_url + '/info/token', headers=headers) | |
print(r.text) | |
# -- | |
# START create (POST) a very bare-bones patron record | |
print('creating the bare-bones patron record!') | |
patron_data = { | |
"fixedFields": { | |
"44": { | |
"label": "E-Lib Update? (P1)", | |
"value": "n" | |
} | |
}, | |
'varFields': [ | |
{ | |
'fieldTag': 'x', | |
'content': 'restapi-testing-xrvoelke' | |
}, | |
{ | |
'fieldTag': 'x', | |
'content': 'restapi-testing-xrvoelke-2' | |
} | |
] | |
} | |
print('json of patron_data:') | |
print(json.dumps(patron_data)) | |
# send the request to create the patron record | |
r = requests.request('POST', base_url + '/patrons', headers=headers, data=json.dumps(patron_data)) | |
print(r.status_code) | |
print(r.text) | |
# the response of the previous request should return the id of the patron record that was created | |
json_data = json.loads(r.text) | |
patron_record_id = None | |
# use a regular expression to get the last digits from the link url ( which will be the patron record number (id) ) | |
match = re.search(r'(\d+)(?!.*\d)', json_data['link']) | |
if match: | |
patron_record_id = match.group() | |
print(patron_record_id) | |
# DONE creating (POST) bare-bones patron record | |
# -- | |
# -- | |
# START fetch (get) the patron record we just created ... | |
# set the paramaters that we want to retrieve for the patron with record id | |
params = {'id': str(patron_record_id),'fields':'default,updatedDate,createdDate,deleted,suppressed,names,barcodes,emails,addresses,phones,fixedFields,varFields'} | |
r = requests.request("GET", base_url + '/patrons', headers=headers, params=params) | |
print(r.status_code) | |
print(r.text) | |
# DONE fetch (get) the patron record we just created ... | |
# -- | |
# -- | |
# START update the patron record we just created ... | |
# set the patron data dictionary for values we want to set / change | |
# (adding a patron name, changing the fixed field 44 to 'y', and replacing the two note fields with new data) | |
patron_data = {} | |
patron_data = { | |
"names": [ | |
"PATRON_LAST_NAME, PATRON_FIRSTNAME" | |
], | |
"fixedFields": { | |
"44": { | |
"label": "E-Lib Update? (P1)", | |
"value": "y" | |
}, | |
# adding this valid, second fixed field to the request results in the following error | |
# {"code":115,"specificCode":0,"httpStatus":400,"name":"Invalid JSON","description":"Invalid JSON : field(s) unknown : fixedFields."} | |
# "46": { | |
# "label": "Foundation? (P3)", | |
# "value":"0" | |
# } | |
}, | |
'varFields': [ | |
{ | |
'fieldTag': 'x', | |
'content': 'restapi-testing-xrvoelke-3' | |
}, | |
{ | |
'fieldTag': 'x', | |
'content': 'restapi-testing-xrvoelke-4' | |
} | |
] | |
} | |
r = requests.request('PUT', base_url + '/patrons/' + str(patron_record_id), headers=headers, data=json.dumps(patron_data)) | |
print(r.status_code) | |
print(r.text) | |
# DONE update the patron record we just created | |
# -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment