summaryrefslogtreecommitdiffstats
path: root/osdf/operation
diff options
context:
space:
mode:
authorAnkitkumar Patel <ankit@research.att.com>2018-02-11 17:51:13 -0500
committerAnkitkumar Patel <ankit@research.att.com>2018-02-11 17:52:51 -0500
commit0b855c08fd98fb8fa0f4bc40d8df430c897b4bad (patch)
treeefdd3c7ab31be64080dd71951a64d13f0ba493de /osdf/operation
parentbb8471cae394aa6ff0af8ba3e5354f3b121c56fc (diff)
Re-org folders, onboard test folder, test config
Reorganized the folder structure. Onboarded testcases. Added test config. Issue-ID: OPTFRA-74 Change-Id: I97882a162a405a18ffd287495039e15ae9d0ad7b Signed-off-by: Ankitkumar Patel <ankit@research.att.com>
Diffstat (limited to 'osdf/operation')
-rw-r--r--osdf/operation/__init__.py0
-rw-r--r--osdf/operation/error_handling.py93
-rw-r--r--osdf/operation/exceptions.py40
-rw-r--r--osdf/operation/responses.py39
4 files changed, 172 insertions, 0 deletions
diff --git a/osdf/operation/__init__.py b/osdf/operation/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/osdf/operation/__init__.py
diff --git a/osdf/operation/error_handling.py b/osdf/operation/error_handling.py
new file mode 100644
index 0000000..dfb0848
--- /dev/null
+++ b/osdf/operation/error_handling.py
@@ -0,0 +1,93 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2015-2017 AT&T Intellectual Property
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# -------------------------------------------------------------------------
+#
+
+import json
+
+from schematics.exceptions import DataError
+
+from requests import RequestException
+from requests import ConnectionError, HTTPError, Timeout
+from osdf.operation.exceptions import BusinessException
+
+import osdf
+
+ERROR_TEMPLATE = osdf.ERROR_TEMPLATE
+
+MESSAGE_BASE = "A solution couldn't be determined because an external application"
+HTTP_ERROR_MESSAGE = MESSAGE_BASE + " returned a HTTP error"
+TIMEOUT_ERROR_MESSAGE = MESSAGE_BASE + " could not respond in time, please check the external application"
+CONNECTION_ERROR_MESSAGE = MESSAGE_BASE + " could not be reached"
+
+internal_error_body = {
+ "serviceException": {
+ "text": "Unhandled internal exception, request could not be processed"
+ }
+}
+
+internal_error_message = json.dumps(internal_error_body)
+
+
+def build_json_error_body(error):
+ if isinstance(error,RequestException):
+ return request_exception_to_json_body(error)
+ elif isinstance(error, DataError):
+ return data_error_to_json_body(error)
+ elif type(error) is BusinessException: # return the error message, because it is well formatted
+ return ERROR_TEMPLATE.render(description=str(error))
+ else:
+ return internal_error_message
+
+
+def data_error_to_json_body(error):
+ description = str(error).replace('"', '\\"')
+ error_message = ERROR_TEMPLATE.render(description=description)
+ return error_message
+
+
+def request_exception_to_json_body(error):
+ friendly_message = "A request exception has occurred when contacting an external system"
+ if type(error) is HTTPError:
+ friendly_message = HTTP_ERROR_MESSAGE
+ if type(error) is ConnectionError:
+ friendly_message = CONNECTION_ERROR_MESSAGE
+ if type(error) is Timeout:
+ friendly_message = TIMEOUT_ERROR_MESSAGE
+
+ eie_body = {
+ "serviceException": {
+ "text": friendly_message,
+ "errorType": "InterfaceError"
+ },
+ "externalApplicationDetails": {
+ "httpMethod": error.request.method,
+ "url": error.request.url
+ }
+ }
+
+ response = error.response
+
+ if response is not None:
+ eie_body['externalApplicationDetails']['httpStatusCode'] = response.status_code
+ content_type = response.headers.get('content-type')
+ if content_type is not None:
+ if 'application/json' in content_type:
+ eie_body['externalApplicationDetails']['responseMessage'] = response.json()
+ elif 'text/html' in content_type:
+ eie_body['externalApplicationDetails']['responseMessage'] = response.text
+ error_message = json.dumps(eie_body)
+ return error_message
diff --git a/osdf/operation/exceptions.py b/osdf/operation/exceptions.py
new file mode 100644
index 0000000..5277b01
--- /dev/null
+++ b/osdf/operation/exceptions.py
@@ -0,0 +1,40 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2015-2017 AT&T Intellectual Property
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# -------------------------------------------------------------------------
+#
+
+class BusinessException(Exception):
+ pass
+
+
+class MessageBusConfigurationException(Exception):
+ pass
+
+
+class CMDataError(Exception):
+ pass
+
+
+class CMSOExecutionError(Exception):
+ pass
+
+
+class CMSOCallBackError(Exception):
+ pass
+
+
+class CMSOInvalidRequestException(Exception):
+ pass
diff --git a/osdf/operation/responses.py b/osdf/operation/responses.py
new file mode 100644
index 0000000..22a94f7
--- /dev/null
+++ b/osdf/operation/responses.py
@@ -0,0 +1,39 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2015-2017 AT&T Intellectual Property
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# -------------------------------------------------------------------------
+#
+
+from flask import Response
+
+from osdf import ACCEPTED_MESSAGE_TEMPLATE
+
+
+def osdf_response_for_request_accept(req_id="", text="", response_code=202, as_http=True):
+ """Helper method to create a response object for request acceptance, so that the object can be sent to a client
+ :param req_id: request ID provided by the caller
+ :param text: extra text description about accepting the request (e.g. "Request accepted")
+ :param response_code: the HTTP status code to send -- default is 202 (accepted)
+ :param as_http: whether to send response as HTTP response object or as a string
+ :return: if as_http is True, return a HTTP Response object. Otherwise, return json-encoded-message
+ """
+ response_message = ACCEPTED_MESSAGE_TEMPLATE.render(description=text, request_id=req_id)
+ if not as_http:
+ return response_message
+
+ response = Response(response_message, content_type='application/json; charset=utf-8')
+ response.headers.add('content-length', len(response_message))
+ response.status_code = response_code
+ return response