Skip to content

Configuration

Vic Shóstak edited this page Jun 19, 2023 · 14 revisions

In this section, we'll take a closer look at how you can quickly and easily set up a configuration file to start implementing any of your tasks. You can use absolutely any format of configuration file:

  • JSON
  • YAML
  • TOML
  • HCL (Terraform)

The main condition is that your config file must contain the sections necessary for csv2api to work.

A configured config.yml file usually looks like this:

save_filtered_pk_to_csv: true

columns_order:
  - id
  - order_id
  - is_paid

csv_separator: ','

api:

  base_url: my-api-server.com
  base_url_schema: https
  auth_method: Bearer
  token: '{{ CONFIG_API_TOKEN }}'
  request_timeout: 0

  update_endpoint:
    endpoint_name: /api/v1/order/%s
    content_type: application/json
    add_pk_to_endpoint_name: true
    http_method: PATCH
    endpoint_body:
      data:
        order: id
        attributes:
          tags: tags

filter_columns:

  - column_name: order_id
    condition: NEQ
    value: ''

update_fields:

  - field_name: tags
    values:
      - paid
    conditions:
      - column_name: is_paid
        condition: EQ
        value: '1'

Let's look at each section in more detail.

save_filtered_pk_to_csv

This section accepts true or false. Allows you to specify whether to save the filtered primary keys (PK) to a CSV file.

This is useful when you need to keep track of which PK (IDs) have been filtered, to control how they change in the database or elsewhere.

columns_order

This section accepts a list of strings. Allows you to specify the order of the fields for filtering and updating.

❗️ Please note: the first element in this list is the primary key (PK), which will be used in other configuration sections.

csv_separator

This section accepts the value of a string. Allows you to specify a separator between columns in a CSV data file.

❗️ Please note: the setting must match the parameters of the Go standard library: https://pkg.go.dev/encoding/csv#Writer

api

This section accepts many parameters and objects. Allows you to specify settings for your REST API instance.

base_url

This section accepts the value of a string. Allows you to specify a base URL address (host) for your REST API.

base_url_schema

This section accepts the one of the values: http or https. Allows you to specify an HTTP schema for the base URL address (host) for your REST API.

auth_method

This section accepts a string. Allows you to specify an auth method for making requests to your REST API.

For example, Bearer.

token

This section accepts a string. Allows you to specify an auth token for making requests to your REST API.

WARNING: do not forget to add your config file to a .gitignore, if you place API token to this section!

❗️ Please note: to be built into the configuration correctly, you must specify environment variables in object style, but use underscores (_) instead of dots (.).

request_timeout

update_endpoint

endpoint_name

content_type

add_pk_to_endpoint_name

http_method

endpoint_body

Accepts the object. Allows you to describe in a simple format the structure of the body of a request.

For example, if your API endpoint has a body structure like this:

{
  "data": {
    "order": "1",
    "attributes": {
      "tags": [
        "new",
        "paid"
      ]
    }
  }
}

Then the configuration for this subsection will be like this:

api:

  # ...

  update_endpoint:

    # ...

    endpoint_body:
      data:
        order: id
        attributes:
          tags: tags

❗️ Please note: you must use exactly the field/column names, that you specified in the columns_order and update_fields sections.

Clone this wiki locally