-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpcd_update_data.py
More file actions
47 lines (35 loc) · 1.33 KB
/
pcd_update_data.py
File metadata and controls
47 lines (35 loc) · 1.33 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
# pip install pandas # if needed
import pandas as pd
import json
from utils.session_manager import SessionManager
from utils.dynamics_config import get_config
# Parameters
PathToDotEnv = "env.example"
EntityBeingAddedTo = "contacts"
PathToCSVOfRecords = "data\pcd_update_records.csv"
# Column names in CSV must match EntityM and EntityN above
# Getting authenticated Requests object.
config = get_config(PathToDotEnv)
manager = SessionManager(config)
session = manager.get_authenticated_session()
session.headers.update({'If-Match': '*'})
# reading the CSV
df = pd.read_csv(PathToCSVOfRecords)
records = json.loads(df.drop(columns='GUID').to_json(orient = "records"))
successful_updates = 0
failures = 0
expected_updates = len(df)
for index, row in df.iterrows():
guid = row['GUID']
request_uri = f'{config.api_base_url}{EntityBeingAddedTo}({guid})'
post_json = records[index]
r = session.patch(request_uri, json = post_json)
if r.status_code != 204:
failures += 1
raw = r.content.decode('utf-8')
print(f'Error updating {guid}. Error {r.status_code}: \n{raw}\n')
else:
successful_updates += 1
if index % 10 == 0:
print(f"Processed: {index + 1}")
print(f'{successful_updates} UPDATES MADE OF {expected_updates} EXPECTED UPDATES.\n{failures} FAILURES.')