-
Notifications
You must be signed in to change notification settings - Fork 0
/
cchub_api_base_client.py
380 lines (324 loc) · 11.3 KB
/
cchub_api_base_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
'''basic class to act as a client for the CCHUB Api'''
# https://{SERVER_ADDRESS}/api/v{VERSION}/{MODEL}/{NAME}.json?accessToken={ACCESS_TOKEN}&{OPTIONAL_PARAMS}
# Date Format: "yyyy-mm-dd hh:mm:ss", for example "2019-05-23 14:22:59"
from configparser import ConfigParser
from requests import Request, Session, RequestException
from requests.adapters import HTTPAdapter
from functools import partialmethod
from php import Php
def set_model_method(cls):
verbs = ['get', 'post', 'put', 'delete']
for model in cls.models:
for verb in verbs:
func_name = f'{verb}_{model}'
setattr(cls, func_name, partialmethod(cls.model_func, model, verb, ))
return cls
def set_get_all_method(cls):
for model in cls.models:
setattr(cls, f'get_all_{model}', partialmethod(cls.get_all, model))
return cls
@set_get_all_method
@set_model_method
class CchubApiBaseClient:
"""
A basic class to act as a client for the CCHUB API.
This class is expected to be extended with business logic for middleware purposes.
Scope:
- Handles authentication, session, and retry functionality.
- Provides basic methods for the API endpoints.
Added dynamic methods:
- self.get_{model}
- self.get_accounts() # get first page. Default pagesize is 100
- self.get_accounts('name_4385245245') # get single item
- self.post_{model}(json={...}) # add single item
- self.put_{model}(cchub_uid, json={...}) # update single item
- self.get_all_{model} # get all items
JSON structure:
{
'title': 'string',
'database': {
'title': 'string'
},
'customFields': {
'MyCustomField': 'string'
},
}
Parameters:
- json (dict): The JSON data to be sent in the request body.
Params:
{
'skip': 0, # offset
'take': 20, # pagesize
'sort': [
{"field": "firstname", "dir": "desc"},
{"field": "lastname", "dir": "asc"}
], # custom field example: "field": "customFields.telefon"
"filter": {
"field": "firstname",
"operator": "eq", # various operators available
"value": "John"
}, # filter can be a list of filter dicts
"fields": [
"firstname",
"lastname",
"account.title"
], # limit the fields which are returned
}
Response structure:
{
"error": [],
"result": {
"data": [
{
"title": "string",
"database": {
"title": "string"
},
"customFields": {
"MyCustomField": "string"
},
}
],
"total": 0
},
}
"""
# There are dynamic methods for each model
models = [
'activities',
'activitiesCall',
'activitiesEmail',
'activitiesWeb',
'activitiesSms',
'activitiesFbm',
'activitiesIgdm',
'activitiesWap',
'activitiesVbr',
'accounts',
'contacts',
'crmRecords',
'crmDatabases',
'campaignsRecords',
'campaignsTypes',
'databases',
'groups',
'pauses',
'queues',
'statuses',
'templates',
'tickets',
'ticketsCategories',
'users',
]
def __init__(self, server_address, token, api_version='6'):
"""
Initialize the CchubApiBaseClient.
Parameters:
- server_address (str): The address of the CCHUB server.
- token (str): The access token for authentication.
- api_version (str): The version of the API to use (default is '6').
"""
self.base_url = f'{server_address}'
self.access_token = token
self.version_url = f'/api/v{api_version}'
self.session = Session()
adapter = HTTPAdapter(max_retries=3)
self.session.mount(self.base_url, adapter)
self.session.headers["Content-Type"] = "application/json"
self.timeout = 10
def model_func(self, model, verb, *args, **kwargs):
"""
Perform a HTTP request for a model using the specified verb.
This function is used by the decorator to dynamically create methods for HTTP verbs for each model.
For example, `self.get_activities()` or `self.post_account(json)`.
Parameters:
- model (str): The name of the model.
- verb (str): The HTTP verb to use for the request.
- args: Positional arguments for the request.
- kwargs: Keyword arguments for the request.
Returns:
- object: The response object.
Raises:
- KeyError: If the specified verb is not supported.
"""
uid = args[0] if args else None
simulate = kwargs.pop('simulate', False)
if uid:
endpoint = f'{self.version_url}/{model}/{uid}.json'
else:
endpoint = f'{self.version_url}/{model}.json'
verb_dict = {
'get': self.get,
'post': self.post,
'put': self.put,
'delete': self.delete,
}
if simulate:
return self.simulate(endpoint, verb, **kwargs)
else:
return verb_dict[verb](endpoint, **kwargs)
def _auth(self, params=None):
"""
Add the access token to the params variable.
Parameters:
- params (dict): The parameters to be sent in the request.
Returns:
- dict: The updated parameters with the access token.
"""
if params:
params['accessToken'] = self.access_token
else:
params = {'accessToken': self.access_token}
return params
def _make_request(self, method, endpoint, **kwargs):
"""
Make a request to the API.
Parameters:
- method (str): The HTTP method to use for the request.
- endpoint (str): The API endpoint to send the request to.
- kwargs: Keyword arguments for the request.
Returns:
- object: The response object.
"""
params = kwargs.get('params', None)
try:
del kwargs['params']
except KeyError:
pass
params = self._auth(params)
param_string = Php.http_build_query(params)
url = f'{self.base_url}{endpoint}?{param_string}'
req = Request(
method,
url,
**kwargs)
prepped = req.prepare()
try:
response = self.session.send(prepped, timeout=self.timeout)
return response
except RequestException as error:
print(f"Error fetching data from the API: {error}")
return None
def get(self, endpoint, **kwargs):
"""
Perform a GET request.
Parameters:
- endpoint (str): The API endpoint to send the request to.
- kwargs: Keyword arguments for the request.
Returns:
- object: The response object.
"""
return self._make_request('GET', endpoint, **kwargs)
def post(self, endpoint, **kwargs):
"""
Perform a POST request.
Parameters:
- endpoint (str): The API endpoint to send the request to.
- kwargs: Keyword arguments for the request.
Returns:
- object: The response object.
"""
return self._make_request('POST', endpoint, **kwargs)
def put(self, endpoint, **kwargs):
"""
Perform a PUT request.
Parameters:
- endpoint (str): The API endpoint to send the request to.
- kwargs: Keyword arguments for the request.
Returns:
- object: The response object.
"""
return self._make_request('PUT', endpoint, **kwargs)
def delete(self, endpoint, **kwargs):
"""
Perform a DELETE request.
Parameters:
- endpoint (str): The API endpoint to send the request to.
- kwargs: Keyword arguments for the request.
Returns:
- object: The response object.
"""
return self._make_request('DELETE', endpoint, **kwargs)
def simulate(self, endpoint, method, params=None, **kwargs):
"""
Simulate a request.
Parameters:
- endpoint (str): The API endpoint to send the request to.
- method (str): The HTTP method to use for the request.
- params (dict): The parameters to be sent in the request.
- kwargs: Keyword arguments for the request.
Returns:
- object: The response object.
"""
if params:
params['_method'] = method.upper()
else:
params = {'_method': method.upper()}
return self._make_request('GET', endpoint, params=params, **kwargs)
def get_all(self, model, **kwargs):
"""
Retrieve data from the API with paging support.
Parameters:
- model (str): The name of the model.
- kwargs: Keyword arguments for the request.
Returns:
- dict: A dictionary containing the retrieved data.
"""
all_data = {
'result': {
'data': [],
'total': 0,
},
'error': [],
'status_code': [],
}
position = 0
# Find the method to use
do = f'get_{model}'
if hasattr(self, do) and callable(func := getattr(self, do)):
method = func
else:
raise Exception(f'No method for {do}')
# Loop until all pages have been retrieved
while True:
# Set the page parameter in the request if provided
try:
params = kwargs['params']
except KeyError:
params = {}
params['skip'] = position
params['take'] = 100
# Make the API request
response = method(params=params)
# Check for errors
if response.status_code != 200:
print(f"Error: Unable to retrieve data. Status code: {response.status_code}")
break
# Add the retrieved items to the result list
all_data['status_code'].append(response.status_code)
res = response.json()
all_data['error'].append(res['error'])
all_data['result']['data'].extend(res['result']['data'])
# Get total from the first page
if position == 0:
all_data['result']['total'] = res['result']['total']
# Check if there are more pages
total = res['result']['total']
pagesize = params['take']
if total > position:
position += pagesize
else:
break
return all_data
# Example interpreter usage:
# file = open('cchub_api_base_client.py')
# exec(file.read())
if __name__ == "__main__":
from pprint import PrettyPrinter
pp = PrettyPrinter()
config = ConfigParser()
config.read('config.ini')
base_url = config['API']['SERVER_ADDRESS']
version = config['API']['VERSION']
access_token = config['API']['ACCESS_TOKEN']
ccapi = CchubApiBaseClient(base_url, access_token, version)