Demonstration: Fetch an external configuration file, with Mechanic.

Mechanic is a development platform for Shopify. :)

Demonstration: Fetch an external configuration file

by Isaac Bowen (team@usemechanic.com)

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.

Runs when a user triggers the task and when an action is performed. Configuration includes save data to cache.

15-day free trial – unlimited tasks

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?

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

Events
when a user triggers the task (mechanic/user/trigger)
when an action is performed (mechanic/actions/perform)
Options
save data to cache (boolean)
Script
{% 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://docs.usemechanic.com/article/370-the-cache-object
    {% 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://docs.usemechanic.com/article/335-preview-actions
    {% 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 tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.