Skip to main content

Introduction: This article presents a Python script that demonstrates how to download a list of projects from the IC Project API and save them to a CSV file. The script makes an API request to retrieve the project data and utilizes the csv module to write the data into a CSV file. By following the code examples provided, you can easily export project information from your IC Project instance to a CSV file for further analysis or reporting purposes.

Importing Required Libraries:

The script begins by importing the necessary libraries: csv, sys, and requests. The csv library is used for writing data to a CSV file, sys is used for system-specific functionality, and requests is used to make HTTP requests to the IC Project API. Here’s the code snippet:

import csv
import sys
import requests

Authentication and Instance Information:

Next, the script declares two variables: authorization_token and instance_slug. These variables store the authorization token and instance slug needed for API authentication. Make sure to replace the empty strings with the actual values obtained from your IC Project instance settings. Here’s an example:

authorization_token = "YOUR_AUTHORIZATION_TOKEN"
instance_slug = "YOUR_INSTANCE_SLUG"

Making an API Request to Retrieve Project Data:

The script makes a GET request to the IC Project API to fetch the list of projects. The necessary headers, including the authorization token, are included in the request. Here’s an example:

headers = {
    'X-Auth-Token': authorization_token,
    'Accept': 'application/json',
}

response = requests.get(
    f"https://app.icproject.com/api/instance/{instance_slug}/project/projects?pagination=0",
    headers=headers,
)

Checking Response Status Code:

After making the API request, the script checks the response status code to ensure the request was successful. If the status code is not 200 (indicating a successful response), an error message is printed, and the script exits. Here’s an example:

if response.status_code != 200:
    print(response.status_code, response.content)
    sys.exit()

Defining Columns for Export:

The script defines the columns to export to the CSV file. Each column consists of a key, a label, and an optional value formatting function. The key represents the field name in the project data, the label is the column header in the CSV file, and the formatting function allows custom formatting of the values if needed. Here’s an example:

columns = (
    ('id', 'ID', None),
    ('no', 'Number', None),
    ('name', 'Name', None),
    # ...
)

Writing Project Data to CSV:

The script opens a CSV file named ‘projects.csv’ in write mode using the csv.writer object. It writes the column headers to the file as the first row. Then, for each project in the API response, it extracts the relevant data based on the defined columns and writes a row to the CSV file. Here’s an example:

with open('projects.csv', 'w', newline='') as csv_file:
    writer = csv.writer(csv_file, delimiter=',', quotechar='"')

    # Write column headers
    writer.writerow([k[1] for k in columns])

    # Loop through projects
    for project in response.json():
        # ...
        row = []
        for key, label, formatter in columns:
            value = project[key]

            # Apply formatting if specified
            if formatter:
                value = formatter(value)

            row.append(value)

        writer.writerow(row)

Conclusion:

In this article, we explored a Python script that utilizes the IC Project API to download a list of projects and save them to a CSV file. By following the code examples and replacing the necessary authorization token and instance slug, you can easily adapt the script to export project data from your IC Project instance. The resulting CSV file can be used for further analysis, reporting, or any other purpose that requires project information in a tabular format.

You can find full source code of this example on GitHub

https://github.com/ngroup-pl/icp-api-examples/tree/master/python-export-projects-to-csv

No Comments yet!

Your Email address will not be published.