-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_poll_config.py
187 lines (167 loc) · 8.32 KB
/
api_poll_config.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
"""
api_poll_config.py
Loads configuration to handle API polling
- Creates a dictionaries for API endpoints containing items(keys) to store from an API
- Loads polling interval per endpoint
- Loads sending strategies which can be specified at different levels.
- More information in the sample YAML configuration file
TODO: switch print statments to debug logs
"""
import pprint # dont really need this except for main()
import logging
import yaml
# store the sending strategy for endpoints here...
# queried as sending_strategy[endpoint][key][strategy]
# or sending_strategy[endpoint]['polling_interval']
# sending_strategy={}
def check_fixed_sending_strategy_list( fixed_list ):
"""returns a valid sorted list with unique values.
Values should be int and in range 0-59
"""
return_list = [] # cleaned list
for i in set(fixed_list): # iterate unique values
logging.debug(f'**check fixed: value {i} {type(i)}')
if type(i) is int: # check type
if i >= 0 and i < 60: # check range
return_list.append(i)
else:
logging.warning(f'check_fixed.. value {i} in fixed out of range, ignoring it.')
else:
logging.warning(f'check_fixed.. value {i} is not of type int, ignoring it.')
return sorted(return_list)
def get_sending_strategy( sending_strategy, upper_strategy , sending_default ):
"""Reads sending strategy from a top level of the configuration data in sending_strategy.
If there is no sending_strategy at the current level, the upper level or defaults are used.
"""
sending_strategy_return = {}
logging.debug(f'get_sending_strategy()\n in: {sending_strategy}\n'
f' upper: {upper_strategy}\n'
f'default: {sending_default}')
# handle current level
if 'always' in sending_strategy:
if sending_strategy['always'] is True:
sending_strategy_return = { 'always': True }
return sending_strategy_return
if 'stale' in sending_strategy:
sending_strategy_return['stale'] = sending_strategy['stale']
else:
if 'fixed' in sending_strategy:
# check all are int and 0-59
sending_strategy_return['fixed'] = \
check_fixed_sending_strategy_list(sending_strategy['fixed'])
if 'previous' in sending_strategy:
logging.debug(f'sending_strategy_previous {sending_strategy}')
if sending_strategy['previous'] is True:
#TODO: test and check that just previous will set changes. too
#sending_strategy_return.update( {'changes': True, 'previous': True} )
sending_strategy_return.update( {'previous': True} )
else:
sending_strategy_return['previous'] = False
if 'changes' in sending_strategy:
if sending_strategy['changes'] is True:
sending_strategy_return['changes'] = True
else:
sending_strategy_return['changes'] = False
logging.debug(f'get_sending_strategy(): CURRENT: {sending_strategy_return} '
f'type {type(sending_strategy_return)} len{len(sending_strategy_return)}' )
#
# if think we just need to check if nothing is set in the return variable
# and repeat the above maybe with upper? if then default?
# use upper strategy if current is empty
if len(sending_strategy_return) == 0:
sending_strategy_return = upper_strategy
logging.debug("EMPTY: using upper_strategy")
# if the upper strategy was empty too evaluate defaults
if len(sending_strategy_return) == 0:
logging.debug("EMPTY: using calling ourself to get defaults")
sending_strategy_return = get_sending_strategy( sending_default, {}, sending_default )
logging.debug(f'get_sending_strategy(): RETURN: {sending_strategy_return}')
return sending_strategy_return
def load_key_prefix_config(api_config):
"""returns key prefix"""
return api_config['key_prefix']
def load_api_endpoint_key_config(api_config):
"""
Loads config and returns dictionary of endpoints, keys and their configuration
The return dict is as follows
'endpoint_name' : { 'keys_names : { key_specific_parameters }
TODO: some errors if something important missing
"""
api_endpoint_key_config={}
#default sending strategy in top level
sending_strategy_default= api_config['sending_strategy_default']
# iterate endpoints
endpoints = api_config['endpoint']
for endpoint in endpoints:
current_sending_strategy={}
logging.debug( 'NAME***' + endpoint['name'])
logging.debug( 'polling_interval***' + repr(endpoint['polling_interval']))
logging.debug( f'*** FOR1 {endpoint.keys()}' )
sending_strategy_endpoint = get_sending_strategy( endpoint,
current_sending_strategy,
sending_strategy_default)
logging.debug( f' sending_strategy_endpoint: {sending_strategy_endpoint}')
api_endpoint_key_config[endpoint['name']] = {}
# get keys and apply strategies
for keylist in endpoint.keys():
logging.debug( f'FOR2 keylist: {keylist} in {endpoint.keys()}')
logging.debug( f' sending_strategy_endpoint: {sending_strategy_endpoint}')
# if type is dict check if it is a keylist and iterate
if type(endpoint[keylist]) is dict:
if 'keys' in endpoint[keylist]:
sending_strategy_keylist = get_sending_strategy( endpoint[keylist],
sending_strategy_endpoint,
sending_strategy_default)
for current_key in endpoint[keylist]['keys']:
logging.debug( f'FOR3a current_key {current_key}' )
for k in endpoint[keylist]['keys']:
logging.debug( f'KEY: {k}')
kdict= { k : sending_strategy_keylist}
api_endpoint_key_config[endpoint['name']].update( kdict )
else:
logging.debug(f'FOR3a skipping {current_key}')
# if type is not dict check if it is a keylist and iterate
else:
if 'key' == keylist or 'keys' == keylist:
logging.debug(f'FOR3b keylist {keylist} '
'endpoint[keylist] {endpoint[keylist]}')
for k in endpoint[keylist]:
logging.debug( f'KEY(keys): {k}')
kdict = { k : sending_strategy_endpoint}
api_endpoint_key_config[endpoint['name']].update( kdict )
else: # TODO: this isn't used anymore I think
logging.debug(f'FOR3b skipping {endpoint[keylist]} '
'(not dict) or not a key list')
logging.debug('*************')
return api_endpoint_key_config #a big dictionary of dictionaries.
def load_polling_interval_minimum(api_config):
"""returns lowest polling interval from the config. checks top level
polling_interval_minimum and polling_interval in the endpoints."""
try:
polling_interval_minimum = api_config['polling_interval_minimum']
except KeyError:
polling_interval_minimum = 0
endpoints = api_config['endpoint']
for endpoint in endpoints:
if polling_interval_minimum == 0:
polling_interval_minimum = endpoint['polling_interval']
elif polling_interval_minimum > endpoint['polling_interval']:
polling_interval_minimum = endpoint['polling_interval']
return polling_interval_minimum
def main():
"""Example use"""
logging.basicConfig(level=logging.INFO)
# Load api config from file
api_config = yaml.safe_load(open('tests/api_design_test.yml'))
#api_config = yaml.safe_load(open('api_design.yml'))
# get the config
endpoint_key_config = load_api_endpoint_key_config(api_config)
# pretty print it
print('***** api_config[\'endpoint\']')
pprint.pp(api_config['endpoint'])
print('***** endpoint_key_config (dict)')
pprint.pp(endpoint_key_config)
print('*****')
print(f'key_prefix: {load_key_prefix_config(api_config)}')
print(f'polling_interval_minimum: {load_polling_interval_minimum(api_config)}')
#main()