-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathimport requests_basicauth.py
More file actions
75 lines (65 loc) · 2.54 KB
/
import requests_basicauth.py
File metadata and controls
75 lines (65 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import requests
import pandas as pd
import sys
import os
import base64
# Query user for configuration
openproject_url = input('Enter your OpenProject URL (e.g., https://openproject.local): ').strip().rstrip('/')
api_token = input('Enter your OpenProject API token: ').strip()
project_id = input('Enter your OpenProject numeric project ID (e.g., 5): ').strip()
csv_file = input('Enter the full path to the CSV file to import (e.g., D:/path/to/file.csv): ').strip()
# Remove any surrounding quotes from the input path
csv_file = csv_file.strip('"').strip("'")
# Check if file exists
if not os.path.isfile(csv_file):
print(f" The file '{csv_file}' does not exist. Please check the path and try again.")
sys.exit(1)
# Load CSV
df = pd.read_csv(csv_file)
# Set Basic Auth header using 'apikey' as username and API token as password
auth_header = base64.b64encode(f'apikey:{api_token}'.encode('utf-8')).decode('utf-8')
headers = {
'Authorization': f'Basic {auth_header}',
'Content-Type': 'application/json'
}
# Test API connectivity
try:
test_response = requests.get(f"{openproject_url}/api/v3/projects/{project_id}", headers=headers)
if test_response.status_code != 200:
print(f" API token test failed — Status {test_response.status_code}: {test_response.text}")
sys.exit(1)
else:
print(f" API token verified. Proceeding with import.")
except Exception as e:
print(f" Failed to connect to API: {e}")
sys.exit(1)
# Loop over each row and create work package
for idx, row in df.iterrows():
payload = {
"_links": {
"project": {
"href": f"/api/v3/projects/{project_id}"
},
"type": {
"href": f"/api/v3/types/1" # usually '1' is Task, adjust if needed
}
},
"subject": row['Subject'],
"description": {
"format": "markdown",
"raw": row['Description']
},
"status": {
"name": row['Status']
},
"priority": {
"name": row['Priority']
}
}
response = requests.post(f"{openproject_url}/api/v3/work_packages", headers=headers, json=payload)
if response.status_code == 201:
print(f" Created work package: {row['Subject']}")
else:
print(f" Failed to create {row['Subject']} — Status {response.status_code}: {response.text}")
# Built by @SOCKS for OpenProject Ingestion
# https://github.com/773-process-312/OpenProject_CSV_Import.git