1212# License for the specific language governing permissions and limitations
1313# under the License.
1414
15+ from fdk import headers
16+ from fdk import response
1517from fdk import errors
1618
1719
20+ class JSONDispatchException (Exception ):
21+
22+ def __init__ (self , context , status , message ):
23+ """
24+ JSON response with error
25+ :param status: HTTP status code
26+ :param message: error message
27+ """
28+ self .status = status
29+ self .message = message
30+ self .context = context
31+
32+ def response (self ):
33+ resp_headers = headers .GoLikeHeaders ({})
34+ resp_headers .set ("content-type" , "text/plain; charset=utf-8" )
35+ return response .RawResponse (
36+ self .context ,
37+ response_data = {
38+ "error" : {
39+ "message" : self .message ,
40+ }
41+ },
42+ headers = resp_headers ,
43+ status_code = self .status )
44+
45+
1846class RequestContext (object ):
1947
2048 def __init__ (self , app_name , route , call_id ,
@@ -62,28 +90,6 @@ def ExecutionType(self):
6290 return self .__exec_type
6391
6492
65- class HTTPContext (RequestContext ):
66-
67- def __init__ (self , app_name , route ,
68- call_id , fntype = "http" ,
69- deadline = None , execution_type = None ,
70- config = None , headers = None ,
71- method = None , url = None ,
72- query_parameters = None ,
73- version = None ):
74- arguments = {
75- "method" : method ,
76- "URL" : url ,
77- "query" : query_parameters ,
78- "http_version" : version
79- }
80- self .DispatchError = errors .HTTPDispatchException
81- super (HTTPContext , self ).__init__ (
82- app_name , route , call_id , fntype ,
83- execution_type = execution_type , deadline = deadline ,
84- config = config , headers = headers , arguments = arguments )
85-
86-
8793class JSONContext (RequestContext ):
8894
8995 def __init__ (self , app_name , route , call_id ,
@@ -95,10 +101,3 @@ def __init__(self, app_name, route, call_id,
95101 app_name , route , call_id , fntype ,
96102 execution_type = execution_type ,
97103 deadline = deadline , config = config , headers = headers )
98-
99-
100- def fromType (fntype , * args , ** kwargs ):
101- if fntype == "json" :
102- return JSONContext (* args , ** kwargs )
103- if fntype == "http" :
104- return HTTPContext (* args , ** kwargs )
0 commit comments