11# -*- coding: utf-8 -*-
22# (c) Christian Meißner 2020
3-
3+ """Default class to handle all api interactions with phpIPAM server."""
44import re
55import requests
66
1515
1616
1717class Api (object ):
18+ """The main class.
1819
19- """ The main class. It generates tha API object where you can run
20+ It generates tha API object where you can run
2021 different actions again to `create`, `update` and `delete` entities.
2122 It also provides functions with informational character only.
2223 """
24+
2325 def __init__ (self , url , app_id , username = None , password = None , token = None , encryption = False , timeout = None , ssl_verify = True , user_agent = None ):
26+ """Generate the api object.
27+
28+ The costructor collects all data to connect to phpIPAM API. If all data is there it makes the connection to the given server.
2429
25- """ contructor method
2630 :param url: The URL to a phpIPAM instance. It includes the protocol (`http` or `https`).
2731 :type url: str
2832 :param app_id: The app_id which is used for the API operations.
@@ -64,8 +68,8 @@ def __init__(self, url, app_id, username=None, password=None, token=None, encryp
6468 self ._login ()
6569
6670 def _query (self , path = 'user' , headers = None , method = GET , data = None , params = None , auth = None , token = None ):
71+ """Send queries to phpIPAM API in a generalistic manner.
6772
68- """ Sends queries to phpIPAM API in a generalistic manner
6973 :param path: Path to the controler and possibly to function to use., defaults to 'user'
7074 :type path: str, optional
7175 :param headers: Optional request headers, defaults to None
@@ -121,23 +125,26 @@ def _query(self, path='user', headers=None, method=GET, data=None, params=None,
121125 return result ['data' ]
122126
123127 def _login (self ):
124- """ Login method """
128+ """Login to phpIPAM API and return token. """
125129 _auth = HTTPBasicAuth (self ._api_username , self ._api_password )
126130 resp = self ._query (method = POST , auth = _auth )
127131
128132 self ._api_token = resp ['token' ]
129133
130134 def get_token (self ):
135+ """Return last login token.
131136
132- """ Method to grap last login token
133137 :return: Returns the api token from the last successful login.
134138 :rtype: str
135139 """
136140 return self ._api_token
137141
138142 def get_entity (self , controller , controller_path = None , params = None ):
143+ """Get existing entity from phpIPAM server.
144+
145+ This method query for existing entity. It there a result it will be returned otherwise
146+ an PhpIPAMEntityNotFound exception is raised from underlying method.
139147
140- """ Method to get an existing entity
141148 :param controller: Name of the controller to request entity from.
142149 :type controller: str
143150 :param controller_path: The path which is used to query for entities, defaults to None
@@ -158,8 +165,8 @@ def get_entity(self, controller, controller_path=None, params=None):
158165 return self ._query (token = self ._api_token , method = GET , path = _path , params = _params )
159166
160167 def create_entity (self , controller , controller_path = None , data = None , params = None ):
168+ """Create an entity.
161169
162- """ Create an entity
163170 :param controller: Name of the controller to use.
164171 :type controller: str
165172 :param controller_path: The path which is used to query for entities, defaults to None
@@ -182,8 +189,8 @@ def create_entity(self, controller, controller_path=None, data=None, params=None
182189 return self ._query (token = self ._api_token , method = POST , path = _path , data = data , params = _params )
183190
184191 def delete_entity (self , controller , controller_path , params = None ):
192+ """Delete an entity.
185193
186- """ This method is used to delete an entity.
187194 :param controller: Name of the controller to use.
188195 :type controller: str
189196 :param controller_path: The path wich is used to access the entity to delete.
@@ -200,8 +207,8 @@ def delete_entity(self, controller, controller_path, params=None):
200207 return self ._query (token = self ._api_token , method = DELETE , path = _path , params = _params )
201208
202209 def update_entity (self , controller , controller_path = None , data = None , params = None ):
210+ """Update an entity.
203211
204- """ This method is used to update an entity.
205212 :param controller: Name of the controller to use.
206213 :type controller: str
207214 :param controller_path: The path which is used to access the entity to update., defaults to None
@@ -224,9 +231,11 @@ def update_entity(self, controller, controller_path=None, data=None, params=None
224231 return self ._query (token = self ._api_token , method = PATCH , path = _path , data = data , params = _params )
225232
226233 def controllers (self ):
234+ """Report all controllers from phpIPAM API.
227235
228- """ This method is used to report all known controllers of phpIPAM API.
236+ This method is used to report all known controllers of phpIPAM API.
229237 Unfortunately the API doesn't report all nor the correct paths for all 'controllers'.
238+
230239 :return: Returns a tuple of controller paths.
231240 :rtype: tuple
232241 """
0 commit comments