1
1
# -*- coding: utf-8 -*-
2
2
# (c) Christian Meißner 2020
3
-
3
+ """Default class to handle all api interactions with phpIPAM server."""
4
4
import re
5
5
import requests
6
6
15
15
16
16
17
17
class Api (object ):
18
+ """The main class.
18
19
19
- """ The main class. It generates tha API object where you can run
20
+ It generates tha API object where you can run
20
21
different actions again to `create`, `update` and `delete` entities.
21
22
It also provides functions with informational character only.
22
23
"""
24
+
23
25
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.
24
29
25
- """ contructor method
26
30
:param url: The URL to a phpIPAM instance. It includes the protocol (`http` or `https`).
27
31
:type url: str
28
32
: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
64
68
self ._login ()
65
69
66
70
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.
67
72
68
- """ Sends queries to phpIPAM API in a generalistic manner
69
73
:param path: Path to the controler and possibly to function to use., defaults to 'user'
70
74
:type path: str, optional
71
75
: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,
121
125
return result ['data' ]
122
126
123
127
def _login (self ):
124
- """ Login method """
128
+ """Login to phpIPAM API and return token. """
125
129
_auth = HTTPBasicAuth (self ._api_username , self ._api_password )
126
130
resp = self ._query (method = POST , auth = _auth )
127
131
128
132
self ._api_token = resp ['token' ]
129
133
130
134
def get_token (self ):
135
+ """Return last login token.
131
136
132
- """ Method to grap last login token
133
137
:return: Returns the api token from the last successful login.
134
138
:rtype: str
135
139
"""
136
140
return self ._api_token
137
141
138
142
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.
139
147
140
- """ Method to get an existing entity
141
148
:param controller: Name of the controller to request entity from.
142
149
:type controller: str
143
150
: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):
158
165
return self ._query (token = self ._api_token , method = GET , path = _path , params = _params )
159
166
160
167
def create_entity (self , controller , controller_path = None , data = None , params = None ):
168
+ """Create an entity.
161
169
162
- """ Create an entity
163
170
:param controller: Name of the controller to use.
164
171
:type controller: str
165
172
: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
182
189
return self ._query (token = self ._api_token , method = POST , path = _path , data = data , params = _params )
183
190
184
191
def delete_entity (self , controller , controller_path , params = None ):
192
+ """Delete an entity.
185
193
186
- """ This method is used to delete an entity.
187
194
:param controller: Name of the controller to use.
188
195
:type controller: str
189
196
: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):
200
207
return self ._query (token = self ._api_token , method = DELETE , path = _path , params = _params )
201
208
202
209
def update_entity (self , controller , controller_path = None , data = None , params = None ):
210
+ """Update an entity.
203
211
204
- """ This method is used to update an entity.
205
212
:param controller: Name of the controller to use.
206
213
:type controller: str
207
214
: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
224
231
return self ._query (token = self ._api_token , method = PATCH , path = _path , data = data , params = _params )
225
232
226
233
def controllers (self ):
234
+ """Report all controllers from phpIPAM API.
227
235
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.
229
237
Unfortunately the API doesn't report all nor the correct paths for all 'controllers'.
238
+
230
239
:return: Returns a tuple of controller paths.
231
240
:rtype: tuple
232
241
"""
0 commit comments