Update the SO sync and async responses.
[optf/osdf.git] / osdf / operation / responses.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2015-2017 AT&T Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 from flask import Response
20
21 from osdf import ACCEPTED_MESSAGE_TEMPLATE
22
23
24 def osdf_response_for_request_accept(request_id="", transaction_id="", request_status="", status_message="",
25                                      response_code=202, as_http=True):
26     """Helper method to create a response object for request acceptance, so that the object can be sent to a client
27     :param request_id: request ID provided by the caller
28     :param transaction_id: transaction ID provided by the caller
29     :param request_status: the status of a request
30     :param status_message: details on the status of a request
31     :param response_code: the HTTP status code to send -- default is 202 (accepted)
32     :param as_http: whether to send response as HTTP response object or as a string
33     :return: if as_http is True, return a HTTP Response object. Otherwise, return json-encoded-message
34     """
35     response_message = ACCEPTED_MESSAGE_TEMPLATE.render(request_id=request_id, transaction_id=transaction_id,
36                                                         request_status=request_status, status_message=status_message)
37     if not as_http:
38         return response_message
39
40     response = Response(response_message, content_type='application/json; charset=utf-8')
41     response.headers.add('content-length', len(response_message))
42     response.status_code = response_code
43     return response