1
+ # Copyright (c) Microsoft Corporation. All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ import azure .functions as func
5
+ import json
6
+
7
+ app = func .FunctionApp (http_auth_level = func .AuthLevel .ANONYMOUS )
8
+
9
+
10
+ @app .generic_trigger (arg_name = "req" , type = "httpTrigger" , route = "sql_input/{productid}" )
11
+ @app .generic_output_binding (arg_name = "$return" , type = "http" )
12
+ @app .generic_input_binding (arg_name = "products" , type = "sql" ,
13
+ command_text = "SELECT * FROM Products WHERE ProductId = @ProductId" ,
14
+ command_type = "Text" ,
15
+ parameters = "@ProductId={productid}"
16
+ connection_string_setting = "AzureWebJobsSqlConnectionString" )
17
+ def sql_input (req : func .HttpRequest , products : func .SqlRowList ) -> func .HttpResponse :
18
+ rows = list (map (lambda r : json .loads (r .to_json ()), products ))
19
+
20
+ return func .HttpResponse (
21
+ json .dumps (rows ),
22
+ status_code = 200 ,
23
+ mimetype = "application/json"
24
+ )
25
+
26
+ @app .generic_trigger (arg_name = "req" , type = "httpTrigger" , route = "sql_input2/{productid}" )
27
+ @app .generic_output_binding (arg_name = "$return" , type = "http" )
28
+ @app .generic_input_binding (arg_name = "products" , type = "sql" ,
29
+ command_text = "SELECT * FROM Products2 WHERE ProductId = @ProductId" ,
30
+ command_type = "Text" ,
31
+ parameters = "@ProductId={productid}"
32
+ connection_string_setting = "AzureWebJobsSqlConnectionString" )
33
+ def sql_input (req : func .HttpRequest , products : func .SqlRowList ) -> func .HttpResponse :
34
+ rows = list (map (lambda r : json .loads (r .to_json ()), products ))
35
+
36
+ return func .HttpResponse (
37
+ json .dumps (rows ),
38
+ status_code = 200 ,
39
+ mimetype = "application/json"
40
+ )
41
+
42
+ @app .generic_trigger (arg_name = "req" , type = "httpTrigger" )
43
+ @app .generic_output_binding (arg_name = "$return" , type = "http" )
44
+ @app .generic_output_binding (arg_name = "r" , type = "sql" ,
45
+ command_text = "[dbo].[Products]" ,
46
+ connection_string_setting = "AzureWebJobsSqlConnectionString" )
47
+ def sql_output (req : func .HttpRequest , product : func .Out [func .SqlRow ]) -> func .HttpResponse :
48
+ body = json .loads (req .get_body ())
49
+ row = func .SqlRow .from_dict (body )
50
+ r .set (row )
51
+
52
+ return func .HttpResponse (
53
+ body = req .get_body (),
54
+ status_code = 201 ,
55
+ mimetype = "application/json"
56
+ )
57
+
58
+ @app .generic_trigger (arg_name = "changes" , type = "sqlTrigger" ,
59
+ table_name = "Products" ,
60
+ connection_string_setting = "AzureWebJobsSqlConnectionString" )
61
+ @app .generic_output_binding (arg_name = "r" , type = "sql" ,
62
+ command_text = "[dbo].[Products2]" ,
63
+ connection_string_setting = "AzureWebJobsSqlConnectionString" )
64
+ def sql_trigger (changes , r : func .Out [func .SqlRow ]) -> str :
65
+ row = func .SqlRow .from_dict (json .loads (changes )[0 ]["Item" ])
66
+ r .set (row )
67
+ return "OK"
0 commit comments