Skip to content

Commit

Permalink
Merge pull request #190 from camunda/rest-client-fix
Browse files Browse the repository at this point in the history
fix(zeebe): add headers to all REST method calls
  • Loading branch information
jwulf authored Jun 18, 2024
2 parents ea6919e + 0386845 commit a81002e
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 27 deletions.
10 changes: 5 additions & 5 deletions src/__tests__/lib/GetCustomCertificateBuffer.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ test('Can use a custom root certificate to connect to a REST API', async () => {
CAMUNDA_OPERATE_BASE_URL: 'https://localhost:3012',
},
})
console.log('Trying to get process instance with certificate')
// console.log('Trying to get process instance with certificate')
const res = await c.getProcessInstance('1')
console.log(
`Got response from self-signed secured server: ${res.bpmnProcessId}`
)
// console.log(
// `Got response from self-signed secured server: ${res.bpmnProcessId}`
// )
expect(res.bpmnProcessId).toBe('test')
const c1 = new OperateApiClient({
config: {
Expand All @@ -68,7 +68,7 @@ test('Can use a custom root certificate to connect to a REST API', async () => {

let threw = false
try {
console.log('Trying to get process instance without certificate')
// console.log('Trying to get process instance without certificate')
await c1.getProcessInstance('1')
} catch (e) {
threw = true
Expand Down
6 changes: 1 addition & 5 deletions src/__tests__/tasklist/tasklist.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ describe('TasklistApiClient', () => {
const tasklist = new TasklistApiClient()
expect(p).toBeTruthy()
const operate = new OperateApiClient()
const res = await operate
.getProcessInstance(p!.processInstanceKey)
.catch((e) => {
console.log('Error getting process instance', e)
})
const res = await operate.getProcessInstance(p!.processInstanceKey)
expect(res).toBeTruthy()
const tasks = await tasklist.searchTasks({ state: 'CREATED' })
const taskid = tasks[0].id
Expand Down
48 changes: 48 additions & 0 deletions src/__tests__/testdata/zeebe-user-task.bpmn
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:zeebe="http://camunda.org/schema/zeebe/1.0" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1is74t8" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.23.0" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.5.0">
<bpmn:process id="zeebe-user-task-test" name="Zeebe User Task test" isExecutable="true">
<bpmn:startEvent id="StartEvent_1" name="Start Zeebe User Task test">
<bpmn:outgoing>Flow_0k05u9v</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_0k05u9v" sourceRef="StartEvent_1" targetRef="Activity_1d6heog" />
<bpmn:endEvent id="Event_0f0ejpu" name="Zeebe User Task test completed">
<bpmn:incoming>Flow_1sm7z6x</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_1sm7z6x" sourceRef="Activity_1d6heog" targetRef="Event_0f0ejpu" />
<bpmn:userTask id="Activity_1d6heog" name="zeebe-user-task">
<bpmn:extensionElements>
<zeebe:userTask />
</bpmn:extensionElements>
<bpmn:incoming>Flow_0k05u9v</bpmn:incoming>
<bpmn:outgoing>Flow_1sm7z6x</bpmn:outgoing>
</bpmn:userTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="zeebe-user-task-test">
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="99" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="155" y="142" width="84" height="27" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_0f0ejpu_di" bpmnElement="Event_0f0ejpu">
<dc:Bounds x="432" y="99" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="409" y="142" width="83" height="27" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_118tltf_di" bpmnElement="Activity_1d6heog">
<dc:Bounds x="270" y="77" width="100" height="80" />
<bpmndi:BPMNLabel />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="Flow_0k05u9v_di" bpmnElement="Flow_0k05u9v">
<di:waypoint x="215" y="117" />
<di:waypoint x="270" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_1sm7z6x_di" bpmnElement="Flow_1sm7z6x">
<di:waypoint x="370" y="117" />
<di:waypoint x="432" y="117" />
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
33 changes: 33 additions & 0 deletions src/__tests__/zeebe/integration-rest/UserTask-rest.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { TasklistApiClient } from '../../../tasklist'
import { ZeebeGrpcClient, ZeebeRestClient } from '../../../zeebe'

jest.setTimeout(30000)
test('can update a task', async () => {
const grpc = new ZeebeGrpcClient()

await grpc.deployResource({
processFilename: './src/__tests__/testdata/zeebe-user-task.bpmn',
})
await grpc.createProcessInstance({
bpmnProcessId: 'zeebe-user-task-test',
variables: {},
})
const tasklist = new TasklistApiClient()
await new Promise((resolve) => setTimeout(resolve, 10000))
const tasks = await tasklist.searchTasks({
state: 'CREATED',
})
const zbc = new ZeebeRestClient()
const res = await zbc.completeUserTask({
userTaskKey: tasks[0].id,
})
expect(res.statusCode).toBe(204)
const res2 = await zbc
.completeUserTask({
userTaskKey: '2251799814261421',
})
.catch((e) => {
return e
})
expect(res2.statusCode).toBe(404)
})
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ xtest('Does not call the onConnectionError handler if there is a business error'
const zbc2 = new ZeebeGrpcClient()
zbc2.on('connectionError', () => {
// tslint:disable-next-line: no-console
console.log('OnConnectionError!!!! Incrementing calledF') // @DEBUG
const e = new Error()
// console.log('OnConnectionError!!!! Incrementing calledF') // @DEBUG
// const e = new Error()
// tslint:disable-next-line: no-console
console.log(e.stack) // @DEBUG
// console.log(e.stack) // @DEBUG
calledF++
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('Will not throw an error if tenantId is provided when starting a process in
await client.cancelProcessInstance(p.processInstanceKey)
} catch (e) {
threwError = true
console.log(e)
// console.log(e)
}

expect(threwError).toBe(false)
Expand Down
37 changes: 24 additions & 13 deletions src/zeebe/zb/ZeebeRESTClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ const ZEEBE_REST_API_VERSION = 'v1'
*/
interface TaskChangeSet {
/* The due date of the task. Reset by providing an empty String. */
dueDate: Date | string
dueDate?: Date | string
/* The follow-up date of the task. Reset by providing an empty String. */
followUpDate: Date | string
followUpDate?: Date | string
/* The list of candidate users of the task. Reset by providing an empty list. */
candidateUsers: string[]
candidateUsers?: string[]
/* The list of candidate groups of the task. Reset by providing an empty list. */
candidateGroups: string[]
candidateGroups?: string[]
}

export class ZeebeRestClient {
Expand Down Expand Up @@ -110,28 +110,31 @@ export class ZeebeRestClient {
) as Promise<TopologyResponse>
}

/* Completes a user task with the given key. */
public completeUserTask({
/* Completes a user task with the given key. The method either completes the task or throws 400, 404, or 409.
Documentation: https://docs.camunda.io/docs/apis-tools/zeebe-api-rest/specifications/complete-a-user-task/ */
public async completeUserTask({
userTaskKey,
variables,
variables = {},
action = 'complete',
}: {
userTaskKey: string
variables: Record<string, unknown>
action: string
variables?: Record<string, unknown>
action?: string
}) {
const headers = await this.getHeaders()
return this.rest.then((rest) =>
rest.post(`user-tasks/${userTaskKey}/completion`, {
body: JSON.stringify({
variables,
action,
}),
headers,
})
)
}

/* Assigns a user task with the given key to the given assignee. */
public assignTask({
public async assignTask({
userTaskKey,
assignee,
allowOverride = true,
Expand All @@ -142,35 +145,43 @@ export class ZeebeRestClient {
allowOverride?: boolean
action: string
}) {
const headers = await this.getHeaders()

return this.rest.then((rest) =>
rest.post(`user-tasks/${userTaskKey}/assignment`, {
body: JSON.stringify({
allowOverride,
action,
assignee,
}),
headers,
})
)
}

/** Update a user task with the given key. */
public updateTask({
public async updateTask({
userTaskKey,
changeset,
}: {
userTaskKey: string
changeset: TaskChangeSet
}) {
const headers = await this.getHeaders()

return this.rest.then((rest) =>
rest.post(`user-tasks/${userTaskKey}/update`, {
body: JSON.stringify(changeset),
headers,
})
)
}
/* Removes the assignee of a task with the given key. */
public removeAssignee({ userTaskKey }: { userTaskKey: string }) {
public async removeAssignee({ userTaskKey }: { userTaskKey: string }) {
const headers = await this.getHeaders()

return this.rest.then((rest) =>
rest.delete(`user-tasks/${userTaskKey}/assignee`)
rest.delete(`user-tasks/${userTaskKey}/assignee`, { headers })
)
}
}

0 comments on commit a81002e

Please sign in to comment.