Files
Gainsight/Scripts/API_Tests/addgroup_users.py

87 lines
2.5 KiB
Python
Raw Permalink Normal View History

2022-11-02 17:03:56 -04:00
import requests
import sys
import pandas as pd
import Apikeys
2022-11-02 17:03:56 -04:00
2022-11-03 21:06:46 -04:00
# Enter your API Key between the quotation marks.
apiKey = ""
groupName = sys.argv[1]
peopleCsv = "/path/to/file/peopletogroups.csv"
2022-11-02 17:03:56 -04:00
2023-01-05 17:15:12 -05:00
2022-11-02 17:03:56 -04:00
def createGroup(groupName, apiKey):
2023-01-05 17:15:12 -05:00
# apiKey = input("What is your API Key? ")
# Create a Group endpoint - params: group_uuid
2022-11-02 17:03:56 -04:00
url = "https://api.northpass.com/v2/groups"
payload = {"data": {"attributes": {"name": f"{groupName}"}}}
headers = {
2023-01-05 17:15:12 -05:00
"accept": "application/json",
"content-type": "application/json",
"X-Api-Key": apiKey,
2022-11-02 17:03:56 -04:00
}
groupresponse = requests.post(url, json=payload, headers=headers)
if "already have a group with this name" in groupresponse.text:
groupName = input("Group already created, what name would you like instead? ")
print("Got it, thanks! Attempting to create the group now.")
createGroup(groupName, apiKey)
else:
readCSV(apiKey, groupresponse)
2023-01-05 17:15:12 -05:00
2022-11-02 17:03:56 -04:00
def readCSV(apiKey, groupresponse):
people = []
readExport = pd.read_csv(
2023-01-05 17:15:12 -05:00
peopleCsv,
usecols=["UUID"],
skipinitialspace=True,
)
people.extend(readExport["UUID"].tolist())
2022-11-02 17:03:56 -04:00
getgroupId(people, groupresponse, apiKey)
2023-01-05 17:15:12 -05:00
2022-11-02 17:03:56 -04:00
def getgroupId(people, groupresponse, apiKey):
groupresponse = groupresponse.json()
2023-01-05 17:15:12 -05:00
groupID = groupresponse["data"]["id"]
2022-11-02 17:03:56 -04:00
ppltoGroup(people, groupID, apiKey)
2023-01-05 17:15:12 -05:00
2022-11-02 17:03:56 -04:00
def ppltoGroup(people, groupID, apiKey):
2022-11-03 21:06:46 -04:00
for uuid in people:
2023-01-05 17:15:12 -05:00
payload = {
"data": [
{"type": "people", "id": uuid},
]
}
2022-11-03 21:06:46 -04:00
# Add People to a Group. Params needed: group_uuid
url = f"https://api.northpass.com/v2/groups/{groupID}/relationships/people"
headers = {
"accept": "*/*",
"content-type": "application/json",
2023-01-05 17:15:12 -05:00
"X-Api-Key": apiKey,
}
2022-11-03 21:06:46 -04:00
response = requests.post(url, json=payload, headers=headers)
if "404" in response.text:
print("Error returned for this user. Collecting names.")
errorNames(apiKey, uuid)
else:
pass
2023-01-05 17:15:12 -05:00
2022-11-03 21:06:46 -04:00
def errorNames(apiKey, uuid):
url = f"https://api.northpass.com/v2/people/{uuid}"
2023-01-05 17:15:12 -05:00
headers = {"accept": "application/json", "X-Api-Key": apiKey}
2022-11-03 21:06:46 -04:00
response = requests.get(url, headers=headers)
response = response.json()
errorList = []
names = response["data"]["attributes"]["full_name"]
errorList.append(names)
fullNames(errorList)
2022-11-02 17:03:56 -04:00
2023-01-05 17:15:12 -05:00
2022-11-03 21:06:46 -04:00
def fullNames(errorList):
print(errorList)
2022-11-02 17:03:56 -04:00
2023-01-05 17:15:12 -05:00
2022-11-03 21:06:46 -04:00
if __name__ == "__main__":
createGroup(groupName, apiKey)