forked from Bandwidth/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_controller.py
More file actions
93 lines (67 loc) · 3.01 KB
/
base_controller.py
File metadata and controls
93 lines (67 loc) · 3.01 KB
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
# -*- coding: utf-8 -*-
"""
bandwidth
This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ).
"""
from bandwidth.api_helper import APIHelper
from bandwidth.exceptions.api_exception import APIException
class BaseController(object):
"""All controllers inherit from this base class.
Attributes:
config (Configuration): The HttpClient which a specific controller
instance will use. By default all the controller objects share
the same HttpClient. A user can use his own custom HttpClient
as well.
http_call_back (HttpCallBack): An object which holds call back
methods to be called before and after the execution of an HttpRequest.
global_headers (dict): The global headers of the API which are sent with
every request.
"""
global_headers = {
'user-agent': 'python-sdk-refs/tags/python6.0.0'
}
def __init__(self, config, call_back=None):
self._config = config
self._http_call_back = call_back
@property
def config(self):
return self._config
@property
def http_call_back(self):
return self._http_call_back
def validate_parameters(self, **kwargs):
"""Validates required parameters of an endpoint.
Args:
kwargs (dict): A dictionary of the required parameters.
"""
for name, value in kwargs.items():
if value is None:
raise ValueError("Required parameter {} cannot be None.".format(name))
def execute_request(self, request, binary=False):
"""Executes an HttpRequest.
Args:
request (HttpRequest): The HttpRequest to execute.
binary (bool): A flag which should be set to True if
a binary response is expected.
Returns:
HttpResponse: The HttpResponse received.
"""
# Invoke the on before request HttpCallBack if specified
if self.http_call_back is not None:
self.http_call_back.on_before_request(request)
# Add global headers to request
request.headers = APIHelper.merge_dicts(self.global_headers, request.headers)
# Invoke the API call to fetch the response.
func = self.config.http_client.execute_as_binary if binary else self.config.http_client.execute_as_string
response = func(request)
# Invoke the on after response HttpCallBack if specified
if self.http_call_back is not None:
self.http_call_back.on_after_response(response)
return response
def validate_response(self, response):
"""Validates an HTTP response by checking for global errors.
Args:
response (HttpResponse): The HttpResponse of the API call.
"""
if (response.status_code < 200) or (response.status_code > 208): # [200,208] = HTTP OK
raise APIException('HTTP response not OK.', response)