Commit ef73085 1 parent c9877be commit ef73085 Copy full SHA for ef73085
File tree 3 files changed +106
-11
lines changed
3 files changed +106
-11
lines changed Original file line number Diff line number Diff line change
1
+ # Example - Invoke a service
2
+
3
+ This example utilizes a receiver and a caller for the ` invoke_method ` functionality.
4
+
5
+ > ** Note:** Make sure to use the latest proto bindings
6
+
7
+ ## Pre-requisites
8
+
9
+ - [ Dapr CLI and initialized environment] ( https://docs.dapr.io/getting-started )
10
+ - [ Install Python 3.8+] ( https://www.python.org/downloads/ )
11
+
12
+ ### Install requirements
13
+
14
+ You can install dapr SDK package using pip command:
15
+
16
+ <!-- STEP
17
+ name: Install requirements
18
+ -->
19
+
20
+ ``` sh
21
+ pip3 install dapr Flask
22
+ ```
23
+
24
+ <!-- END_STEP -->
25
+
26
+ ## Run the example
27
+
28
+ To run this example, the following code can be utilized:
29
+
30
+ Start the receiver:
31
+ <!-- STEP
32
+ name: Run invoke http example
33
+ expected_stdout_lines:
34
+ - '== APP == Order received : {"id": 1, "message": "hello world"}'
35
+ - '== APP == Order error : {"id": 2, "message": "hello world"}'
36
+ background: true
37
+ sleep: 5
38
+ -->
39
+
40
+ ``` bash
41
+ dapr run --app-id=invoke-receiver --app-port=8088 --app-protocol http -- python3 invoke-receiver.py
42
+ ```
43
+ <!-- END_STEP -->
44
+
45
+ Start the caller:
46
+ <!-- STEP
47
+ name: Run invoke http example
48
+ expected_stdout_lines:
49
+ - '== APP == text/html'
50
+ - '== APP == {"success": true}'
51
+ - '== APP == 200'
52
+ - '== APP == error occurred'
53
+ - '== APP == MY_CODE'
54
+ background: true
55
+ sleep: 5
56
+ -->
57
+
58
+ ``` bash
59
+ dapr run --app-id=invoke-caller -- python3 invoke-caller.py
60
+ ```
61
+ <!-- END_STEP -->
62
+
63
+ ## Cleanup
64
+
65
+ <!-- STEP
66
+ expected_stdout_lines:
67
+ - '✅ app stopped successfully: invoke-receiver'
68
+ name: Shutdown dapr
69
+ -->
70
+
71
+ ``` bash
72
+ dapr stop --app-id invoke-receiver
73
+ ```
74
+
75
+ <!-- END_STEP -->
Original file line number Diff line number Diff line change 6
6
with DaprClient () as d :
7
7
req_data = {'id' : 1 , 'message' : 'hello world' }
8
8
9
- while True :
10
- # Create a typed message with content type and body
11
- resp = d .invoke_method (
9
+ # First message: success
10
+ # Create a typed message with content type and body
11
+ resp1 = d .invoke_method (
12
+ 'invoke-receiver' ,
13
+ 'my-method' ,
14
+ http_verb = 'POST' ,
15
+ data = json .dumps (req_data ),
16
+ )
17
+
18
+ # Print the response
19
+ print (resp1 .content_type , flush = True )
20
+ print (resp1 .text (), flush = True )
21
+ print (str (resp1 .status_code ), flush = True )
22
+
23
+ # Second message: error
24
+ req_data = {'id' : 2 , 'message' : 'hello world' }
25
+ try :
26
+ resp2 = d .invoke_method (
12
27
'invoke-receiver' ,
13
- 'my-method' ,
28
+ 'my-method-err ' ,
14
29
http_verb = 'POST' ,
15
30
data = json .dumps (req_data ),
16
31
)
17
-
18
- # Print the response
19
- print (resp .content_type , flush = True )
20
- print (resp .text (), flush = True )
21
- print (str (resp .status_code ), flush = True )
22
-
23
- time .sleep (2 )
32
+ except Exception as e :
33
+ print (e ._message , flush = True )
34
+ print (e ._error_code , flush = True )
Original file line number Diff line number Diff line change @@ -12,4 +12,13 @@ def getOrder():
12
12
return json .dumps ({'success' : True }), 200 , {'ContentType' : 'application/json' }
13
13
14
14
15
+ @app .route ('/my-method-err' , methods = ['POST' ])
16
+ def getOrderErr ():
17
+ data = request .json
18
+ print ('Order error : ' + json .dumps (data ), flush = True )
19
+ resp = {'message' : 'error occurred' , 'errorCode' : 'MY_CODE' }
20
+ return json .dumps (resp ), 503 , {'ContentType' : 'application/json' }
21
+
22
+
23
+ print ('Starting Flask app on port 8088...' , flush = True )
15
24
app .run (port = 8088 )
You can’t perform that action at this time.
0 commit comments