Product

Simpler API helper python script to import data from CSV file

Based on feedback, we have simplified the API helper script to upload data to device42 instance from a csv file. The new script now has only one section that needs to be changed. API argument info is taken from the csv file header row.

Requirement and script location

Only requirement is python 2.7.x. Available at: http://www.python.org/download/releases/

v2.1 of the API helper script is available at: https://github.com/device42/API_Helpers

Get the url and field names

This sample script can be easily modified to read a comma-delimited csv file of data for any of the API’s supported by device42 and documented at: https://docs.device42.com/apis/.

Example script uses the API to add/update custom key/values for application components as documented at: https://docs.device42.com/apis/application-component-custom-field-apis/

  • From the documentation, determine which API call you need to use.
  • From the documentation of the API call, note the URL (e.g. /api/1.0/custom_fields/appcomp/) and the URL method (either PUT or POST).
  • From the documentation of the API call, identity the mandatory fields you must include and the optional fields you wish to include.

Create the CSV file

API_helper_csv_header.png

Create comma separated CSV file with following:

  • The header row (first line) values must match the API field names found in the documentation.
  • After the header row, there should be one row of values for each data record that you need to send to the device42 appliance.
  • Each line in the CSV file must have a value for each mandatory field.

Modify the script

Modify_the_script.png

You will need to Change lines 26-31 in the script to match your environment:

  • D42_API_URL will be the d42 instance base url plus the api call url that you found in the API documentation.
  • D42_USERNAME and D42_PASSWORD are self explanatory.
  • API_METHOD will be put or post, depending on the documentation for the particular API.
  • CSV_FILE_NAME will be the name of the csv file with data. (As created in Step #4)
  • DEBUG can be changed to True or False, depending on how verbose you want the output to be.

Execute the script

Once you save the file, you can execute(python script_name.py). After the script completes, it will print two sections of information: The added section will show all the rows that were added successfully. The notadded section will have any rows that failed and the reason for the failure.

Share this post

About the author