Skip to main content

The provided code demonstrates a Python script that utilizes the IC Project API to create projects based on data stored in a CSV file. The script reads the CSV file, extracts project information, and sends a request to the IC Project API to create each project. This article will walk you through the code and explain its functionality in detail.

Importing Required Libraries:

The code begins by importing the necessary libraries, namely csv and requests. These libraries enable reading the CSV file and making HTTP requests to the IC Project API, respectively. Here’s the code snippet:

import csv
import requests

Authentication and Instance Information:

Next, you’ll notice the declaration of two variables: authorization_token and instance_slug. These variables are essential for authenticating the API requests. Make sure to replace the empty strings with your actual authorization token and instance slug obtained from your IC Project instance settings. Here’s an example of how to populate these variables:

authorization_token = "YOUR_AUTHORIZATION_TOKEN"
instance_slug = "YOUR_INSTANCE_SLUG"

Reading and Processing the CSV File:

The script then proceeds to open the CSV file named ‘sample-projects.csv’ using a with statement. This ensures that the file is closed automatically after it’s processed. Inside the with block, a csv.reader object is created, which reads the CSV file and splits each row into separate fields based on the specified delimiter (‘,’ in this case). Here’s an example:

with open('sample-projects.csv') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')

Skipping the Header Row:

To skip the header row of the CSV file, the next function is called on the reader object, which effectively advances the reader to the next row without returning any data. Here’s an example:

next(reader, None)

Creating Projects from CSV Data:

The script then enters a loop that iterates over each row of the CSV file. The values from each field in the row are unpacked into the respective variables: project_name, date_start, date_end, description, and status. Here’s an example:

for project_name, date_start, date_end, description, status in reader:

Making API Requests to Create Projects:

Within the loop, the script constructs the project data and headers required for the API request. A POST request is made to the IC Project API using the requests.post method. Here’s an example:

params = {
    "name": project_name,
    "dateStartPlanned": date_start,
    "dateEndPlanned": date_end,
    "category": None,
    "tags": None,
    "description": description,
    "isBlameableRemovalEnabled": True,
    "status": status,
    "budget": 0
}

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

response = requests.post(
    f"https://app.icproject.com/api/instance/{instance_slug}/project/projects",
    json=params,
    headers=headers,
)

Handling Response and Errors:

After making the request, the script prints the response status code using the print function. This indicates whether the project creation was successful or if any errors occurred. In case of an error (response status code other than 200), the script prints the response content, which can provide additional information about the issue encountered during the API call. Here’s an example:

print("\t", response.status_code)

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

Conclusion:

In this article, we explored a Python script that leverages the IC Project API to create projects based on data stored in a CSV file. The code examples provided demonstrate how to read the CSV file, construct API requests with the required data and headers, and handle response codes and errors. By customizing the code to fit your specific needs and providing the appropriate authorization token and instance slug, you can automate the creation of projects in your IC Project instance using data from a CSV file.

Full code of this example You can find on GitHub:

https://github.com/ngroup-pl/icp-api-examples/blob/master/python-import-projects-from-csv/

No Comments yet!

Your Email address will not be published.