Skip to content

Commit

Permalink
Add create/update method
Browse files Browse the repository at this point in the history
  • Loading branch information
NV4RE committed Jul 14, 2021
1 parent a024038 commit 67788bb
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
92 changes: 92 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func (c *Client) GetTaskDefinitions() ([]*TaskDefinition, goerror.Error) {
return r.Data, nil
}

func (c *Client) CreateTaskDefinition(t TaskDefinition) goerror.Error {
return c.SetTaskDefinition(t)
}

func (c *Client) SetTaskDefinition(t TaskDefinition) goerror.Error {
b, err := json.Marshal(t)
if err != nil {
Expand Down Expand Up @@ -148,6 +152,52 @@ func (c *Client) SetTaskDefinition(t TaskDefinition) goerror.Error {
return nil
}

func (c *Client) UpdateTaskDefinition(t TaskDefinition) goerror.Error {
b, err := json.Marshal(t)
if err != nil {
return ErrParseRequestBodyFailed.WithCause(err)
}

u, err := url.Parse(fmt.Sprintf("%s/v1/definition/task", c.processManagerEndpoint))
if err != nil {
return ErrParseRequestBodyFailed.WithCause(err)
}
h := http.Header{}
h.Set("Content-type", "application/json")

resp, err := c.httpClient.Do(&http.Request{
Method: http.MethodPut,
URL: u,
Body: ioutil.NopCloser(bytes.NewReader(b)),
Header: h,
})
if err != nil {
return ErrRequestFailed.WithCause(err)
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ErrReadResponseBodyFailed.WithCause(err)
}

var r melonadeResponse
err = json.Unmarshal(body, &r)
if err != nil {
return ErrParseResponseBodyFailed.WithCause(err)
}

if r.Success != true {
return ErrRequestNotSuccess.WithExtendMsg(fmt.Sprintf("%v", r.Error))
}

return nil
}

func (c *Client) CreateWorkflowDefinition(w WorkflowDefinition) goerror.Error {
return c.SetWorkflowDefinition(w)
}

func (c *Client) SetWorkflowDefinition(t WorkflowDefinition) goerror.Error {
b, err := json.Marshal(t)
if err != nil {
Expand Down Expand Up @@ -178,6 +228,48 @@ func (c *Client) SetWorkflowDefinition(t WorkflowDefinition) goerror.Error {
return nil
}

func (c *Client) UpdateWorkflowDefinition(w WorkflowDefinition) goerror.Error {
b, err := json.Marshal(w)
if err != nil {
return ErrParseRequestBodyFailed.WithCause(err)
}

u, err := url.Parse(fmt.Sprintf("%s/v1/definition/workflow", c.processManagerEndpoint))
if err != nil {
return ErrParseRequestBodyFailed.WithCause(err)
}
h := http.Header{}
h.Set("Content-type", "application/json")

resp, err := c.httpClient.Do(&http.Request{
Method: http.MethodPut,
URL: u,
Body: ioutil.NopCloser(bytes.NewReader(b)),
Header: h,
})
if err != nil {
return ErrRequestFailed.WithCause(err)
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ErrReadResponseBodyFailed.WithCause(err)
}

var r melonadeResponse
err = json.Unmarshal(body, &r)
if err != nil {
return ErrParseResponseBodyFailed.WithCause(err)
}

if r.Success != true {
return ErrRequestNotSuccess.WithExtendMsg(fmt.Sprintf("%v", r.Error))
}

return nil
}

func errWithInput(err goerror.Error, workflowName, revision, transactionId string, payload interface{}) goerror.Error {
return err.WithKeyValueInput(
"workflowName", workflowName,
Expand Down
4 changes: 4 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ type Service interface {
GetWorkflowDefinitions() ([]*WorkflowDefinition, goerror.Error)
GetTaskDefinitions() ([]*TaskDefinition, goerror.Error)
SetTaskDefinition(t TaskDefinition) goerror.Error
CreateTaskDefinition(t TaskDefinition) goerror.Error
UpdateTaskDefinition(t TaskDefinition) goerror.Error
SetWorkflowDefinition(t WorkflowDefinition) goerror.Error
CreateWorkflowDefinition(t WorkflowDefinition) goerror.Error
UpdateWorkflowDefinition(t WorkflowDefinition) goerror.Error
}

0 comments on commit 67788bb

Please sign in to comment.