Skip to content

Commit 000a510

Browse files
Merge pull request #8 from camunda/create-zeebe-tutorial
draft zeebe api tutorial
2 parents 8a52b39 + 32ec073 commit 000a510

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

.env.example

+17
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ OPTIMIZE_BASE_URL=fill-this-in
3030
## This value will only change if you're not using SaaS.
3131
OPTIMIZE_AUDIENCE=optimize.camunda.io
3232

33+
------------------------------------
34+
35+
## Zeebe API tutorial
36+
37+
## Capture these values from an API client created at the cluster level, with access to the Zeebe scope.
38+
### This value comes from the `ZEEBE_CLIENT_ID`.
39+
ZEEBE_CLIENT_ID=fill-this-in
40+
### This value comes from the `ZEEBE_CLIENT_SECRET`.
41+
ZEEBE_CLIENT_SECRET=fill-this-in
42+
### This value comes from the `ZEEBE_TOKEN_AUDIENCE`.
43+
ZEEBE_AUDIENCE=zeebe.camunda.io
44+
### This value comes from the `ZEEBE_REST_ADDRESS`.
45+
ZEEBE_BASE_URL=fill-this-in
46+
47+
------------------------------------
48+
3349
# Web Modeler API tutorial
3450

3551
## These values will only change if you're not using SaaS.
@@ -41,3 +57,4 @@ MODELER_CLIENT_ID=fill-this-in
4157
MODELER_CLIENT_SECRET=fill-this-in
4258

4359

60+

cli.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import "dotenv/config";
33

44
import admin from "./administration.js";
55
import optimize from "./optimize.js";
6+
import zeebe from "./zeebe.js";
67
import modeler from "./modeler.js";
78

89
// All API objects accessible to the CLI app are included here.
910
// The name of each property translates to an API object that can be called by the CLI.
1011
// e.g. if we export a property named `admin`, you can run `npm run cli admin <action>`.
11-
const APIs = { admin, optimize, modeler };
12+
const APIs = { admin, optimize, zeebe, modeler };
1213

1314
// Parse the arguments passed into the CLI, and direct a specific action to a specific API object.
1415
// Example: `npm run cli administration list` will find the arguments `administration` and `list`,

completed/zeebe.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import axios from "axios";
2+
import { getAccessToken } from "../auth.js";
3+
4+
const authorizationConfiguration = {
5+
clientId: process.env.ZEEBE_CLIENT_ID,
6+
clientSecret: process.env.ZEEBE_CLIENT_SECRET,
7+
audience: process.env.ZEEBE_AUDIENCE
8+
};
9+
10+
// An action that assigns a Zeebe user task.
11+
async function assignUser([userTaskKey, assignee]) {
12+
// Every request needs an access token.
13+
const accessToken = await getAccessToken(authorizationConfiguration);
14+
15+
// These settings come from your .env file.
16+
const zeebeApiUrl = process.env.ZEEBE_BASE_URL;
17+
18+
// This is the API endpoint to assign a user task.
19+
const url = `${zeebeApiUrl}/user-tasks/${userTaskKey}/assignment`;
20+
21+
// Configure the API call.
22+
const options = {
23+
method: "POST",
24+
url,
25+
headers: {
26+
Accept: "application/json",
27+
Authorization: `Bearer ${accessToken}`
28+
},
29+
data: {
30+
// The body contains information about the new assignment.
31+
assignee: assignee
32+
}
33+
};
34+
35+
try {
36+
// Call the add endpoint.
37+
const response = await axios(options);
38+
39+
// Process the results from the API call.
40+
if (response.status === 204) {
41+
console.log(`User task assigned to ${assignee}.`);
42+
} else {
43+
// Emit an unexpected error message.
44+
console.error("Unable to assign this user!");
45+
}
46+
} catch (error) {
47+
// Emit an error from the server.
48+
console.error(error.message);
49+
}
50+
}
51+
52+
async function unassignUser([userTaskKey]) {
53+
const accessToken = await getAccessToken(authorizationConfiguration);
54+
55+
const zeebeApiUrl = process.env.ZEEBE_BASE_URL;
56+
57+
const url = `${zeebeApiUrl}/user-tasks/${userTaskKey}/assignee`;
58+
59+
// Configure the API call.
60+
const options = {
61+
method: "DELETE",
62+
url,
63+
headers: {
64+
Accept: "application/json",
65+
Authorization: `Bearer ${accessToken}`
66+
}
67+
};
68+
69+
try {
70+
// Call the delete endpoint.
71+
const response = await axios(options);
72+
73+
// Process the results from the API call.
74+
if (response.status === 204) {
75+
console.log("User task has been unassigned!");
76+
} else {
77+
// Emit an unexpected error message.
78+
console.error("Unable to unassign this user task!");
79+
}
80+
} catch (error) {
81+
// Emit an error from the server.
82+
console.error(error.message);
83+
}
84+
}
85+
86+
// These functions are aliased to specific command names for terseness.
87+
// The name of each property translates to a method that can be called by the CLI.
88+
// e.g. if we export a function named `assign`, you can run `npm run cli zeebe assign`.
89+
90+
export default {
91+
assign: assignUser,
92+
unassign: unassignUser
93+
};

zeebe.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import axios from "axios";
2+
import { getAccessToken } from "./auth.js";
3+
4+
async function assignUser([userTaskKey, assignee]) {
5+
console.log(`User task assigned to ${assignee}.`);
6+
}
7+
8+
async function unassignUser([userTaskKey]) {
9+
console.log("User task has been unassigned!");
10+
}
11+
12+
// These functions are aliased to specific command names for terseness.
13+
// The name of each property translates to a method that can be called by the CLI.
14+
// e.g. if we export a function named `assign`, you can run `npm run cli zeebe assign`.
15+
16+
export default {
17+
assign: assignUser,
18+
unassign: unassignUser
19+
};

0 commit comments

Comments
 (0)