Last active
January 24, 2024 17:21
-
-
Save rterakedis/437614bb6de6d5e74b9128de0de20c1f to your computer and use it in GitHub Desktop.
Gainsight PX API Paging Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example Gainsight Paging API Call | |
############################################################################## | |
# Reference API documentation: | |
# https://gainsightpx.docs.apiary.io/#reference/surveyresponse | |
# | |
# Use the returned scrollId to make a request for the next page of results (i.e. /users?pageSize=100&scrollId=XXXXXXX) | |
# Scroll until the returned result list is less than the requested size. Do not depend on the scrollId becoming null, | |
# in some cases it does not be null even though the last page of results is returned. | |
############################################################################## | |
import json, requests, sys | |
from datetime import datetime, timedelta, date | |
############################################################################## | |
## CONSTANTS | |
############################################################################## | |
# BaseURL for API calls | |
BASE_URL = "BASE-URL-HERE" | |
# API Key Name | |
API_KEY_NAME = "X-APTRINSIC-API-KEY" | |
# API Key Value | |
API_KEY_VALUE = "GUID-HERE" | |
# API Page Size | |
API_PAGE_SIZE = 100 | |
# Survey Engagement ID | |
ENGAGEMENT_ID = "ENGAGEMENT-ID-HERE" | |
############################################################################## | |
## HELPER FUNCTION | |
############################################################################## | |
# create a function to get the list of survey responses for a specific survey | |
def get_surveyresponsesapi(engagementId, scrollId, datefiltertimestamp): | |
if scrollId == "": | |
# Set the request url for the first page of results | |
if datefiltertimestamp == "": | |
url = BASE_URL + "/v1/survey/responses?filter=engagementId=="+ engagementId + "&pageSize=" + str(API_PAGE_SIZE) + "&sort=-date" | |
else: | |
url = BASE_URL + "/v1/survey/responses?filter=engagementId=="+ engagementId + ";date>" + str(datefiltertimestamp) + "&pageSize=" + str(API_PAGE_SIZE) + "&sort=-date" | |
else: | |
# Set the request url for the subsequent pages of results | |
url = BASE_URL + "/v1/survey/responses?filter=engagementId=="+ engagementId + "&pageSize=" + str(API_PAGE_SIZE) + "&sort=-date" + "&scrollId=" + scrollId | |
payload={} | |
headers = { | |
'Accept': 'application/json', | |
API_KEY_NAME: API_KEY_VALUE | |
} | |
# Make the API call | |
return requests.request("GET", url, headers=headers, data=payload) | |
############################################################################## | |
## STEP 2: Get the responses for each survey in the list and write them to a CSV file | |
############################################################################## | |
def get_surveyresponses(): | |
# prompt user to pick a time period | |
print("Please enter last number of days to filter by:") | |
print("0: No Date Filter") | |
print("#: Last # days") | |
time_period = int(input("Enter a number: ")) | |
print("You selected: " + str(time_period)) | |
# set the Date Filter based on the user's input | |
if time_period == 0: | |
# no time filter | |
datefilter = "" | |
print("No Date Filter") | |
epochms = "" | |
else: | |
# set the date filter based on the user's input | |
datefilter = datetime.combine(date.today(),datetime.min.time()) - timedelta(days = switch_time_filter(time_period)) | |
print("Date Filter: " + str(datefilter)) | |
# convert to epoch time in milliseconds | |
epochms = int(datetime.timestamp(datefilter)) * 1000 | |
# create a counter for the number of survey responses returned | |
totalResponseCount = 1 | |
currentresponsecount = 0 | |
currentScrollId = "" | |
# Does currentresponsecount < totalResponseCount? If so, we need to scroll through the results | |
# Good Reference: https://jonathansoma.com/lede/foundations-2018/classes/apis/multiple-pages-of-data-from-apis/ | |
while currentresponsecount < totalResponseCount: | |
print("Parsing survey: " + ENGAGEMENT_ID + " at scrollId: " + currentScrollId) | |
print("Current Response Count: " + str(currentresponsecount)) | |
print("Total Response Count: " + str(totalResponseCount)) | |
# Make the API call | |
surveyResponses = get_surveyresponsesapi(ENGAGEMENT_ID, currentScrollId, epochms) | |
if surveyResponses.status_code == 200: | |
#Successful API call | |
# Parse the response as JSON | |
responseData = surveyResponses.json() | |
# Extract the list of surveys from the JSON response | |
responses = responseData['results'] | |
# Increase the totalResponseCount by the API's count of total responses | |
totalResponseCount = responseData['totalHits'] | |
# count the number of responses returned in this page of API responses | |
currentresponsecount = currentresponsecount + len(responses) | |
# set the scrollId for the next page of results | |
currentScrollId = responseData['scrollId'] | |
# loop through the survey results and ignore the ones that are older than the date filter | |
for response in responses: | |
############################ | |
# DO SOME TYPE OF WORK HERE | |
############################ | |
print("EventID: " + response['eventId'] + ", Contact Me Allowed: " + str(response['contactMeAllowed']) + ", Date: " + str(response['date']) + ", Comment: " + response['comment']) | |
else: | |
print("Error: " + surveyResponses.status_code) | |
############################################################################## | |
## MAIN | |
############################################################################## | |
def main(): | |
# Start the program | |
get_surveyresponses() | |
sys.exit(0) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment