Skip to content

Commit

Permalink
Merge pull request #5 from bigbank-as/feature/camunda-get-variables
Browse files Browse the repository at this point in the history
Get process/task variables
  • Loading branch information
Waldz authored Mar 13, 2017
2 parents f0caafd + bd06ec7 commit e3700ec
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 66 deletions.
5 changes: 5 additions & 0 deletions client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package go_camunda_client
type CamundaClient interface {
StartProcess(processDefinitionKey string, request interface{}) (Process, error)
GetProcess(processId string) (Process, error)
GetProcessVariable(processId string, variableName string) (VariableResponse, error)
GetNextTask(processId string) (Task, error)
GetAllTasks(processId string) ([]Task, error)
CompleteTask(taskId string, request interface{}) (error)
GetTaskVariable(taskId string, variableName string) (VariableResponse, error)
HandleErrors(errorCallback func(error))
}

Expand All @@ -20,3 +22,6 @@ type Task interface {
GetTaskDefinitionKey() string
}

type VariableResponse interface {
GetValue() string
}
111 changes: 78 additions & 33 deletions rest/client_rest.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package rest

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/bigbank-as/go_camunda_client"
"github.com/bigbank-as/go_camunda_client/rest/dto"
"io/ioutil"
"net/http"
"bytes"
)

func Construct(urlRoot string, username string, password string, httpClient http.Client) go_camunda_client.CamundaClient {
const CLIENT_NAME = "goclient-v0.1"

func Construct(urlRoot, username, password string, httpClient http.Client) go_camunda_client.CamundaClient {
client := new(camundaClientRest)
client.urlRoot = urlRoot
client.authUsername = username
Expand All @@ -21,10 +23,17 @@ func Construct(urlRoot string, username string, password string, httpClient http
return client
}

func (client *camundaClientRest) StartProcess(processDefinitionKey string, request interface{}) (go_camunda_client.Process, error) {
func (client *camundaClientRest) StartProcess(processDefinitionKey string, requestDto interface{}) (
go_camunda_client.Process,
error,
) {
var process dto.Process

response, err := client.doRequest("POST", "process-definition/key/" + processDefinitionKey + "/start", request)
response, err := client.doRequestJson(
"POST",
"process-definition/key/" + processDefinitionKey + "/start",
requestDto,
)
if err == nil {
err = client.parseResponseJson(response, &process)
defer response.Body.Close()
Expand All @@ -36,7 +45,7 @@ func (client *camundaClientRest) StartProcess(processDefinitionKey string, reque
func (client *camundaClientRest) GetProcess(processId string) (go_camunda_client.Process, error) {
var process dto.Process

response, err := client.doRequest("GET", "process-instance/" + processId, nil)
response, err := client.doRequest("GET", "process-instance/" + processId)
if err == nil {
err = client.parseResponseJson(response, &process)
defer response.Body.Close()
Expand All @@ -45,6 +54,13 @@ func (client *camundaClientRest) GetProcess(processId string) (go_camunda_client
return process, err
}

func (client *camundaClientRest) GetProcessVariable(processId, variableName string) (
go_camunda_client.VariableResponse,
error,
) {
return client.doRequestVariable("process-instance/" + processId + "/variables/" + variableName)
}

func (client camundaClientRest) GetNextTask(processId string) (go_camunda_client.Task, error) {
tasks, err := client.GetAllTasks(processId)
if len(tasks) >= 1 {
Expand All @@ -57,7 +73,7 @@ func (client camundaClientRest) GetAllTasks(processId string) ([]go_camunda_clie
var dtoTasks []dto.Task
var tasks []go_camunda_client.Task

response, err := client.doRequest("GET", "task/?processInstanceId=" + processId, nil)
response, err := client.doRequest("GET", "task/?processInstanceId=" + processId)
if err == nil {
err = client.parseResponseJson(response, &dtoTasks)
defer response.Body.Close()
Expand All @@ -69,11 +85,19 @@ func (client camundaClientRest) GetAllTasks(processId string) ([]go_camunda_clie
}
return tasks, err
}
func (client camundaClientRest) CompleteTask(taskId string, request interface{}) (error) {
_, err := client.doRequest("POST", "task/" + taskId + "/complete", request)

func (client camundaClientRest) CompleteTask(taskId string, requestDto interface{}) (error) {
_, err := client.doRequestJson("POST", "task/" + taskId + "/complete", requestDto)
return err
}

func (client *camundaClientRest) GetTaskVariable(taskId, variableName string) (
go_camunda_client.VariableResponse,
error,
) {
return client.doRequestVariable("task/" + taskId + "/variables/" + variableName)
}

func (client *camundaClientRest) HandleErrors(errorCallback func(error)) {
client.errorCallbacks = append(client.errorCallbacks, errorCallback)
}
Expand All @@ -92,21 +116,64 @@ type camundaClientRest struct {
errorCallbacks []func(error)
}

func (client *camundaClientRest) doRequest(method, path string, payload interface{}) (*http.Response, error) {
func (client *camundaClientRest) doRequest(method, path string) (
*http.Response,
error,
) {
request, err := http.NewRequest(method, client.urlRoot + "/" + path, nil)
if err != nil {
client.notifyErrorHandlers(err)
return nil, err
}


return client.transportRequest(request)
}

func (client *camundaClientRest) doRequestJson(method, path string, payload interface{}) (
*http.Response,
error,
) {
payloadJson, err := json.Marshal(payload)
if err != nil {
client.notifyErrorHandlers(err)
return nil, err
}

request, err := client.constructRequest(method, path, payload)
request, err := http.NewRequest(method, client.urlRoot + "/" + path, bytes.NewBuffer(payloadJson))
request.Header.Set("Content-Type", "application/json")
if err != nil {
client.notifyErrorHandlers(err)
return nil, err
}

return client.transportRequest(request)
}

func (client *camundaClientRest) doRequestVariable(resoucePath string) (dto.VariableResponse, error) {
var variableResponse dto.VariableResponse

response, err := client.doRequest("GET", resoucePath+ "/?deserializeValue=false")
if err == nil {
err = client.parseResponseJson(response, &variableResponse)
defer response.Body.Close()
}

return variableResponse, err
}

func (client *camundaClientRest) transportRequest(request *http.Request) (*http.Response, error) {
request.Header.Set("User-Agent", CLIENT_NAME)
request.Header.Set("Accept", "application/json")
request.SetBasicAuth(client.authUsername, client.authPassword)

response, err := client.httpClient.Do(request)
if err != nil {
client.notifyErrorHandlers(err)
return response, err
}

if (response.StatusCode < 200 || response.StatusCode >= 300) {
if response.StatusCode < 200 || response.StatusCode >= 300 {
err := client.parseResponseError(response)
client.notifyErrorHandlers(err)
return response, err
Expand Down Expand Up @@ -141,26 +208,4 @@ func (client *camundaClientRest) parseResponseJson(response *http.Response, dto
}

return nil
}

func (client *camundaClientRest) constructRequest(method, path string, payload interface{})(*http.Request, error) {
var request *http.Request;
var err error;
url := client.urlRoot + "/" + path

if payload == nil {
request, err = http.NewRequest(method, url, nil)
} else {
var payloadJson []byte;
payloadJson, err = json.Marshal(payload)
if err != nil {
client.notifyErrorHandlers(err)
return nil, err
}
request, err = http.NewRequest(method, url, bytes.NewBuffer(payloadJson))
}

request.Header.Set("Content-Type", "application/json")
request.SetBasicAuth(client.authUsername, client.authPassword)
return request, err
}
File renamed without changes.
14 changes: 0 additions & 14 deletions rest/dto/process.go

This file was deleted.

33 changes: 33 additions & 0 deletions rest/dto/process_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dto

import (
"fmt"
"encoding/json"
)

type Process struct {
id string
isEnded bool
}

func (process Process) GetId() string {
return process.id
}

func (process Process) IsEnded() bool {
return process.isEnded
}

func (response *Process) UnmarshalJSON(data []byte) error {
var responseRaw struct{
Id string `json:"id"`
IsEnded bool `json:"ended"`
}
if err := json.Unmarshal(data, &responseRaw); err != nil {
return fmt.Errorf("Failed to parse variable: %s", err)
}

response.id = responseRaw.Id
response.isEnded = responseRaw.IsEnded
return nil
}
19 changes: 0 additions & 19 deletions rest/dto/task.go

This file was deleted.

40 changes: 40 additions & 0 deletions rest/dto/task_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dto

import (
"fmt"
"encoding/json"
)

type Task struct {
id string
name string
taskDefinitionKey string
}

func (task Task) GetId() string {
return task.id
}

func (task Task) GetName() string {
return task.name
}

func (task Task) GetTaskDefinitionKey() string {
return task.taskDefinitionKey
}

func (response *Task) UnmarshalJSON(data []byte) error {
var responseRaw struct{
Id string `json:"id"`
Name string `json:"name"`
TaskDefinitionKey string `json:"taskDefinitionKey"`
}
if err := json.Unmarshal(data, &responseRaw); err != nil {
return fmt.Errorf("Failed to parse variable: %s", err)
}

response.id = responseRaw.Id
response.name = responseRaw.Name
response.taskDefinitionKey = responseRaw.TaskDefinitionKey
return nil
}
37 changes: 37 additions & 0 deletions rest/dto/variable_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dto

import (
"fmt"
"encoding/json"
"strings"
)

type VariableResponse struct {
value string
valueType string
valueFormat string
}

func (response VariableResponse) GetValue() string {
return response.value
}

func (response *VariableResponse) UnmarshalJSON(data []byte) error {
var responseRaw struct{
Type string `json:"type"`
Value string `json:"value"`
ValueInfo struct{
SerializationDataFormat string `json:"serializationDataFormat"`
} `json:"valueInfo"`
}
if err := json.Unmarshal(data, &responseRaw); err != nil {
return fmt.Errorf("Failed to parse variable: %s", err)
}

response.value = strings.Replace(responseRaw.Value, `\\\"`, `"`, -1)
response.valueType = responseRaw.Type
response.valueFormat = responseRaw.ValueInfo.SerializationDataFormat
return nil
}


0 comments on commit e3700ec

Please sign in to comment.