-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpcd_create_records.py
More file actions
64 lines (51 loc) · 1.97 KB
/
pcd_create_records.py
File metadata and controls
64 lines (51 loc) · 1.97 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
import json
import pandas as pd
import time
from requests import Request
from utils.session_manager import SessionManager
from utils.dynamics_config import get_config
# Parameters
PathToDotEnv = "env.example"
EntityBeingAddedTo = "contacts"
PathToCSVOfRecords = "data\pcd_create_records.csv"
AttributesToReturn = "?$select=lastname" # optionally include the attributes to return, otherwise all are returned
# The Pandas data types of the columns imported to avoid import issues
dtypes = {
"parentcustomerid_account@odata.bind": "object",
"lastname": "object",
"firstname": "object"
}
# read the CSV and convert to dataframe
df = pd.read_csv(PathToCSVOfRecords, dtype = dtypes)
records = json.loads(df.to_json(orient = "records"))
# Getting authenticated Requests object.
config = get_config(PathToDotEnv)
manager = SessionManager(config)
session = manager.get_authenticated_session()
session.headers.update({"Prefer" : "return=representation"})
# the post uri
request_uri = f'{config.api_base_url}{EntityBeingAddedTo}{AttributesToReturn}'
row = 0
successful_updates = 0
failures = 0
expected_updates = len(df)
percent_complete = 0
timeStart = time.perf_counter()
for record in records:
req = Request('POST', request_uri, json=record, headers = session.headers).prepare()
r = session.send(req)
record['HTTP_RESPONSE'] = r.status_code
record['HTTP_CONTENT'] = json.loads(r.content.decode('utf-8'))
if r.status_code != 201:
failures += 1
else:
successful_updates +=1
row += 1
if round(row/expected_updates * 100,0) != percent_complete:
percent_complete = round(row/expected_updates * 100,0)
print(f"{percent_complete}% complete")
print(f'{successful_updates} UPDATES MADE OF {expected_updates} EXPECTED UPDATES. {failures} FAILURES.')
print(f'IMPORTING TOOK: {round(time.perf_counter() - timeStart,0)} SECONDS ')
# Writing to output.json
with open("output/output.json", "w") as outfile:
outfile.write(json.dumps(records))