AUTHENTICATION
1. Basic Authentication : The user provides user-id and password every time a request is send as the auth-header.
2. Session Authentication : The user credentials are stored in a cookie.
class TestRestApi():
def __init__(self, ip_addr, username, password):
# Initialise the options.
self.ip_addr = ip_addr
self.username = username
self.password = password
self.rest_params_init()
# Initialize REST API parameters
def rest_params_init(self, sub_url="", method="",
body=None, content_type="application/json"):
self.sub_url = sub_url
self.body = body
self.method = method
self.content_type = content_type
# Create a REST client session.
def rest_call(self):
base_url = 'https://%s:9440/PrismGateway/services/rest/v2.0/%s' % (
self.ip_addr, self.sub_url)
if self.body and self.content_type == "application/json":
self.body = json.dumps(self.body)
request = urllib2.Request(base_url, data=self.body)
base64string = base64.encodestring(
'%s:%s' %
(self.username, self.password)).replace(
'\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
request.add_header(
'Content-Type',
'%s; charset=utf-8' %
self.content_type)
request.get_method = lambda: self.method
try:
response = urllib2.urlopen(request)
result = response.read()
if result:
result = json.loads(result)
return response.code, result
except urllib2.HTTPError as e:
err_result = e.read()
if err_result:
try:
err_result = json.loads(err_result)
except:
print "Error: %s" % e
return "408", None
return "408", err_result
except Exception as e:
print "Error: %s" % e
return "408", None