-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_call.py
64 lines (47 loc) · 2.4 KB
/
api_call.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
from apimatic_core.configurations.endpoint_configuration import EndpointConfiguration
from apimatic_core.configurations.global_configuration import GlobalConfiguration
from apimatic_core.logger.sdk_logger import LoggerFactory
from apimatic_core.response_handler import ResponseHandler
class ApiCall:
@property
def new_builder(self):
return ApiCall(self._global_configuration)
def __init__(
self,
global_configuration=GlobalConfiguration()
):
self._global_configuration = global_configuration
self._request_builder = None
self._response_handler = ResponseHandler()
self._endpoint_configuration = EndpointConfiguration()
self._api_logger = LoggerFactory.get_api_logger(self._global_configuration.get_http_client_configuration()
.logging_configuration)
def request(self, request_builder):
self._request_builder = request_builder
return self
def response(self, response_handler):
self._response_handler = response_handler
return self
def endpoint_configuration(self, endpoint_configuration):
self._endpoint_configuration = endpoint_configuration
return self
def execute(self):
_http_client_configuration = self._global_configuration.get_http_client_configuration()
if _http_client_configuration.http_client is None:
raise ValueError("An HTTP client instance is required to execute an Api call.")
_http_request = self._request_builder.build(self._global_configuration)
# Logging the request
self._api_logger.log_request(_http_request)
_http_callback = _http_client_configuration.http_callback
# Applying the on before sending HTTP request callback
if _http_callback is not None:
_http_callback.on_before_request(_http_request)
# Executing the HTTP call
_http_response = _http_client_configuration.http_client.execute(
_http_request, self._endpoint_configuration)
# Logging the response
self._api_logger.log_response(_http_response)
# Applying the after receiving HTTP response callback
if _http_callback is not None:
_http_callback.on_after_response(_http_response)
return self._response_handler.handle(_http_response, self._global_configuration.get_global_errors())