# `Bobbin.Integration`
[🔗](https://github.com/heddlehq/Bobbin/blob/main/lib/bobbin/integration.ex#L1)

An integration written as a manifest: a YAML file that describes a service's
credential and its actions as HTTP requests, with no code.

Each action becomes a node in Heddle, with a `success` and an `error` output.

## Example

```yaml
id: acme
name: Acme
version: 1.0.0
description: Tickets in Acme.
icon: hero-ticket
base_url: https://api.acme.test/v1/

credential:
  label: Acme
  fields:
    - name: api_key
      label: API key
      secret: true
  auth:
    type: header
    name: X-Api-Key
    value: "{{credential.api_key}}"

actions:
  - name: Create Ticket
    description: Opens a ticket.
    fields:
      - name: title
        label: Title
    request:
      method: POST
      path: tickets
      body:
        title: "{{fields.title}}"
    output:
      pick: [id, url]
```

## Templates

Strings in `request`, `credential.auth` and `output.value` may hold
`{{fields.<name>}}` and `{{credential.<name>}}`. A string that is a single
placeholder keeps the value's type. Values that end up empty are left out of the
query and the body, so optional fields need no extra syntax.

## Credential `auth` types

  * `bearer`: `token`
  * `header`: `name` and `value`
  * `query`: `name` and `value`
  * `basic`: `username` and `password`
  * `oauth2`: `authorize_url`, `token_url` and `scopes`. The host runs the
    authorization and passes the access token as `{{credential.access_token}}`.
  * `none`

## Responses

  * `errors.ok_path`: a field of the body that must be `true`, for APIs that
    answer 200 when something failed. `errors.message_path` holds why.
  * `output.path`: the part of the body the node returns, like `data.items`.
  * `output.pick`: the keys to keep, from each item of a list.
  * `output.value`: a value built from templates, in place of the body.

## Pagination

`pagination.type` is `cursor`, `offset` or `link` (the `Link` header). The node
returns the items of every page, from `pagination.items_path`.

## Triggers

`triggers` start a flow when something new shows up in the service. A trigger is
written like an action whose output is a list, plus `poll`:

```yaml
triggers:
  - name: New Ticket
    request:
      method: GET
      path: tickets
    output:
      path: tickets
    poll:
      every: 5   # minutes, 5 when left out
      id: id     # the key that tells items apart, "id" when left out
```

The host calls the request every `every` minutes and starts the flow once for each
item whose `id` it hasn't seen. The first call only records what is already there.

See `priv/integration.schema.json` for the full format.

# `t`

```elixir
@type t() :: %{
  id: String.t(),
  name: String.t(),
  version: String.t(),
  description: String.t(),
  icon: String.t(),
  category: String.t(),
  base_url: String.t(),
  credential: map() | nil,
  errors: map(),
  actions: [map()],
  triggers: [map()]
}
```

# `action`

```elixir
@spec action(t(), String.t()) :: map() | nil
```

The action called `name`, or nil.

# `load`

```elixir
@spec load(Path.t()) :: {:ok, t()} | {:error, [String.t()]}
```

Reads and checks the manifest at `path`.

# `load!`

```elixir
@spec load!(Path.t()) :: t()
```

Like `load/1`, raising with what is wrong.

# `parse`

```elixir
@spec parse(String.t()) :: {:ok, t()} | {:error, [String.t()]}
```

Parses and checks a manifest written in YAML.

# `trigger`

```elixir
@spec trigger(t(), String.t()) :: map() | nil
```

The trigger called `name`, or nil.

# `validate`

```elixir
@spec validate(map()) :: {:ok, t()} | {:error, [String.t()]}
```

Checks a manifest already decoded into a map with string keys.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
