Created
August 3, 2018 09:57
-
-
Save jeremysherriff/8e96aed8e1140d567507f0c78823af17 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
import requests | |
import json | |
ssl = False | |
ip = "192.168.0.32" | |
port = "3579" | |
usernm = "foo" | |
passwd = "bar" | |
WEBURL = "http" + ("s" if ssl else "") + "://"+ip+":"+port | |
h = dict() | |
j = dict() | |
print "Test of POST: Submit credentials in return for auth bearer token" | |
u = "/api/v1/Token" | |
j.update({ "username": usernm, "password": passwd }) | |
r = requests.post( WEBURL + u, json=j ) | |
print "POST request: " + str(r.status_code) + " " + str(r.reason) | |
if r.status_code == 200: | |
d = r.json() | |
jwt_token = "Bearer " + str(d['access_token']) | |
h.update({ "Authorization": jwt_token }) | |
tok_expiry = d['expiration'] | |
print 'Authorization successful.' | |
print 'Token Expires: ' + str(tok_expiry) | |
else: | |
exit (1) | |
print "\nTest of GET: fetch identity of current logged in user" | |
u = "/api/v1/Identity" | |
r = requests.get( WEBURL + u, headers=h ) | |
print "GET request: " + str(r.status_code) + " " + str(r.reason) | |
if r.status_code == 200: | |
d = r.json() | |
if str(d['alias']) != '': | |
myAlias = str(d['alias']) | |
print "Username: " + str(d['userName']) + " (" + str(d['alias']) + ")" | |
else: | |
print "Username: " + str(d['userName']) + " (no alias defined)" | |
myAlias = "Bob" | |
print " emailAddress: " + str(d['emailAddress']) | |
print " lastLoggedIn: " + str(d['lastLoggedIn']) | |
print "\nTest of PUT: update alias for current logged in user" | |
j = d #Take the data from the first request as the json for the PUT | |
j.pop('hasLoggedIn', 0) # Drop read-only values | |
j.pop('lastLoggedIn', 0) | |
j.pop('password', 0) | |
j.update({ 'alias': "Pirate_"+myAlias }) # Update the field(s) we want | |
print "Values to PUT:" | |
print json.dumps(j, indent=4) | |
u = "/api/v1/Identity" # It's the same URL to both get and update/put | |
r = requests.put( WEBURL + u, headers=h, json=j ) | |
print "PUT request: " + str(r.status_code) + " " + str(r.reason) | |
if r.status_code == 200: | |
d = r.json() | |
if d['successful']: | |
print "Update success" | |
else: | |
print "Failed. Errors: " + str(d['errors']) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment