Files
Gainsight/Scripts/API_Tests/add_learnerprop.py

83 lines
2.2 KiB
Python
Raw Normal View History

2023-01-05 17:15:12 -05:00
import requests
import pandas as pd
import Apikeys
2023-01-05 17:15:12 -05:00
csv = "/Users/normrasmussen/Downloads/client-learners.csv"
apiKey = Apikeys.client
2023-01-05 17:15:12 -05:00
baseUrlemail = "https://api.northpass.com/v2/people?filter[email][eq]="
def readcsv(csv, apiKey, baseUrlemail):
emails = []
data = pd.read_csv(csv)
emails.extend(data["email"].tolist())
getfromEmail(emails, baseUrlemail, apiKey, data)
def getfromEmail(emails, baseUrlemail, apiKey, data):
email_ids = {}
errors = []
for email in emails:
url = baseUrlemail + f"{email}"
headers = {
"accept": "*/*",
"x-api-key": apiKey,
"content-type": "application/json",
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
response = response.json()
try:
uuid = response["data"][0]["id"]
print(uuid)
email_ids.update({email: uuid})
except Exception as e:
errors.append(email)
pass
else:
print("Another Error!")
data["uuid"] = data["email"].map(email_ids)
2023-06-26 19:05:11 -04:00
data.dropna(how="any", inplace=True)
2023-01-05 17:15:12 -05:00
assignProps(data, apiKey)
errorstoCsv(errors)
def errorstoCsv(errors):
errorcsv = pd.DataFrame(errors)
errorcsv.to_csv(
2023-06-26 19:05:11 -04:00
path_or_buf="/Users/normrasmussen/Downloads/Emails_Not_Northpass.csv",
index=False,
)
2023-01-05 17:15:12 -05:00
def assignProps(data, apiKey):
for row in data.itertuples():
print(row)
ts_id = row[2]
ts_role = row[3]
uuid = row[4]
url = "https://api.northpass.com/v2/properties/people/bulk"
payload = {
2023-06-26 19:05:11 -04:00
"data": [
{
2023-01-05 17:15:12 -05:00
"type": "person_properties",
2023-06-26 19:05:11 -04:00
"attributes": {
"properties": {"user_id": ts_id, "role_type": ts_role}
},
2023-01-05 17:15:12 -05:00
"id": uuid,
2023-06-26 19:05:11 -04:00
}
]
}
2023-01-05 17:15:12 -05:00
headers = {
"accept": "application/json",
"content-type": "application/json",
"X-Api-Key": apiKey,
}
response = requests.post(url, json=payload, headers=headers)
print(response)
if __name__ == "__main__":
readcsv(csv, apiKey, baseUrlemail)