Skip to content

Instantly share code, notes, and snippets.

@nijave
Created November 12, 2016 04:11
Show Gist options
  • Save nijave/221da4b1172b5d3f272dbd9bcb9655db to your computer and use it in GitHub Desktop.
Save nijave/221da4b1172b5d3f272dbd9bcb9655db to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
'''
A simple Miami Web Services example Python script
Date: 11 November 2016
By: Nick Venenga
Setup:
pip3 install requests
Usage:
python3 miamiWS.py
Output:
$ python3 miamiWS.py
Enter your unique ID:venengnj
Enter your password:
{'data': {'CIPCode': None,
'CIPDescription': None,
'classificationCode': 'SP',
'classificationDescription': 'Student Regular',
'fullTimeCode': 'ST',
'fullTimeDescription': 'Student',
'gradLevelCertDate': None,
'gradLevelCode': None,
'gradLevelDescription': None,
'gradLevelExpDate': None,
'homeOrgCode': 'MCS99',
'homeOrgDescription': 'Information Technology Services',
'pidm': '1234567',
'plusNumber': '+01234567',
'professionalLibrarian': 'false',
'rankCode': None,
'rankDescription': None,
'standardizedDepartmentCode': 'ITMCS',
'standardizedDepartmentName': 'Information Technology Services',
'standardizedDivisionCode': 'MCS',
'standardizedDivisionName': 'Information Technology Services',
'startDate': '2014-04-10',
'tenureCode': None,
'tenureDescription': None,
'uniqueId': 'VENENGNJ'},
'error': False,
'status': 200}
'''
import logging
import os
import json
import requests
import getpass
import pprint
# Specify the API endpoint for making requests to
api_endpoint = 'https://ws.miamioh.edu'
# Some APIs will return data based on the accept content type header
headers = {"Accept": "application/json"}
# Get a logging instance
logger = logging.getLogger('miamiWebServices')
# Get credentials from the user
uid = input("Enter your unique ID:")
pwd = getpass.getpass("Enter your password:")
logger.debug("Got user credentials")
# Get an authentication token
logger.debug("Attempting to fetch an authorization token")
# Construct the request URL
url = api_endpoint + '/authentication.json?application=' + uid + '_application'
# Submit the POST request
response = requests.post(url=url, data={'type': 'usernamePassword', 'username': uid, 'password': pwd}, headers=headers)
token = ''
if response.status_code == 200:
# Grab the token from the response
token = json.loads(response.content.decode("utf-8"))['token']
else:
logger.error("Could not fetch " + url + " resource returned response code: " + reponse.status_code)
raise SystemExit("Could not fetch authorization code.")
logger.info("Successfully got authorization token")
# Try to get your employee information (assuming your a Miami employee)
logger.debug("Attempting to fetch employee information")
# Construct the request URL
url = api_endpoint + '/api/employee/v1/' + uid + '?token=' + token
# Perform the GET request
response = requests.get(url=url, headers=headers)
# Handle the response
if response.status_code == 200:
# Dump the response
pprint.pprint(json.loads(response.content.decode("utf-8")))
else:
logger.error("Could not fetch " + url + " resource returned response code: " + reponse.status_code)
logger.info("Successfully fetched employee information")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment