Skip to content

Commit 88f8ff7

Browse files
Merge pull request #10 from camunda/c8-api-tutorial
C8 api tutorial
2 parents 000a510 + 7372540 commit 88f8ff7

File tree

8 files changed

+228
-5
lines changed

8 files changed

+228
-5
lines changed

.env.example

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ CLUSTER_ID=fill-this-in
33

44
------------------------------------
55

6+
# Camunda 8 API tutorial
7+
8+
## Capture these values from an API client created at the cluster level, with access to the Zeebe scope.
9+
### This value comes from the `ZEEBE_CLIENT_ID`.
10+
ZEEBE_CLIENT_ID=fill-this-in
11+
### This value comes from the `ZEEBE_CLIENT_SECRET`.
12+
ZEEBE_CLIENT_SECRET=fill-this-in
13+
### This value comes from the `ZEEBE_TOKEN_AUDIENCE`.
14+
ZEEBE_AUDIENCE=zeebe.camunda.io
15+
### This value comes from the `ZEEBE_REST_ADDRESS`.
16+
ZEEBE_BASE_URL=fill-this-in
17+
18+
------------------------------------
19+
620
# Administration API tutorial
721

822
## Capture these values from an API client created at the organization level, with access to the Cluster scope.

auth.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ function configureAuthorizationClient({ clientId, clientSecret }) {
3838
},
3939
auth: {
4040
// This is the URL for our auth server.
41-
tokenHost: "https://login.cloud.camunda.io/oauth/token"
41+
tokenHost:
42+
process.env.ZEEBE_AUTHORIZATION_SERVER_URL ||
43+
"https://login.cloud.camunda.io/oauth/token"
4244
},
4345
options: {
4446
authorizationMethod: "body"

camunda-8.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import axios from "axios";
2+
import { getAccessToken } from "./auth.js";
3+
4+
// An action that lists all roles.
5+
async function listRoles() {
6+
console.log("listing roles");
7+
}
8+
9+
// An action that creates a role.
10+
async function createRole([roleName]) {
11+
console.log(`adding role ${roleName}`);
12+
}
13+
14+
// An action that retrieves a role.
15+
async function getRole([roleKey]) {
16+
console.log(`viewing role ${roleKey}`);
17+
}
18+
19+
// An action that deletes a role.
20+
async function deleteRole([roleKey]) {
21+
console.log(`deleting role ${roleKey}`);
22+
}
23+
24+
// These functions are aliased to specific command names for terseness.
25+
// The name of each property translates to a method that can be called by the CLI.
26+
// e.g. if we export a function named `list`, you can run `npm run cli zeebe get`.
27+
28+
export default {
29+
list: listRoles,
30+
create: createRole,
31+
view: getRole,
32+
delete: deleteRole
33+
};

cli.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import admin from "./administration.js";
55
import optimize from "./optimize.js";
66
import zeebe from "./zeebe.js";
77
import modeler from "./modeler.js";
8+
import camunda8 from "./camunda-8.js";
89

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

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

completed/camunda-8.js

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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 lists all roles.
11+
async function listRoles() {
12+
// Every request needs an access token.
13+
const accessToken = await getAccessToken(authorizationConfiguration);
14+
15+
// These settings come from your .env file.
16+
const camundaApiUrl = process.env.ZEEBE_BASE_URL;
17+
18+
// This is the API endpoint to query roles.
19+
const url = `${camundaApiUrl}/roles/search`;
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+
// No filtering/paging/sorting, we want all roles.
30+
data: {}
31+
};
32+
33+
try {
34+
// Call the endpoint.
35+
const response = await axios(options);
36+
37+
// Process the results from the API call.
38+
const results = response.data;
39+
40+
// Emit roles to output.
41+
results.items.forEach(x =>
42+
console.log(`Role Name: ${x.name}; key: ${x.key}`)
43+
);
44+
} catch (error) {
45+
// Emit an error from the server.
46+
console.error(error.message);
47+
}
48+
}
49+
50+
// An action that creates a role.
51+
async function createRole([roleName]) {
52+
// Every request needs an access token.
53+
const accessToken = await getAccessToken(authorizationConfiguration);
54+
55+
// These settings come from your .env file.
56+
const camundaApiUrl = process.env.ZEEBE_BASE_URL;
57+
58+
// This is the API endpoint to add a new client to a cluster.
59+
const url = `${camundaApiUrl}/roles`;
60+
61+
// Configure the API call.
62+
const options = {
63+
method: "POST",
64+
url,
65+
headers: {
66+
Accept: "application/json",
67+
Authorization: `Bearer ${accessToken}`
68+
},
69+
data: {
70+
// The body contains information about the new role.
71+
name: roleName
72+
}
73+
};
74+
75+
try {
76+
const response = await axios(options);
77+
78+
// Process the results from the API call.
79+
const newRole = response.data;
80+
81+
// Emit new role to output.
82+
console.log(`Role added! Name: ${roleName}. Key: ${newRole.roleKey}.`);
83+
} catch (error) {
84+
// Emit an error from the server.
85+
console.error(error.message);
86+
}
87+
}
88+
89+
// An action that retrieves a role.
90+
async function getRole([roleKey]) {
91+
// Every request needs an access token.
92+
const accessToken = await getAccessToken(authorizationConfiguration);
93+
94+
// These settings come from your .env file.
95+
const camundaApiUrl = process.env.ZEEBE_BASE_URL;
96+
97+
// This is the API endpoint to get a specific role.
98+
const url = `${camundaApiUrl}/roles/${roleKey}`;
99+
100+
// Configure the API call.
101+
const options = {
102+
method: "GET",
103+
url,
104+
headers: {
105+
Accept: "application/json",
106+
Authorization: `Bearer ${accessToken}`
107+
}
108+
};
109+
110+
try {
111+
// Call the endpoint.
112+
const response = await axios(options);
113+
114+
// Process the results from the API call.
115+
const results = response.data;
116+
117+
// Emit role to output.
118+
console.log(
119+
`Role Name: ${results.name}; Key: ${
120+
results.key
121+
}; Members: ${JSON.stringify(results.assignedMemberKeys)}`
122+
);
123+
} catch (error) {
124+
// Emit an error from the server.
125+
console.error(error.message);
126+
}
127+
}
128+
129+
// An action that deletes a role.
130+
async function deleteRole([roleKey]) {
131+
const accessToken = await getAccessToken(authorizationConfiguration);
132+
133+
const camundaApiUrl = process.env.ZEEBE_BASE_URL;
134+
135+
const url = `${camundaApiUrl}/roles/${roleKey}`;
136+
137+
// Configure the API call.
138+
const options = {
139+
method: "DELETE",
140+
url,
141+
headers: {
142+
Accept: "application/json",
143+
Authorization: `Bearer ${accessToken}`
144+
}
145+
};
146+
147+
try {
148+
// Call the delete endpoint.
149+
const response = await axios(options);
150+
151+
// Process the results from the API call.
152+
if (response.status === 204) {
153+
console.log("Role deleted!");
154+
} else {
155+
// Emit an unexpected error message.
156+
console.error("Unable to delete this role!");
157+
}
158+
} catch (error) {
159+
// Emit an error from the server.
160+
console.error(error.message);
161+
}
162+
}
163+
164+
// These functions are aliased to specific command names for terseness.
165+
// The name of each property translates to a method that can be called by the CLI.
166+
// e.g. if we export a function named `list`, you can run `npm run cli zeebe get`.
167+
168+
export default {
169+
list: listRoles,
170+
create: createRole,
171+
view: getRole,
172+
delete: deleteRole
173+
};

completed/modeler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from "axios";
2-
import { getAccessToken } from "../auth.js";
2+
import { getAccessToken } from "./auth.js";
33

44
const authorizationConfiguration = {
55
clientId: process.env.MODELER_CLIENT_ID,

completed/zeebe.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from "axios";
2-
import { getAccessToken } from "../auth.js";
2+
import { getAccessToken } from "./auth.js";
33

44
const authorizationConfiguration = {
55
clientId: process.env.ZEEBE_CLIENT_ID,

modeler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from "axios";
2-
import { getAccessToken } from "../auth.js";
2+
import { getAccessToken } from "./auth.js";
33

44
async function createProject([projectName, adminEmail]) {
55
console.log(

0 commit comments

Comments
 (0)