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 15 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
11 changes: 9 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ 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

# Required for Web Modeler API tutorial, to access API
MODELER_BASE_URL=https://modeler.cloud.camunda.io/api/v1
christinaausley marked this conversation as resolved.
Show resolved Hide resolved
MODELER_AUDIENCE=modeler.cloud.camunda.io
MODELER_CLIENT_ID=fill-this-in
MODELER_CLIENT_SECRET=fill-this-in
christinaausley marked this conversation as resolved.
Show resolved Hide resolved


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
131 changes: 131 additions & 0 deletions modeler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
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]) {
pepopowitz marked this conversation as resolved.
Show resolved Hide resolved
christinaausley marked this conversation as resolved.
Show resolved Hide resolved
// 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;

pepopowitz marked this conversation as resolved.
Show resolved Hide resolved
// This is the API endpoint to create a new project.
const url = `${modelerApiUrl}/projects`;

// Configure the API call.
const options = {
method: "POST",
url,
headers: {
Accept: "application/json",
Authorization: `Bearer ${accessToken}`
},
data: {
// The body contains information about the new project.
name: projectName
}
pepopowitz marked this conversation as resolved.
Show resolved Hide resolved
};

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

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

console.log(
`Project added! Name: ${newProject.name}. ID: ${newProject.id}.`
);
pepopowitz marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
// Emit an error from the server.
console.error(error.message);
}
}

// An action that views one 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;

pepopowitz marked this conversation as resolved.
Show resolved Hide resolved
// This is the API endpoint to view a single project.
const url = `${modelerApiUrl}/projects/${projectId}`;

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

try {
const response = await axios(options);

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

// Emit the project details.
console.log("Project:", data);
} 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;

pepopowitz marked this conversation as resolved.
Show resolved Hide resolved
// 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
};