# Cain
Camunda-REST-API-Interpreter to handle common Camunda specific workflow use cases for a more clearly usage by hiding the REST-Calls under the hood.

### Covered

- Handle external tasks
- Invoke DMN evaluations

## HTTP-Client

`Cain.ExternalWorker` and `Cain.DecisionTable` are using `Cain.Client.Default` for submitting HTTP requests on default.
Therefore it's using the [Tesla]( library. If you want to add you own HTTP-Client it has to implement the `Cain.Client` behaviour and also has to support handling of JSON data.

Using `Cain.Client.Default` requires to add following configuration in your config.

config :cain, Cain.Endpoint, url: "http://localhost:4004/engine-rest/"

## External Task Client

Use `Cain.ExternalWorker` for referencing your external task function implementation in your application.

defmodule MyWorker do
  use Cain.ExternalWorker, [
    client: My.HTTPClient   # default: Cain.Client.Default
    max_tasks: 5,           # default: 3
    use_priority: true,     # default: false
    polling_interval: 1000  # default: 3000

Add `register_topics/1` and create a list of a tuple with the following elements:
- Name of the topic that has been given in the BPMN-Process-Model
- Function implementation with a tuple of Module, function and args the topic refers to
- Setting lock duration in milliseconds, defaults to 3000ms

def register_topics do
  [{:my_topic, {MyTopicHandler, :handle_topic, [:my_arg]}, [lock_duration: 5000]}]

Response in the referenced function by using the provided API in `MyWorker`.
> Notice: The payload of a topic fetch is always provided as the first argument and needs to be considered on your function implementation

defmodule MyTopicHandler do

  def handle_topic(payload, :my_arg) do


    case an_external_service_call() do
      :ok -> 
          # provide a map with atom keys to response with variables

      {:error, error} -> 
          MyWorker.retry("external_service_error", inspect(error), 3, 3000)

      _unexpected -> 
          MyWorker.create_incident("unexpected_error", "See the logs")


## DMN Evaluation

Use `Cain.DecisionTable` and set the corresponding `definition_key` of the deployed table in the engine.

defmodule MyDecisionTable do
  use Cain.DecisionTable, 
    client: My.HTTPClient   #default: Cain.Client.Default
    definition_key: "MY_TABLE"
And evaluate.

MyDecisionTable.evaluate(%{first: 1, second: "Second"})
# {:ok, [%{is_valid: true}]}

The return value depends on the output definition of the modeled DMN table. 

## Installation

<!-- If [available in Hex](, the package can be installed
by adding `cain` to your list of dependencies in `mix.exs`: -->

def deps do
    {:cain, "~> 0.3.0"}
Documentation can be generated with [ExDoc](
and published on [HexDocs]( Once published, the docs can
be found at []( -->