1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
3
import json
4
+ import typing
4
5
5
6
import azure .functions as func
6
- import azurefunctions .extensions .bindings .eventhub as eh
7
+ # import azurefunctions.extensions.bindings.eventhub as eh
7
8
8
9
app = func .FunctionApp (http_auth_level = func .AuthLevel .ANONYMOUS )
9
10
10
11
11
- # @app.function_name(name="put_bc_trigger")
12
- # @app.blob_output(arg_name="file",
13
- # path="python-worker-tests/test-blobclient-trigger.txt",
14
- # connection="AzureWebJobsStorage")
15
- # @app.route(route="put_bc_trigger")
16
- # def put_bc_trigger(req: func.HttpRequest, file: func.Out[str]) -> str:
17
- # file.set(req.get_body())
18
- # return 'OK'
19
-
20
- # An HttpTrigger to generating EventHub event from EventHub Output Binding
21
- @app .function_name (name = "eventhub_output" )
12
+ @app .function_name (name = "put_eh_trigger" )
22
13
@app .event_hub_output (arg_name = "event" ,
23
14
event_hub_name = "python-worker-ci-eventhub-one" ,
24
15
connection = "AzureWebJobsEventHubConnectionString" )
25
- @app .route (route = "eventhub_output " )
26
- def eventhub_output (req : func .HttpRequest , event : func .Out [str ]) -> str :
27
- event .set ('debug' )
16
+ @app .route (route = "put_eh_trigger " )
17
+ def put_eh_trigger (req : func .HttpRequest , event : func .Out [str ]) -> str :
18
+ event .set (req . get_body () )
28
19
return 'OK'
29
20
30
- # This is an actual EventHub trigger which will convert the event data
31
- # into a storage blob.
32
- @app .function_name (name = "eventhub_trigger" )
33
- @app .event_hub_message_trigger (arg_name = "event" ,
34
- event_hub_name = "python-worker-ci-eventhub-one" ,
35
- connection = "AzureWebJobsEventHubConnectionString" )
21
+ @app .function_name (name = "eh_ed_trigger" )
22
+ @app .event_hub_message_trigger (
23
+ arg_name = "event" ,
24
+ event_hub_name = "python-worker-ci-eventhub-one-metadata" ,
25
+ connection = "AzureWebJobsEventHubConnectionString" )
36
26
@app .blob_output (arg_name = "$return" ,
37
- path = "python-worker-tests/test-eventhub -triggered.txt" ,
27
+ path = "python-worker-tests/test-metadata -triggered.txt" ,
38
28
connection = "AzureWebJobsStorage" )
39
- def eventhub_trigger (event : eh .EventData ) -> bytes :
40
- return bytes (event .body_as_str ())
29
+ async def eh_ed_trigger (event : func .EventHubEvent ) -> bytes :
30
+ event_dict : typing .Mapping [str , typing .Any ] = {
31
+ 'body' : event .get_body ().decode ('utf-8' ),
32
+ # Uncomment this when the EnqueuedTimeUtc is fixed in azure-functions
33
+ # 'enqueued_time': event.enqueued_time.isoformat(),
34
+ 'partition_key' : event .partition_key ,
35
+ 'sequence_number' : event .sequence_number ,
36
+ 'offset' : event .offset ,
37
+ 'metadata' : event .metadata
38
+ }
39
+
40
+ return json .dumps (event_dict )
41
41
42
42
# Retrieve the event data from storage blob and return it as Http response
43
- @app .function_name (name = "get_eventhub_triggered " )
44
- @app .route (route = "get_eventhub_triggered " )
43
+ @app .function_name (name = "get_eh_ed_triggered " )
44
+ @app .route (route = "get_eh_ed_triggered " )
45
45
@app .blob_input (arg_name = "file" ,
46
- path = "python-worker-tests/test-eventhub -triggered.txt" ,
46
+ path = "python-worker-tests/test-metadata -triggered.txt" ,
47
47
connection = "AzureWebJobsStorage" )
48
- def get_eventhub_triggered (req : func .HttpRequest ,
49
- file : func .InputStream ) -> str :
50
- return file .read ().decode ('utf-8' )
51
-
52
- # @app.event_hub_message_trigger(
53
- # arg_name="event",
54
- # event_hub_name="python-worker-ci-eventhub-one",
55
- # connection="AzureWebJobsEventHubConnectionString"
56
- # )
57
- # def eventhub_trigger(event: eh.EventHubData) -> str:
58
- # return event.body_as_str()
48
+ async def get_eh_ed_triggered (req : func .HttpRequest ,
49
+ file : func .InputStream ) -> str :
50
+ return func .HttpResponse (body = file .read ().decode ('utf-8' ),
51
+ status_code = 200 ,
52
+ mimetype = 'application/json' )
0 commit comments