11# -*- coding: utf-8 -*-
22# (c) Christian Meißner 2020
33
4- import json
54import re
65import requests
76
1615
1716
1817class Api (object ):
18+
1919 """ The main class. It generates tha API object where you can run
2020 different actions again to `create`, `update` and `delete` entities.
2121 It also provides functions with informational character only.
2222 """
23-
2423 def __init__ (self , url , app_id , username = None , password = None , token = None , encryption = False , timeout = None , ssl_verify = True , user_agent = None ):
25- """ contructor method
2624
25+ """ contructor method
2726 :param url: The URL to a phpIPAM instance. It includes the protocol (`http` or `https`).
2827 :type url: str
2928 :param app_id: The app_id which is used for the API operations.
@@ -45,7 +44,6 @@ def __init__(self, url, app_id, username=None, password=None, token=None, encryp
4544 :param user_agent: With this parameter you can define a own user agent header string., defaults to None
4645 :type user_agent: str, optional
4746 """
48-
4947 self ._api_url = url
5048 self ._api_appid = app_id
5149 self ._api_username = username
@@ -66,8 +64,8 @@ def __init__(self, url, app_id, username=None, password=None, token=None, encryp
6664 self ._login ()
6765
6866 def _query (self , path = 'user' , headers = None , method = GET , data = None , params = None , auth = None , token = None ):
69- """ Sends queries to phpIPAM API in a generalistic manner
7067
68+ """ Sends queries to phpIPAM API in a generalistic manner
7169 :param path: Path to the controler and possibly to function to use., defaults to 'user'
7270 :type path: str, optional
7371 :param headers: Optional request headers, defaults to None
@@ -89,7 +87,6 @@ def _query(self, path='user', headers=None, method=GET, data=None, params=None,
8987 :return: If query returns any data it returns this dict or list else it returns last exit status
9088 :rtype: Union[bool, dict, list]
9189 """
92-
9390 _api_path = path
9491 _api_headers = headers or {}
9592 _method = method
@@ -124,26 +121,24 @@ def _query(self, path='user', headers=None, method=GET, data=None, params=None,
124121 return result ['data' ]
125122
126123 def _login (self ):
127- """ Login method
128- """
129124
125+ """ Login method """
130126 _auth = HTTPBasicAuth (self ._api_username , self ._api_password )
131127 resp = self ._query (method = POST , auth = _auth )
132128
133129 self ._api_token = resp ['token' ]
134130
135131 def get_token (self ):
136- """ Method to grap last login token
137132
133+ """ Method to grap last login token
138134 :return: Returns the api token from the last successful login.
139135 :rtype: str
140136 """
141-
142137 return self ._api_token
143138
144139 def get_entity (self , controller , controller_path = None , params = None ):
145- """ Method to get an existing entity
146140
141+ """ Method to get an existing entity
147142 :param controller: Name of the controller to request entity from.
148143 :type controller: str
149144 :param controller_path: The path which is used to query for entities, defaults to None
@@ -154,7 +149,6 @@ def get_entity(self, controller, controller_path=None, params=None):
154149 :return: Result of the query. It can be either a 'list' or 'dict'.
155150 :rtype: Union[dict, list]
156151 """
157-
158152 _path = controller
159153 _controller_path = controller_path
160154 _params = params
@@ -165,8 +159,8 @@ def get_entity(self, controller, controller_path=None, params=None):
165159 return self ._query (token = self ._api_token , method = GET , path = _path , params = _params )
166160
167161 def create_entity (self , controller , controller_path = None , data = None , params = None ):
168- """ Create an entity
169162
163+ """ Create an entity
170164 :param controller: Name of the controller to use.
171165 :type controller: str
172166 :param controller_path: The path which is used to query for entities, defaults to None
@@ -179,7 +173,6 @@ def create_entity(self, controller, controller_path=None, data=None, params=None
179173 :return: Returns the newly created entity.
180174 :rtype: Union[dict, list]
181175 """
182-
183176 _path = controller
184177 _controller_path = controller_path
185178 _params = params
@@ -190,8 +183,8 @@ def create_entity(self, controller, controller_path=None, data=None, params=None
190183 return self ._query (token = self ._api_token , method = POST , path = _path , data = data , params = _params )
191184
192185 def delete_entity (self , controller , controller_path , params = None ):
193- """ This method is used to delete an entity.
194186
187+ """ This method is used to delete an entity.
195188 :param controller: Name of the controller to use.
196189 :type controller: str
197190 :param controller_path: The path wich is used to access the entity to delete.
@@ -202,15 +195,14 @@ def delete_entity(self, controller, controller_path, params=None):
202195 :return: Returns True if entity was deleted successfully or either 'dict' or 'list' of entities to work on.
203196 :rtype: Union[book, dict, list]
204197 """
205-
206198 _path = '{}/{}' .format (controller , controller_path )
207199 _params = params
208200
209201 return self ._query (token = self ._api_token , method = DELETE , path = _path , params = _params )
210202
211203 def update_entity (self , controller , controller_path = None , data = None , params = None ):
212- """ This method is used to update an entity.
213204
205+ """ This method is used to update an entity.
214206 :param controller: Name of the controller to use.
215207 :type controller: str
216208 :param controller_path: The path which is used to access the entity to update., defaults to None
@@ -223,7 +215,6 @@ def update_entity(self, controller, controller_path=None, data=None, params=None
223215 :return: Returns either a 'dict' or 'list' of the changed entity
224216 :rtype: Union[dict, list]
225217 """
226-
227218 _path = controller
228219 _controller_path = controller_path
229220 _params = params
@@ -234,13 +225,12 @@ def update_entity(self, controller, controller_path=None, data=None, params=None
234225 return self ._query (token = self ._api_token , method = PATCH , path = _path , data = data , params = _params )
235226
236227 def controllers (self ):
228+
237229 """ This method is used to report all known controllers of phpIPAM API.
238230 Unfortunately the API doesn't report all nor the correct paths for all 'controllers'.
239-
240231 :return: Returns a tuple of controller paths.
241232 :rtype: tuple
242233 """
243-
244234 result = self ._query (token = self ._api_token , method = OPTIONS , path = '/' )
245235
246236 controllers = ({re .sub (r'^/api/' + self ._api_appid + '/(.+)/$' , r'\1' , v ) for ctrl in result ['controllers' ] for (k , v ) in ctrl .items () if k == 'href' })
0 commit comments