Add basic error checking on network requests

This commit is contained in:
Adam Goldsmith 2018-05-08 16:01:15 -04:00
parent 7a7f0a324b
commit 8dd7904c5f

View File

@ -1,3 +1,4 @@
import sys
import requests import requests
import csv import csv
from io import StringIO from io import StringIO
@ -36,6 +37,10 @@ def getMembershipworksData():
"eml": MEMBERSHIPWORKS_USERNAME, "eml": MEMBERSHIPWORKS_USERNAME,
"org": "10000", "org": "10000",
"pwd": MEMBERSHIPWORKS_PASSWORD}) "pwd": MEMBERSHIPWORKS_PASSWORD})
if r.status_code != 200 or 'SF' not in r.json():
print("MembershipWorks Login Error: ", r.status_code, r.reason)
print(r.text)
sys.exit(1)
SFtoken = r.json()['SF'] SFtoken = r.json()['SF']
# get list of member/staff IDs # get list of member/staff IDs
@ -44,6 +49,10 @@ def getMembershipworksData():
"lbl": "5ae37979f033bfe8534f8799,5771675edcdf126302a2f6b9", # members and staff "lbl": "5ae37979f033bfe8534f8799,5771675edcdf126302a2f6b9", # members and staff
"org": "15475", # unknown "org": "15475", # unknown
"var": "_id,nam,ctc"}) "var": "_id,nam,ctc"})
if r.status_code != 200 or 'usr' not in r.json():
print("MembershipWorks User Listing Error: ", r.status_code, r.reason)
print(r.text)
sys.exit(1)
ids = [user['_id'] for user in r.json()['usr']] ids = [user['_id'] for user in r.json()['usr']]
# get members CSV # get members CSV
@ -54,6 +63,10 @@ def getMembershipworksData():
"mux": "", # unknown "mux": "", # unknown
"tid": ",".join(ids), # ids of members to get "tid": ",".join(ids), # ids of members to get
"var": "lvl,xws,xms,xsc,xrs,xfd,xac,phn,eml,lbl,nam"}) # which columns to get "var": "lvl,xws,xms,xsc,xrs,xfd,xac,phn,eml,lbl,nam"}) # which columns to get
if r.status_code != 200:
print("MembershipWorks CSV Generation Error: ", r.status_code, r.reason)
print(r.text)
sys.exit(1)
return r.text return r.text
@ -90,8 +103,12 @@ def doRequest(ip, params=None, files=None):
params=params, params=params,
files=files, files=files,
auth=requests.auth.HTTPDigestAuth(DOOR_USERNAME, DOOR_PASSWORD), auth=requests.auth.HTTPDigestAuth(DOOR_USERNAME, DOOR_PASSWORD),
timeout=1,
verify=False) # ignore insecure SSL verify=False) # ignore insecure SSL
if r.status_code != 200:
print("Door Updating Error: ", r.status_code, r.reason)
print(r.text) print(r.text)
sys.exit(1)
def updateDoor(doorIP, csv): def updateDoor(doorIP, csv):
"""Do the update import procedure on a door control""" """Do the update import procedure on a door control"""