Fixes in howto-bindings: (#2416)

- JS and curl samples were sending data as objects but sample apps expected ints
- Improved JS sample to correctly handle async code and make it more readable
- Fixed indentation in .NET code
- JavaScript has uppercased S

Signed-off-by: Alessandro Segala (ItalyPaleAle) <43508+ItalyPaleAle@users.noreply.github.com>

Co-authored-by: Mark Fussell <markfussell@gmail.com>
This commit is contained in:
Alessandro (Ale) Segala 2022-05-06 00:50:18 +00:00 committed by GitHub
parent 1695a3e985
commit a221cfdcf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 22 deletions

View File

@ -95,7 +95,7 @@ spec:
Below are code examples that leverage Dapr SDKs to interact with an output binding. Below are code examples that leverage Dapr SDKs to interact with an output binding.
{{< tabs Dotnet Java Python Go Javascript>}} {{< tabs Dotnet Java Python Go JavaScript>}}
{{% codetab %}} {{% codetab %}}
@ -119,7 +119,8 @@ namespace EventService
{ {
string BINDING_NAME = "checkout"; string BINDING_NAME = "checkout";
string BINDING_OPERATION = "create"; string BINDING_OPERATION = "create";
while(true) { while(true)
{
System.Threading.Thread.Sleep(5000); System.Threading.Thread.Sleep(5000);
Random random = new Random(); Random random = new Random();
int orderId = random.Next(1,1000); int orderId = random.Next(1,1000);
@ -268,38 +269,36 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
```javascript ```javascript
//dependencies //dependencies
import { DaprClient, CommunicationProtocolEnum } from "dapr-client";
import { DaprServer, DaprClient, CommunicationProtocolEnum } from 'dapr-client';
//code //code
const daprHost = "127.0.0.1"; const daprHost = "127.0.0.1";
var main = function() { (async function () {
for(var i=0;i<10;i++) { for (var i = 0; i < 10; i++) {
sleep(5000); await sleep(2000);
var orderId = Math.floor(Math.random() * (1000 - 1) + 1); const orderId = Math.floor(Math.random() * (1000 - 1) + 1);
start(orderId).catch((e) => { try {
await sendOrder(orderId)
} catch (err) {
console.error(e); console.error(e);
process.exit(1); process.exit(1);
});
} }
} }
})();
async function start(orderId) { async function sendOrder(orderId) {
const BINDING_NAME = "checkout"; const BINDING_NAME = "checkout";
const BINDING_OPERATION = "create"; const BINDING_OPERATION = "create";
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
//Using Dapr SDK to invoke output binding //Using Dapr SDK to invoke output binding
const result = await client.binding.send(BINDING_NAME, BINDING_OPERATION, { orderId: orderId }); const result = await client.binding.send(BINDING_NAME, BINDING_OPERATION, orderId);
console.log("Sending message: " + orderId); console.log("Sending message: " + orderId);
} }
function sleep(ms) { function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
main();
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -317,7 +316,7 @@ All that's left now is to invoke the output bindings endpoint on a running Dapr
You can also invoke the output bindings endpoint using HTTP: You can also invoke the output bindings endpoint using HTTP:
```bash ```bash
curl -X POST -H 'Content-Type: application/json' http://localhost:3601/v1.0/bindings/checkout -d '{ "data": { "orderId": "100" }, "operation": "create" }' curl -X POST -H 'Content-Type: application/json' http://localhost:3601/v1.0/bindings/checkout -d '{ "data": 100, "operation": "create" }'
``` ```
As seen above, you invoked the `/binding` endpoint with the name of the binding to invoke, in our case its `checkout`. As seen above, you invoked the `/binding` endpoint with the name of the binding to invoke, in our case its `checkout`.

View File

@ -106,7 +106,7 @@ Now configure your application to receive incoming events. If using HTTP, you ne
Below are code examples that leverage Dapr SDKs to demonstrate an output binding. Below are code examples that leverage Dapr SDKs to demonstrate an output binding.
{{< tabs Dotnet Java Python Go Javascript>}} {{< tabs Dotnet Java Python Go JavaScript>}}
{{% codetab %}} {{% codetab %}}