Skip to content

Commit 8f00d1a

Browse files
committed
DLE-190516-Impl get data from json
1 parent 838c37a commit 8f00d1a

File tree

14 files changed

+423
-30
lines changed

14 files changed

+423
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python: Current File",
9+
"type": "python",
10+
"request": "launch",
11+
"program": "${file}",
12+
"console": "integratedTerminal"
13+
}
14+
]
15+
}

config.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,35 @@
77
},
88
{
99
"type": "api",
10-
"api_type": "banking_exchange_rates",
10+
"api_name": "openexchangerates_exchange_rates",
1111
"site_name": "openexchangerates",
1212
"site_domain": "https://openexchangerates.org",
13-
"is_default": "True",
1413
"api_url": "/api/historical/",
1514
"api_extension": ".json",
1615
"api_params": [
1716
{
1817
"param_name": "app_id",
19-
"default_vaule": "e1e21981345b4bbe959f49186802ce97"
18+
"default_value": "e1e21981345b4bbe959f49186802ce97"
2019
},
2120
{
2221
"param_name": "base",
23-
"default_vaule": "USD"
22+
"default_value": "USD"
2423
}
2524
]
2625
},
2726
{
2827
"type": "api",
29-
"api_type": "banking_exchange_rates",
28+
"api_name": "exchangeratesapi_exchange_rates",
3029
"site_name": "exchangeratesapi",
31-
"site_domain": "https://api.exchangeratesapi.io",
32-
"is_default": "False",
30+
"site_domain": "https://api.exchangeratesapi.io",
3331
"api_url": "/",
3432
"api_extension": ".json",
3533
"api_params": [
3634
{
3735
"param_name": "base",
38-
"default_vaule": "USD"
36+
"default_value": "USD"
3937
}
4038
]
41-
},
39+
}
4240
]
4341
}

config.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
sys.path.append(os.path.dirname(ROOT_DIR + r'/'))# Add absolute path to current sys.path
77
# print('Path: ' + str(sys.path))
88

9+
from domains.domain_container import DomainContainer
10+
911
class Configuration():
1012

1113

1214
def __init__(self):
13-
Configuration.ConfigFileName = "config.json"
14-
Configuration.Platform = self.getOSplatform()
15-
self.apiType_banking = "banking_exchange_rates"
15+
self.ConfigFileName = "config.json"
16+
self.ConfigFilePath = Path(ROOT_DIR + r'\\' + self.ConfigFileName)
17+
self.Platform = self.getOSplatform()
18+
self.domain_container = DomainContainer()
19+
self.ApiConfigModel = self.domain_container.init_ModelClass('ApiConfigModel')
1620

1721

1822
def getOSplatform(self):
@@ -28,22 +32,25 @@ def getOSplatform(self):
2832
return os
2933

3034

31-
def getApiConnection(self, apiName, apiType):
35+
def getApiConfig(self, apiName):
3236

3337
try:
34-
filename = Path(ROOT_DIR + r'\\' + Configuration.ConfigFileName)
35-
with open(filename, 'rt') as config_file:
36-
config = json.load(config_file)
37-
# print(config)
38-
for cf in list(config['configurations']):
39-
if(cf['type'] == "api" and cf['api_type'] == self.apiType_banking and cf['is_default'] == "True"):
40-
return cf
41-
42-
return None
38+
if apiName:
39+
40+
with open(self.ConfigFilePath, 'rt') as config_file:
41+
config = json.load(config_file)
42+
# print(config)
43+
for cf in list(config['configurations']):
44+
if(cf['type'] == "api" and cf['api_name'] == apiName):
45+
modelApiConfig = self.domain_container.map_JsonToAnDomainClass(self.ApiConfigModel, cf)
46+
return modelApiConfig
4347

4448
except Exception as ex:
4549
print('Error: ', ex)
50+
51+
return None
4652

53+
4754

4855

4956

-726 Bytes
Binary file not shown.
-966 Bytes
Binary file not shown.
File renamed without changes.

domains/config/ApiConfigModel.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
"""
3+
doc: Model class with the decorator
4+
"""
5+
class ApiConfigModel(object):
6+
"""
7+
A data descriptor that sets and returns values normally and optional prints a message logging their access.
8+
"""
9+
def __init__(self):
10+
pass
11+
12+
13+
@property
14+
def type(self):
15+
return self.__type
16+
@type.setter
17+
def type(self, val):
18+
self.__type = val
19+
20+
@property
21+
def api_name(self):
22+
return self.__api_name
23+
@api_name.setter
24+
def api_name(self, val):
25+
self.__api_name = val
26+
27+
@property
28+
def site_name(self):
29+
return self.__site_name
30+
@site_name.setter
31+
def site_name(self, val):
32+
self.__site_name = val
33+
34+
35+
@property
36+
def site_domain(self):
37+
return self.__site_domain
38+
@site_domain.setter
39+
def site_domain(self, val):
40+
self.__site_domain = val
41+
42+
43+
@property
44+
def api_url(self):
45+
return self.__api_url
46+
@api_url.setter
47+
def api_url(self, val):
48+
self.__api_url = val
49+
50+
51+
@property
52+
def api_extension(self):
53+
return self.__api_extension
54+
@api_extension.setter
55+
def api_extension(self, val):
56+
self.__api_extension = val
57+
58+
@property
59+
def api_params(self):
60+
return self.__api_params
61+
@api_params.setter
62+
def api_params(self, val):
63+
self.__api_params = val

domains/config/ApiParamConfigModel.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
"""
3+
doc: Model class with the decorator
4+
"""
5+
class ApiParamConfigModel(object):
6+
"""
7+
A data descriptor that sets and returns values normally and optional prints a message logging their access.
8+
"""
9+
def __init__(self):
10+
pass
11+
12+
13+
@property
14+
def param_name(self):
15+
return self.__param_name
16+
@param_name.setter
17+
def param_name(self, val):
18+
self.__param_name = val
19+
20+
21+
@property
22+
def default_value(self):
23+
return self.__default_value
24+
@default_value.setter
25+
def default_value(self, val):
26+
self.__default_value = val
27+

domains/domain_container.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import inspect
2+
import sys
3+
4+
5+
from domains.api.ApiOpenExcRateModel import ApiOpenExcRateModel
6+
from domains.config.ApiConfigModel import ApiConfigModel
7+
from domains.config.ApiParamConfigModel import ApiParamConfigModel
8+
9+
"""
10+
doc: Container to declare all entities model
11+
"""
12+
class DomainContainer():
13+
14+
def __init__(self):
15+
pass
16+
17+
18+
def get_allDomainClassRegistered(self):
19+
20+
container = [(name, obj) for name, obj in list(inspect.getmembers(sys.modules[__name__], inspect.isclass)) if name not in DomainContainer.__name__]
21+
return container
22+
23+
24+
def init_ModelClass(self, className):
25+
26+
clsmembers = self.get_allDomainClassRegistered()
27+
for name, obj in clsmembers:
28+
if name in className:
29+
return obj
30+
31+
32+
# def print_ValueDomainClass(self, model):
33+
34+
# if(model is not None and inspect.isclass(type(model))):
35+
# attrs = [ (key, value) for key, value in inspect.getmembers(model)
36+
# if (key not in dir(type('dummy', (object,), {})))
37+
# and not (key.startswith('_') or key.endswith('_'))
38+
# ]
39+
40+
# for key, value in attrs:
41+
# print(str(key) + ': ' + str(value))
42+
43+
44+
# def print_ValueListDomainClass(self, listmodel):
45+
46+
# if(listmodel is not None):
47+
# for item in listmodel:
48+
# print('#'*40)
49+
# self.print_ValueDomainClass(item)
50+
51+
52+
def map_JsonToAnDomainClass(self, modelClass, obj):
53+
54+
if modelClass is None or obj is None:
55+
return None
56+
57+
modelAttrs = [str(item[0]).lower() for item in inspect.getmembers(modelClass) if isinstance(getattr(modelClass, item[0], None), property)]
58+
59+
model = modelClass()
60+
61+
for key, value in obj.items():
62+
if any(item in key for item in modelAttrs):
63+
setattr(model, key, value)#setattr(object, name, value)
64+
65+
return model
66+
67+
68+
def map_ListJsonToListDomainClass(self, modelClass, listObj):
69+
70+
if modelClass is None or not listObj:
71+
return None
72+
73+
modelAttrs = [str(item[0]).lower() for item in inspect.getmembers(modelClass) if isinstance(getattr(modelClass, item[0], None), property)]
74+
listModel = []
75+
for obj in listObj:
76+
model = modelClass()
77+
for key, value in obj.items():
78+
if any(item in key for item in modelAttrs):
79+
setattr(model, key, value)#setattr(object, name, value)
80+
listModel.append(model)
81+
82+
return listModel
83+
84+
85+
def map_UpdateValueToAnDomainClass(self, modelClass, attrName, attrValue):
86+
87+
if attrName is None or attrValue is None:
88+
return None
89+
90+
setattr(modelClass, attrName, attrValue)
91+
92+
return modelClass
93+
94+

0 commit comments

Comments
 (0)