-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.py
103 lines (84 loc) · 3.15 KB
/
builder.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
from urllib.parse import urlparse
from urllib.parse import quote
import cb_edp.config.constants as const
from cb_edp.config.manager import ConfigManager
from cb_edp.utils.helpers import Helpers
from cb_edp.utils.validators import Validators
class APIBuilder:
"""
Utils class that includes different methods to build well-formatted solution's API URLs.
"""
@staticmethod
def build_resource_url(fiware_service, fiware_service_path, **kwargs):
"""
Builds a REST API call based on the parameters passed and the specification of solution's API.
:param fiware_service: FIWARE Service which the Data Model belongs to
:param fiware_service_path: FIWARE Service Path where Data Model is located in its service
:param kwargs: Filters to apply to the query done to Orion Context Broker (order matters)
:return: Integration API URL for querying context data
:rtype: str
"""
api_host = APIBuilder.get_host(const.INTEGRATION_API)
orion_host = APIBuilder.get_host(const.INTEGRATION_ORION)
orion_host = APIBuilder.encode_orion(orion_host)
url = '{api_host}/{orion_host}'.format(api_host=api_host, orion_host=orion_host)
url += '/{param}/{value}'.format(param='entity', value=quote(kwargs['entity']))
del kwargs['entity']
for name, value in kwargs.items():
url += '/{param}/{value}'.format(param=name, value=quote(value))
url += APIBuilder.build_parameters(fiware_service, fiware_service_path)
return url
@staticmethod
def get_host(key):
"""
Obtains the host value specified in the config file.
:param str key: Key name of wanted host
:return: Well-formatted host wanted.
:rtype: str
"""
host = ConfigManager.get_value(const.MAIN_SECTION, key)
Validators.is_valid_url(key, host)
return APIBuilder.clean_host(host)
@staticmethod
def encode_orion(orion_host):
"""
Encodes Orion's host in HTTP request compatible format.
:param str orion_host: Orion's reachable address
:return: Encoded Orion host
:rtype: str
"""
orion_host_parsed = urlparse(orion_host)
if orion_host_parsed.path:
orion_host = '{scheme}://{netloc}{path}'.format(scheme=orion_host_parsed.scheme,
netloc=orion_host_parsed.netloc,
path=quote(orion_host_parsed.path))
return Helpers.encode_base64_url(orion_host)
@staticmethod
def clean_host(host):
"""
Remove from a host string those characters not desired.
:param str host: Host value to treat
:return: Cleaned host string
:rtype: str
"""
if host[-1] is '/':
host = host[:-1]
return host
@staticmethod
def build_parameters(fiware_service, fiware_service_path):
"""
Builds the parameters to add to the API call.
:param str fiware_service: FIWARE service
:param fiware_service_path: FIWARE service path
:return: Portion with HTTP request parameters
:rtype: str
"""
params = ''
if fiware_service:
params += const.API_URL_STRUCTURE_FIWARE_SERVICE.format(value=Helpers.encode_base64_url(fiware_service))
if fiware_service_path:
if fiware_service_path[0] is '/':
fiware_service_path = fiware_service_path[1:]
params += const.API_URL_STRUCTURE_FIWARE_SERVICEPATH.format(
value=Helpers.encode_base64_url(fiware_service_path))
return params