diff --git a/netdot/Client.py b/netdot/Client.py index 7b2f652..11e2ca5 100755 --- a/netdot/Client.py +++ b/netdot/Client.py @@ -37,429 +37,430 @@ __version__ = "0.03" + class Connect(object): - def __init__(self, username, password, server, debug = 0): - """ - Class constructor, instantiates a number of - variables for use in the class. Mainly the required - NetDot HTTP headers and login form parameters. - - Usage: - import netdot - dot = netdot.Client.Connect(username, - password, - "https://netdot.localdomain/netdot", - [debug]) - - Returns: NetDot.Client object. - """ - if debug == 1: - self.debug = True - print "DEBUG MODE: ON" - if username and password and server: - self.base_url = server + '/rest' - self.login_url = server + '/NetdotLogin' - self.timeout = 10 - self.retries = 3 - self.version = __version__ - self.headers = { 'User_Agent':'Netdot::Client::REST/self.version', - 'Accept':'text/xml; version=1.0'} - params = {'destination':'index.html', - 'credential_0':username, - 'credential_1':password, - 'permanent_session':1} - # Call the _login() function - self._login(params) - else: - raise AttributeError('Username, Password and Server are REQUIRED') - - def _login(self,params): - """ - Internal Function. Logs into the NetDot API with provided credentials, - stores the Apache generated cookies into the self object to be - reused. - - Arguments: - dict -- 'destination':'index.html', - 'credential_0':username, - 'credential_1':password, - 'permanent_session':1 - """ - response = requests.post(self.login_url, data=params, - headers=self.headers) - if response.status_code == 200: - self.auth_cookies = response.request.cookies - else: - raise AttributeError('Invalid Credentials') - - def get(self, url): - """ - This function provides a simple interface - into the "GET" function by handling the authentication - cookies as well as the required headers and base_url for - each request. - - Arguments: - url -- Url to append to the base url - - Usage: - response = netdot.Client.get("/url") - - Returns: - Result as a multi-level dictionary on sucsess. - """ - response = requests.get(self.base_url + url, - cookies=self.auth_cookies, - headers=self.headers) - if hasattr(self, 'debug'): - Util.dump(response) - if response.error: - raise HTTPError - if response.status_code == 200: - xmlmatch = re.search(r'\