Web API import

In addition to import files, one can import data in the system via a Web API service.

Web API Authentication

The Web API import is only possible with an authentication key. The Web API process requires an authentication via a public key and the variable AGREGG_RSA_TIME_ENCRYPTION_ENABILITY.

The user must have right permission to use this function: the role id 24 (Web API Reader and Writer) must be activated.

With every post add the parameter “qsauth”:

current_time = int(time.time())
encrypted  = encrypt_rsa(str(current_time) + AGREGG_RSA_TIME_ENCRYPTION_ENABILITY + str(user.username) )
param = urlencode({'qsauth': str(encrypted)})

def encrypt_rsa(text):
            from Crypto.PublicKey import RSA
            from base64 import b64encode
            public_key = RSA.importKey('<CHIAVE_PUBBLICA>', None)
            sutf8 = text.encode('utf8')
            enc = public_key.encrypt(sutf8, None)[0]
            enc_b64 = b64encode(enc)
            return enc_b64

Post Row by Row

With this method there are three steps to follow:

  • start_import: After starting a new import, an import_id is returned:
POST: http://localhost:8081/webapi/start_import/
{
            "kpi_import_type_id": 1,
            "from_who_id": 1,
            "period_end_date": "20170930"
}

Return:
    {'import_id': import_id }
  • insert_row: Then one makes a POST for every row:
Parametri: import_id
POST: http://localhost:8081/webapi/insert_row/<import_id>/
{
    "row_number": 1,
    "kpi_code": "GDWP",
    "currency_code": "EUR",
    "kpi_value": 10000,
    "from_who": 4,
    "generic_filters" : [
                    {
                    "kpi_filter": 8,
                    "filter_item_key": "12" // NB in caso di filtro data YYYYMMDD
                    }
    ]
}
Return:
    {'row_id': row.pk, 'import_id': import_id}
  • end_import: Finally, at the end of the rows, one has to confirm the import:
Parametri: import_id
POST: http://localhost:8081/webapi/end_import/<import_id>/
{
            "source": "rmp"
}

Web API Flat Import

In this case one directly posts the full JSON object.

POST: http://localhost:8081/webapi/insert_new_file/
{
    "kpi_import_type_id": 2,
    "enable_new_filters" : "False",
    "from_who_id": 4,
    "period_type_selection": 1,
    "period_end_date": "20170930",
    "source": "SOURCE",
    "rows": [
        {
            "row_number": 1,
            "kpi_code": "TEST",
            "currency_code": "EUR",
            "kpi_value": 10000,
            "from_who": 4,
            "generic_filters" : [
                {
                "text": "2017",
                "display_text": "2017",
                "col_index": 1,
                "attribute_json": ""
                }
            ]
        },
        {
            "row_number": 2,
            "kpi_code": "TEST",
            "currency_code": "EUR",
            "kpi_value": 5000,
            "from_who": 4,
            "generic_filters" : [
                {
                "text": "2016",
                "col_index": 1
                }
            ]
        }
    ]
}

Create or update Kpi

http://localhost:8000/webapi/create_kpi/
[{
    "kpi_code": "TEST_TEST",
    "description": "description",
    "kpi_title": "Title",
    "kpi_measure_type": "decimal",
    "kpi_formula": "",
    "kpi_aggregation_type": "sum",
    "import_type_id": 1,
    "kpi_value_scale": 1,
    "kpi_value_scale_output": 1,
    "period_type": "0_actual",
    "projection": 0,
    "number_of_decimals": 2,
    "reset": "True",
    "ignore_aggregation": "False",
    "priority": 0,
}]

Insert Update From Who

It is also possible to update the business unit hierarchy via Web API:

POST: http://localhost:8081/webapi/insert_update_from_who/
[{
    "from_who_id" : 95,
    "area_id" : 9,
    "area" : "area name",
    "area_latitude" : "51.360634",
    "area_longitude" : "10.59082",
    "area_currency" : 6,
    "area_order_number" : 1,
    "area_code" : "YX"
}]

[{
    "from_who_id" : 1,
    "area_id" : 1,
    "area" : "area name",
    "country_id" : 1,
    "country" : "country name",
    "business_unit_id" : 1,
    "business_unit"  : "bu name",
    "sub_business_unit_id" : 1,
    "sub_business_unit" : "sub bu name",
    "attribute_json" : "",

    "area_latitude" : "51.360634",
    "area_longitude" : "10.59082",
    "area_currency" : 6,
    "area_order_number" : 1,
    "area_code" : "YX",

    "country_latitude" : "51.360634",
    "country_longitude" : "10.59082",
    "country_currency" : 6,
    "country_order_number" : 1,
    "country_code" : "YY",

    "business_unit_latitude" : "51.360634",
    "business_unit_longitude" : "10.59082",
    "business_unit_currency" : 6,
    "business_unit_order_number" : 1,
    "business_unit_code" : "XY",

    "sub_business_unit_latitude" : "51.360634",
    "sub_business_unit_longitude" : "10.59082",
    "sub_business_unit_currency"  : 6,
    "sub_business_unit_order_number" : 1,
    "sub_business_unit_code" : "XX"

}]

Partial Import

An import file is associated to the following fields:

When importing a new file, all data that are already in the database and correspond to the previous fields are erased, and are substituted with the new ones, if any.

When importing via Web API, it is possible to associate to an import file filters and attributes via the fields “gf_import_items” and “gf_import_attributes”.

The field “gf_import_items” contains a JSON with the list of generic filters associated to the imported file:

[
{
"filter_item_level_num": 1,
"values": [
    {"value": "4", "key": "level_item_id_1"}
],
"filter_id": 1
},
{
"filter_item_level_num": 1,
"values": [
    {"value": "3", "key": "level_item_id_2"}
],
"filter_id": 2
}
]

In this example the import file is associated to - the filter with ID 1, level number 1 and value 4 in the field level_item_id_1, - the filter with ID 2, level number 1 and value 3 in the field level_item_id_2.

The field “gf_import_attributes” contains a JSON with the list attributes associated to the imported file:

[
{
"filter_item_level_num": 4,
"values": [
{"value": "0000H", "key": "level_item_42_attr_1"},
{"value": "01", "key": "level_item_43_attr_1"}
],
"filter_id": 24
}
]

In this case the import file is associated to the attribute of the filter with ID 24, level number 4 and value 0000H in the field level_item_42_attr_1 and value 01 in the field level_item_43_attr_1.

With these fields one can create partial imports, i.e. import files associated to a subset of filters/attributes, in addition to the standard fields.

Post file to batch_files folder

POST: http://localhost:8081/webapi/post_file_to_batch
DATA: request.FILES['files']