Mechanic Get Mechanic
Task library

Demonstration: Fetch an external configuration file

This task script demonstrates using the "http" action to retrieve external data, using a GET request. It also demonstrates saving the retrieved data to the Mechanic cache, where it can be used for future task runs.

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs when a Mechanic action is performed

What you choose

Save data to cache

Documentation

This task script demonstrates using the "http" action to retrieve external data, using a GET request. It also demonstrates saving the retrieved data to the Mechanic cache, where it can be used for future task runs.

Learn more about this technique: Can I load data from external sources?

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

mechanic/user/trigger
mechanic/actions/perform
How task subscriptions work

Option types and flags

Task code

{% if event.topic == "mechanic/user/trigger" %}
  {% action "http" %}
    {
      "method": "get",
      "url": "http://example.com/config.txt"
    }
  {% endaction %}

  {% if options.save_data_to_cache__boolean %}
    {% comment %}
      For details on reading from the cache, see
      https://learn.mechanic.dev/platform/liquid/objects/cache
    {% endcomment %}

    {% log cached_config: cache.demonstration_config %}
  {% endif %}
{% elsif event.topic == "mechanic/actions/perform" %}
  {% if event.preview %}
    {% comment %}
      For details on this technique, see
      https://learn.mechanic.dev/core/tasks/previews
    {% endcomment %}

    {% capture action_json %}
      {
        "type": "http",
        "run": {
          "result": {
            "body": "sample config"
          }
        }
      }
    {% endcapture %}

    {% assign action = action_json | parse_json %}
  {% endif %}

  {% if action.type == "http" %}
    {% assign config = action.run.result.body %}

    {% log retrieved_config: config %}

    {% if options.save_data_to_cache__boolean %}
      {% action "cache", "set", "demonstration_config", config %}
    {% endif %}
  {% endif %}
{% endif %}
Mechanic Liquid reference