|
| 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 " |
| 14 | + "WHERE ProductId = @ProductId", |
| 15 | + command_type="Text", |
| 16 | + parameters="@ProductId={productid}", |
| 17 | + connection_string_setting="AzureWebJobsSqlConnectionString") |
| 18 | +def sql_input(req: func.HttpRequest, products: func.SqlRowList) -> func.HttpResponse: |
| 19 | + rows = list(map(lambda r: json.loads(r.to_json()), products)) |
| 20 | + |
| 21 | + return func.HttpResponse( |
| 22 | + json.dumps(rows), |
| 23 | + status_code=200, |
| 24 | + mimetype="application/json" |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +@app.generic_trigger(arg_name="req", type="httpTrigger", route="sql_input2/{productid}") |
| 29 | +@app.generic_output_binding(arg_name="$return", type="http") |
| 30 | +@app.generic_input_binding(arg_name="products", type="sql", |
| 31 | + command_text="SELECT * FROM Products2 " |
| 32 | + "WHERE ProductId = @ProductId", |
| 33 | + command_type="Text", |
| 34 | + parameters="@ProductId={productid}", |
| 35 | + connection_string_setting="AzureWebJobsSqlConnectionString") |
| 36 | +def sql_input2(req: func.HttpRequest, products: func.SqlRowList) -> func.HttpResponse: |
| 37 | + rows = list(map(lambda r: json.loads(r.to_json()), products)) |
| 38 | + |
| 39 | + return func.HttpResponse( |
| 40 | + json.dumps(rows), |
| 41 | + status_code=200, |
| 42 | + mimetype="application/json" |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +@app.generic_trigger(arg_name="req", type="httpTrigger", route="sql_output") |
| 47 | +@app.generic_output_binding(arg_name="$return", type="http") |
| 48 | +@app.generic_output_binding(arg_name="r", type="sql", |
| 49 | + command_text="[dbo].[Products]", |
| 50 | + connection_string_setting="AzureWebJobs" |
| 51 | + "SqlConnectionString") |
| 52 | +def sql_output(req: func.HttpRequest, r: func.Out[func.SqlRow]) \ |
| 53 | + -> func.HttpResponse: |
| 54 | + body = json.loads(req.get_body()) |
| 55 | + row = func.SqlRow.from_dict(body) |
| 56 | + r.set(row) |
| 57 | + |
| 58 | + return func.HttpResponse( |
| 59 | + body=req.get_body(), |
| 60 | + status_code=201, |
| 61 | + mimetype="application/json" |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +@app.generic_trigger(arg_name="changes", type="sqlTrigger", |
| 66 | + table_name="Products", |
| 67 | + connection_string_setting="AzureWebJobsSqlConnectionString") |
| 68 | +@app.generic_output_binding(arg_name="r", type="sql", |
| 69 | + command_text="[dbo].[Products2]", |
| 70 | + connection_string_setting="AzureWebJobsSqlConnectionString") |
| 71 | +def sql_trigger(changes, r: func.Out[func.SqlRow]) -> str: |
| 72 | + row = func.SqlRow.from_dict(json.loads(changes)[0]["Item"]) |
| 73 | + r.set(row) |
| 74 | + return "OK" |
0 commit comments