-
-
Notifications
You must be signed in to change notification settings - Fork 1
Configuration
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.
This section accepts a boolean (true
or false
) value. 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.
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.
This section accepts 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
This section accepts many parameters and objects. Allows you to specify settings for your REST API instance.
This section accepts a string. Allows you to specify a base URL address (host) for your REST API.
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.
This section accepts a string. Allows you to specify an auth method for making requests to your REST API.
For example, Bearer
.
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!
It is recommended to keep such data under protection, for example by using environment variables. For example, you can specify a token like this when you start the parser:
MYVAR_API_TOKEN=secret-1234567 csv2api -c /path/to/config.yml -d /path/to/data.csv -e MYVAR
During initialization, the parser will insert your token from env into this configuration section by itself.
βοΈ Please note: to be built into the configuration correctly, you must specify environment variables in object style, but use underscores (
_
) instead of dots (.
).
This section accepts an int. Allows you to specify a request timeout for HTTP client.
This section accepts object. Allows you to specify endpoint to update actions from your REST API.
This section accepts a string. Allows you to specify an endpoint name (path).
For example, if you need to set a primary key (PK) to endpoint path, this section support Go markers (like you use in fmt.Sprintf
function):
endpoint_name: /api/v1/order/%s # <-- '%s' is a stub where PK will be inserted
This section accepts a string. Allows you to specify an endpoint's Content-Type
.
This section accepts a boolean (true
or false
) value. Allows you to set a primary key (PK) to the endpoint name (path).
βοΈ Please note: if you set this setting to
true
, then you must necessarily specify a stub in theendpoint_name
option.
This section accepts a string. Allows you to specify an endpoint's HTTP method.
This section accepts an 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
andupdate_fields
sections.
This section accepts a list of objects. Allows you to describe in a simple format the structure of the filter for input data.
βοΈ Please note: there is a logical
AND
between all filters.
This section accepts a string. Allows you to specify a column name for filtering.
This section accepts the one of the values: EQ
, NEQ
, GT
, LT
, GTE
, LTE
. Allows you to specify a condition for filtering.
For string
or int
values:
-
EQ
β equals to the given value (==
) -
NEQ
β not equals to the given value (!=
)
Only for int
values:
-
GT
β greater than the given value (>
) -
LT
β less than the given value (<
) -
GTE
β greater than or equal to the given value (>=
) -
LTE
β less than or equal to the given value (<=
)
This section accepts a string. Allows you to specify a column value for filtering.
βοΈ Please note: if the value should be empty, specify it as
''
.
This section accepts a list of objects. Allows you to specify fields to be updated in your REST API.
This section accepts a string. Allows you to specify a field name for updating in your REST API.
This section accepts a list of strings. Allows you to specify field value(s) for updating in your REST API.
This section accepts a list of objects. Allows you to specify conditions for the field.
βοΈ Please note: there is a logical
AND
between all conditions.
This section accepts a string. Allows you to specify a column name for filtering.
This section accepts the one of the values: EQ
, NEQ
, GT
, LT
, GTE
, LTE
. Allows you to specify a condition for filtering.
For string
or int
values:
-
EQ
β equals to the given value (==
) -
NEQ
β not equals to the given value (!=
)
Only for int
values:
-
GT
β greater than the given value (>
) -
LT
β less than the given value (<
) -
GTE
β greater than or equal to the given value (>=
) -
LTE
β less than or equal to the given value (<=
)
This section accepts a string. Allows you to specify a column value for filtering.
βοΈ Please note: if the value should be empty, specify it as
''
.
Sometimes it happens, it's trueβ¦ but don't get upset! π
Wiki is a dynamically growing section, so write us your question, and we'll try to answer it. If the question is interesting and/or repeated too often, we'll add it to a FAQ section (with reference to the author's issue).
The best way to ask a question is to create a new issue or a discussion in GitHub repository.
So, follow this checklist to ask a question that we can answer in the shortest possible time:
- Try to search similar question in the issues section.
- If your question is about Go, or else, try to search at StackOverflow first.
- Please fully follow our template to create a new issue.
- If English is not your native language, please use an online translator in advance (for example, DeepL or Google Translate).
- Demonstrate understanding to authors, because this is Open Source and not-for-profit product, and their support is not paid.
- Be nice to the other members of our community.