Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration test for http invocation #758

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions examples/invoke-http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Example - Invoke a service

This example utilizes a receiver and a caller for the `invoke_method` functionality.

> **Note:** Make sure to use the latest proto bindings

## Pre-requisites

- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started)
- [Install Python 3.8+](https://www.python.org/downloads/)

### Install requirements

You can install dapr SDK package using pip command:

<!-- STEP
name: Install requirements
-->

```sh
pip3 install dapr Flask
```

<!-- END_STEP -->

## Run the example

To run this example, the following code can be utilized:

Start the receiver:
<!-- STEP
name: Run invoke http example
expected_stdout_lines:
- '== APP == Order received : {"id": 1, "message": "hello world"}'
- '== APP == Order error : {"id": 2, "message": "hello world"}'
background: true
sleep: 5
-->

```bash
dapr run --app-id=invoke-receiver --app-port=8088 --app-protocol http -- python3 invoke-receiver.py
```
<!-- END_STEP -->

Start the caller:
<!-- STEP
name: Run invoke http example
expected_stdout_lines:
- '== APP == text/html'
- '== APP == {"success": true}'
- '== APP == 200'
- '== APP == error occurred'
- '== APP == MY_CODE'
background: true
sleep: 5
-->

```bash
dapr run --app-id=invoke-caller -- python3 invoke-caller.py
```
<!-- END_STEP -->

## Cleanup

<!-- STEP
expected_stdout_lines:
- '✅ app stopped successfully: invoke-receiver'
name: Shutdown dapr
-->

```bash
dapr stop --app-id invoke-receiver
```

<!-- END_STEP -->
33 changes: 22 additions & 11 deletions examples/invoke-http/invoke-caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@
with DaprClient() as d:
req_data = {'id': 1, 'message': 'hello world'}

while True:
# Create a typed message with content type and body
resp = d.invoke_method(
# First message: success
# Create a typed message with content type and body
resp1 = d.invoke_method(
'invoke-receiver',
'my-method',
http_verb='POST',
data=json.dumps(req_data),
)

# Print the response
print(resp1.content_type, flush=True)
print(resp1.text(), flush=True)
print(str(resp1.status_code), flush=True)

# Second message: error
req_data = {'id': 2, 'message': 'hello world'}
try:
resp2 = d.invoke_method(
'invoke-receiver',
'my-method',
'my-method-err',
http_verb='POST',
data=json.dumps(req_data),
)

# Print the response
print(resp.content_type, flush=True)
print(resp.text(), flush=True)
print(str(resp.status_code), flush=True)

time.sleep(2)
except Exception as e:
print(e._message, flush=True)
print(e._error_code, flush=True)
9 changes: 9 additions & 0 deletions examples/invoke-http/invoke-receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ def getOrder():
return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}


@app.route('/my-method-err', methods=['POST'])
def getOrderErr():
data = request.json
print('Order error : ' + json.dumps(data), flush=True)
resp = {'message': 'error occurred', 'errorCode': 'MY_CODE'}
return json.dumps(resp), 503, {'ContentType': 'application/json'}


print('Starting Flask app on port 8088...', flush=True)
app.run(port=8088)
Loading