1
1
# -*- coding: utf-8 -*-
2
2
# (c) Christian Meißner 2020
3
3
4
- import json
5
4
import re
6
5
import requests
7
6
16
15
17
16
18
17
class Api (object ):
18
+
19
19
""" The main class. It generates tha API object where you can run
20
20
different actions again to `create`, `update` and `delete` entities.
21
21
It also provides functions with informational character only.
22
22
"""
23
-
24
23
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
26
24
25
+ """ contructor method
27
26
:param url: The URL to a phpIPAM instance. It includes the protocol (`http` or `https`).
28
27
:type url: str
29
28
: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
45
44
:param user_agent: With this parameter you can define a own user agent header string., defaults to None
46
45
:type user_agent: str, optional
47
46
"""
48
-
49
47
self ._api_url = url
50
48
self ._api_appid = app_id
51
49
self ._api_username = username
@@ -66,8 +64,8 @@ def __init__(self, url, app_id, username=None, password=None, token=None, encryp
66
64
self ._login ()
67
65
68
66
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
70
67
68
+ """ Sends queries to phpIPAM API in a generalistic manner
71
69
:param path: Path to the controler and possibly to function to use., defaults to 'user'
72
70
:type path: str, optional
73
71
: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,
89
87
:return: If query returns any data it returns this dict or list else it returns last exit status
90
88
:rtype: Union[bool, dict, list]
91
89
"""
92
-
93
90
_api_path = path
94
91
_api_headers = headers or {}
95
92
_method = method
@@ -124,26 +121,24 @@ def _query(self, path='user', headers=None, method=GET, data=None, params=None,
124
121
return result ['data' ]
125
122
126
123
def _login (self ):
127
- """ Login method
128
- """
129
124
125
+ """ Login method """
130
126
_auth = HTTPBasicAuth (self ._api_username , self ._api_password )
131
127
resp = self ._query (method = POST , auth = _auth )
132
128
133
129
self ._api_token = resp ['token' ]
134
130
135
131
def get_token (self ):
136
- """ Method to grap last login token
137
132
133
+ """ Method to grap last login token
138
134
:return: Returns the api token from the last successful login.
139
135
:rtype: str
140
136
"""
141
-
142
137
return self ._api_token
143
138
144
139
def get_entity (self , controller , controller_path = None , params = None ):
145
- """ Method to get an existing entity
146
140
141
+ """ Method to get an existing entity
147
142
:param controller: Name of the controller to request entity from.
148
143
:type controller: str
149
144
: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):
154
149
:return: Result of the query. It can be either a 'list' or 'dict'.
155
150
:rtype: Union[dict, list]
156
151
"""
157
-
158
152
_path = controller
159
153
_controller_path = controller_path
160
154
_params = params
@@ -165,8 +159,8 @@ def get_entity(self, controller, controller_path=None, params=None):
165
159
return self ._query (token = self ._api_token , method = GET , path = _path , params = _params )
166
160
167
161
def create_entity (self , controller , controller_path = None , data = None , params = None ):
168
- """ Create an entity
169
162
163
+ """ Create an entity
170
164
:param controller: Name of the controller to use.
171
165
:type controller: str
172
166
: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
179
173
:return: Returns the newly created entity.
180
174
:rtype: Union[dict, list]
181
175
"""
182
-
183
176
_path = controller
184
177
_controller_path = controller_path
185
178
_params = params
@@ -190,8 +183,8 @@ def create_entity(self, controller, controller_path=None, data=None, params=None
190
183
return self ._query (token = self ._api_token , method = POST , path = _path , data = data , params = _params )
191
184
192
185
def delete_entity (self , controller , controller_path , params = None ):
193
- """ This method is used to delete an entity.
194
186
187
+ """ This method is used to delete an entity.
195
188
:param controller: Name of the controller to use.
196
189
:type controller: str
197
190
: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):
202
195
:return: Returns True if entity was deleted successfully or either 'dict' or 'list' of entities to work on.
203
196
:rtype: Union[book, dict, list]
204
197
"""
205
-
206
198
_path = '{}/{}' .format (controller , controller_path )
207
199
_params = params
208
200
209
201
return self ._query (token = self ._api_token , method = DELETE , path = _path , params = _params )
210
202
211
203
def update_entity (self , controller , controller_path = None , data = None , params = None ):
212
- """ This method is used to update an entity.
213
204
205
+ """ This method is used to update an entity.
214
206
:param controller: Name of the controller to use.
215
207
:type controller: str
216
208
: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
223
215
:return: Returns either a 'dict' or 'list' of the changed entity
224
216
:rtype: Union[dict, list]
225
217
"""
226
-
227
218
_path = controller
228
219
_controller_path = controller_path
229
220
_params = params
@@ -234,13 +225,12 @@ def update_entity(self, controller, controller_path=None, data=None, params=None
234
225
return self ._query (token = self ._api_token , method = PATCH , path = _path , data = data , params = _params )
235
226
236
227
def controllers (self ):
228
+
237
229
""" This method is used to report all known controllers of phpIPAM API.
238
230
Unfortunately the API doesn't report all nor the correct paths for all 'controllers'.
239
-
240
231
:return: Returns a tuple of controller paths.
241
232
:rtype: tuple
242
233
"""
243
-
244
234
result = self ._query (token = self ._api_token , method = OPTIONS , path = '/' )
245
235
246
236
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