35 from requests.auth
import HTTPBasicAuth
38 from RAPPAuth
import RAPPAuth
39 from requests.adapters
import HTTPAdapter
40 from Adapters
import TLSAdapter
as SSLAdapter
45 """ Service Controller base class implementation """
50 @param service Service: Service instance
51 @param connection dictionary: Connection information.
52 @param timeout int: Connection Timeout value.
57 if self._service.persistent:
65 'user-agent':
'rapp-platform-api/python'
71 """! Check if it is a json string.
74 @return - True if is a json. False otherwise.
84 raise NotImplementedError()
88 """! Return the basename of input filepath. """
89 return path.basename(filepath)
94 """! General member method to perform a .post request to the
97 If files are specified, then multipart/form-data form is used.
98 Otherwhise, x-www-urlencoded form is used.
100 @param session The session oject to use for this request.
101 @param urlpath The complete urlpath of the request.
102 @param data The data to send. Literal.
103 @param files Files to send.
105 @return dictionary - Platform Service response.
108 _payload = {
'json': payload.make_json()}
111 _files.append(f.make_tuple())
113 response = {
'error':
''}
121 timeout=self._service.timeout,
125 header = resp.headers
127 resp.raise_for_status()
128 except Exception
as e:
135 if "application/json" in resp.headers[
'content-type']:
136 response = json.loads(resp.content)
137 elif "This service is unknown!" in resp.content:
139 'error':
'Connection Error. Connection could not be established at %s' %self._service.url
144 'payload': resp.content,
145 'error':
'Non application/json response'
152 """! Post Request while initiating a new session
154 @param data dictionary - the data payload to send.
155 @param files Array - Array of serialized File objects to send.
157 with requests.Session()
as session:
164 """! Post Request using active session - persistent connection.
166 @param data dictionary - the data payload to send.
167 @param files Array - Array of serialized File objects to send.
173 """! Mount http and https Transport Adapters to the session
175 @param session Session - The session to mount the adapters.
177 session.mount(
"http://", HTTPAdapter())
178 session.mount(
"https://", SSLAdapter())
182 """! Handles exceptions and return an error message that complies to
183 the Exception caught.
185 @param exc Exception of any type
190 if type(exc)
is ConnectionError:
191 errorMsg =
"Connection Error"
192 elif "401" in str(exc):
195 elif "500" in str(exc):
198 elif "404" in str(exc):
201 elif type(exc)
is HTTPError:
203 elif type(exc)
is ConnectTimeout:
204 errorMsg =
"The request timed out while trying to connect to the remote server"
205 elif type(exc)
is ReadTimeout:
206 errorMsg =
"The server did not send any data in the allotted amount of time."
207 elif type(exc)
is Timeout:
208 errorMsg =
"Connection Timeout exception."
210 errorMsg =
"Catched Exception %s" %exc
def basename
Return the basename of input filepath.
_service
Cloud Service instance.
def _post_persistent
Post Request using active session - persistent connection.
def handle_exception
Handles exceptions and return an error message that complies to the Exception caught.
def __mount_adapters
Mount http and https Transport Adapters to the session.
def _post_session_once
Post Request while initiating a new session.
def is_json
Check if it is a json string.
def post_request
General member method to perform a .post request to the.