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

Create modeler api tutorial #5

Merged
merged 21 commits into from
Aug 9, 2024
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
15 changes: 13 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ OPTIMIZE_CLIENT_ID=fill-this-in
OPTIMIZE_CLIENT_SECRET=fill-this-in
### This value comes from the `CAMUNDA_OPTIMIZE_BASE_URL`.
OPTIMIZE_BASE_URL=fill-this-in

## This value will only change if you're not using SaaS.
OPTIMIZE_AUDIENCE=optimize.camunda.io
OPTIMIZE_AUDIENCE=optimize.camunda.io

# Web Modeler API tutorial

## These values will only change if you're not using SaaS.
MODELER_BASE_URL=https://modeler.camunda.io/api/v1
MODELER_AUDIENCE=modeler.cloud.camunda.io
### This value comes from the `CAMUNDA_CONSOLE_CLIENT_ID`.
MODELER_CLIENT_ID=fill-this-in
### This value comes from the `CAMUNDA_CONSOLE_CLIENT_SECRET`.
MODELER_CLIENT_SECRET=fill-this-in


3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import "dotenv/config";

import admin from "./administration.js";
import optimize from "./optimize.js";
import modeler from "./modeler.js";

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

// Parse the arguments passed into the CLI, and direct a specific action to a specific API object.
// Example: `npm run cli administration list` will find the arguments `administration` and `list`,
Expand Down
164 changes: 164 additions & 0 deletions completed/modeler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import axios from "axios";
import { getAccessToken } from "../auth.js";

const authorizationConfiguration = {
clientId: process.env.MODELER_CLIENT_ID,
clientSecret: process.env.MODELER_CLIENT_SECRET,
audience: process.env.MODELER_AUDIENCE
};

// An action that creates a new project.
async function createProject([projectName, adminEmail]) {
// Every request needs an access token.
const accessToken = await getAccessToken(authorizationConfiguration);

// These settings come from your .env file.
const modelerApiUrl = process.env.MODELER_BASE_URL;

// Step 1: Create a new project.

// This is the API endpoint to create a new project.
const projectUrl = `${modelerApiUrl}/projects`;

// Configure the API call.
const projectOptions = {
method: "POST",
url: projectUrl,
headers: {
Accept: "application/json",
Authorization: `Bearer ${accessToken}`
},
data: {
// The body contains information about the new project.
name: projectName
}
};

try {
// Call the add project endpoint.
const response = await axios(projectOptions);

// Capture data from the new project.
const newProject = response.data;

console.log(
`Project added! Name: ${newProject.name}. ID: ${newProject.id}.`
);

// Step 2: Add a collaborator to the project.

// This is the API endpoint to add a collaborator to a project.
const collaboratorUrl = `${modelerApiUrl}/collaborators`;

// Configure the API call.
const collaboratorOptions = {
method: "PUT",
url: collaboratorUrl,
headers: {
Accept: "application/json",
Authorization: `Bearer ${accessToken}`
},
data: {
// The body contains information about the project and collaborator.
email: adminEmail,
projectId: newProject.id,
role: "project_admin"
}
};

// Call the add collaborator endpoint.
const collaboratorResponse = await axios(collaboratorOptions);

if (collaboratorResponse.status === 204) {
console.log(`Collaborator added! Email: ${adminEmail}.`);
} else {
console.error("Unable to add collaborator!");
}
} catch (error) {
// Emit an error from the server.
console.error(error.message);
}
}

// An action to view details of a project.
async function viewProject([projectId]) {
// Every request needs an access token.
const accessToken = await getAccessToken(authorizationConfiguration);

// These settings come from your .env file.
const modelerApiUrl = process.env.MODELER_BASE_URL;

// This is the API endpoint to retrieve details of a project.
const url = `${modelerApiUrl}/projects/${projectId}`;

// Configure the API call.
const options = {
method: "GET",
url,
headers: {
Accept: "application/json",
Authorization: `Bearer ${accessToken}`
}
};

try {
// Call the endpoint.
const response = await axios(options);

// Process the results from the API call.
const project = response.data;

// Emit the project to output.
console.log("Project:", project);
} catch (error) {
// Emit an error from the server.
console.error(error.message);
}
}

// An action that deletes a project.
async function deleteProject([projectId]) {
// Every request needs an access token.
const accessToken = await getAccessToken(authorizationConfiguration);

// These settings come from your .env file.
const modelerApiUrl = process.env.MODELER_BASE_URL;

// This is the API endpoint to delete a project.
const url = `${modelerApiUrl}/projects/${projectId}`;

// Configure the API call.
const options = {
method: "DELETE",
url,
headers: {
Accept: "application/json",
Authorization: `Bearer ${accessToken}`
}
};

try {
// Call the delete endpoint.
const response = await axios(options);

// Process the results from the API call.
if (response.status === 204) {
console.log(`Project ${projectId} was deleted!`);
} else {
// Emit an unexpected error message.
console.error("Unable to delete project!");
}
} catch (error) {
// Emit an error from the server.
console.error(error.message);
}
}

// These functions are aliased to specific command names for terseness.
// The name of each property translates to a method that can be called by the CLI.
// e.g. if we export a function named `create`, you can run `npm run cli modeler create`.
export default {
create: createProject,
view: viewProject,
delete: deleteProject
};
25 changes: 25 additions & 0 deletions modeler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import axios from "axios";
import { getAccessToken } from "../auth.js";

async function createProject([projectName, adminEmail]) {
console.log(
`Project added! Name: ${newProject.name}. ID: ${newProject.id}.`
);
pepopowitz marked this conversation as resolved.
Show resolved Hide resolved
};

async function viewProject([projectId]) {
console.log("Project:", project);
}

async function deleteProject([projectId]) {
console.log(`Project ${projectId} was deleted!`);
}

// These functions are aliased to specific command names for terseness.
// The name of each property translates to a method that can be called by the CLI.
// e.g. if we export a function named `create`, you can run `npm run cli modeler create`.
export default {
create: createProject,
view: viewProject,
delete: deleteProject
};