Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

CSV module

kenn22 edited this page Sep 7, 2020 · 7 revisions

The CSV module has 4 endpoints:

  • create table columns from csv header
  • update csv field mapping
  • import csv to existing table
  • export csv errors to another csv

Create table columns from csv

POST /api/csv-imports/

Request body (form-data)

{
    "file": <file_obj>,
    "delimiter": ";"
}

Response

{
    "import_id": <import_id>,
    "fields": [
        {
            "original_name": "<original_csv_column_name>",
            "display_name": "<table_column_display_name>",
            "field_type": "<field_type>",
            "field_format": "<field_format>"
        }
    ]
}

The fields object from this response can be used to update the field mapping:

  • <original_csv_column_name> needs to be left untouched - the actual field name from CSV header
  • <table_column_display_name> is users's desired display name for the final table's column ( will be transformed into "snake_case" for the field_name)
  • <field_type> should be one of int, float, text, date, enum. In case of enum, during import all values from this column will be mapped as choices into the field.
  • <field_format> is used and required only for field_type=date, and should be a python datetime format string ( E.g: %d.%m.%Y for a date like 11.05.1989)

Create table and Update csv field mapping

POST /tables/

Request body (form-data)

{
    "database": <database_id>,
    "name": "<table_name>",
    "import_id": <csv_import_id>,
    "fields": [
        {
            "original_name": "<original_csv_column_name>",
            "display_name": "<table_column_display_name>",
            "field_type": "<field_type>",
            "field_format": "<field_format>"
        }
    ]
}

<csv_import_id> is the id returned by csv-prepare-fields

Response

{
    "errors_count": <errors_count>,
    "imports_count": <imports_count>,
    "errors": [
        {
            "row": {
                "<csv_field_name>": "<csv_field_value>",
            },
            "errors": {
                "<csv_field_name>": "<error_type>"
            }
        }
    ]
}

This method will actually do the csv import using the mapped field types. In case of errors, these will be present in the errors key.
<error_type> will specify what kind of error occurred (ValueError for wrong field format or type)

Import csv to existing table

PUT /tables/{id}/csv-manual-import/

Request body (form-data)

{
    "file": <file_obj>,
    "delimiter": ";"
}

Response

{
    "import_id": <import_id>,
    "errors_count": <errors_count>,
    "imports_count": <imports_count>,
    "errors": [
        {
            "row": {
                "<csv_field_name>": "<csv_field_value>",
            },
            "errors": {
                "<csv_field_name>": "<error_type>"
            }
        }
    ]
}

This method will import the uploaded file into specified table, and will return the same response as previous method.

Export csv errors to another csv

GET /csv-imports/{id}/export-errors/

Clone this wiki locally