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 16 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.cloud.camunda.io/api/v1
christinaausley marked this conversation as resolved.
Show resolved Hide resolved
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
144 changes: 144 additions & 0 deletions modeler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
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);
}
}

// A function that adds a new collaborator to a project
async function addCollaborator([collaboratorEmail, projectId, role]) {
// 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 add a new collaborator.
const url = `${modelerApiUrl}/collaborators`;

// Configure the API call.
const options = {
method: "PUT",
url,
headers: {
Accept: "application/json",
Authorization: `Bearer ${accessToken}`
},
data: {
christinaausley marked this conversation as resolved.
Show resolved Hide resolved
// The body contains information about the new collaborator.
collaboratorEmail: collaboratorEmail,
projectId: projectId,
role: role
}
};

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

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

// Emit new collaborator to output.
console.log(
`Collaborator added! Email: ${newCollaborator.email}. Role: ${newCollaborator.role}.`
);
} catch (error) {
// Emit an error from the server.
console.error(error.message);
}
}

// A function that searches for collaborators.
async function findCollaborator([]) {
// 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 search for a collaborator.
const url = `${modelerApiUrl}/collaborators/search`;

// Configure the API call.
const options = {
method: "POST",
url,
headers: {
Accept: "application/json",
Authorization: `Bearer ${accessToken}`
},
data: {
// The body contains information about the collaborator you're searchiing for.
collaboratorEmail: collaboratorEmail
}
};

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

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

// Emit collaborator to output.
console.log(
`Collaborator found! Email: ${collaboratorEmail.email}.`
);
} 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,
add: addCollaborator,
find: findCollaborator
};