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 7 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
77 changes: 77 additions & 0 deletions modeler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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 retrieves a file.
async function retrieveFile([fileId]) {
const accessToken = await getAccessToken(authorizationConfiguration);

const ModelerBaseUrl = process.env.MODELER_BASE_URL;
christinaausley marked this conversation as resolved.
Show resolved Hide resolved

pepopowitz marked this conversation as resolved.
Show resolved Hide resolved
const url = `${ModelerBaseUrl}/files/${fileId}`;

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

try {
const response = await axios(options);
const results = response.data;

results.forEach(x => console.log(`File retrieved! ID: ${id}. Name: ${name}`));
christinaausley marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
// Emit an error from the server.
console.error(error.message);
}
}

async function deleteFile([fileId]) {
console.log(`deleting file ${fileId}`);

const accessToken = await getAccessToken(authorizationConfiguration);

const ModelerBaseUrl = process.env.MODELER_BASE_URL;
const url = `${ModelerBaseUrl}/files/${fileId}`;

// 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(`File ${fileId} was deleted!`);
} else {
// Emit an unexpected error message.
console.error("Unable to delete file!");
}
} catch (error) {
// Emit an error from the server.
console.error(error.message);
}
}

export default {
retrieve: retrieveFile,
delete: deleteFile
};