OOF design framework seed code
[optf/osdf.git] / 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(req_id="", text="", response_code=202, as_http=True):
25     """Helper method to create a response object for request acceptance, so that the object can be sent to a client
26     :param req_id: request ID provided by the caller
27     :param text: extra text description about accepting the request (e.g. "Request accepted")
28     :param response_code: the HTTP status code to send -- default is 202 (accepted)
29     :param as_http: whether to send response as HTTP response object or as a string
30     :return: if as_http is True, return a HTTP Response object. Otherwise, return json-encoded-message
31     """
32     response_message = ACCEPTED_MESSAGE_TEMPLATE.render(description=text, request_id=req_id)
33     if not as_http:
34         return response_message
35
36     response = Response(response_message, content_type='application/json; charset=utf-8')
37     response.headers.add('content-length', len(response_message))
38     response.status_code = response_code
39     return response