|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# This sample demonstrates how to use the GET analytics/custom_actions/scripts |
| 3 | +# endpoint in the REST API. |
| 4 | + |
| 5 | +# For this scenario to work there must already be custom action scripts on the |
| 6 | +# system the sample is being run against. |
| 7 | +# The scenario demonstrates the following actions: |
| 8 | +# - How to get all custom action scripts |
| 9 | +# - How to retrieve specific custom action scripts |
| 10 | + |
| 11 | +# To view a list of the endpoints with the parameters they accept, you can view |
| 12 | +# the REST API interactive help page on your deployment at |
| 13 | +# https://<hostname>/api_doc. You can also retrieve a list of available |
| 14 | +# endpoints with the REST API itself at the /api/help/capabilities endpoint. |
| 15 | + |
| 16 | +import sys |
| 17 | +import os |
| 18 | +import json |
| 19 | + |
| 20 | +import importlib |
| 21 | +sys.path.append(os.path.realpath('../modules')) |
| 22 | +client_module = importlib.import_module('RestApiClient') |
| 23 | +SampleUtilities = importlib.import_module('SampleUtilities') |
| 24 | + |
| 25 | + |
| 26 | +def main(): |
| 27 | + # Create our client. |
| 28 | + restClient = client_module.RestApiClient(version='5.0') |
| 29 | + # Endpoint used in this sample. |
| 30 | + scripts_endpoint = 'analytics/custom_actions/scripts' |
| 31 | + # Using the analytics/custom_actions/scripts endpoint with GET request. |
| 32 | + SampleUtilities.pretty_print_request(restClient, |
| 33 | + scripts_endpoint, |
| 34 | + 'GET') |
| 35 | + # Calling endpoint to get all available scripts. |
| 36 | + response = restClient.call_api(scripts_endpoint, 'GET') |
| 37 | + |
| 38 | + # Checking for a successful response code. |
| 39 | + if (response.code != 200): |
| 40 | + print('Failed to retrieve custom action script list') |
| 41 | + SampleUtilities.pretty_print_response(response) |
| 42 | + sys.exit(1) |
| 43 | + # Extract the script metadata from the response. |
| 44 | + response_body = json.loads(response.read().decode('utf-8')) |
| 45 | + total_retrieved = len(response_body) |
| 46 | + # Print total received |
| 47 | + print(str(total_retrieved) + ' custom action scripts were retrieved') |
| 48 | + |
| 49 | + script_id = None |
| 50 | + |
| 51 | + if (total_retrieved > 0): |
| 52 | + |
| 53 | + print('Retrieved scripts:\n') |
| 54 | + format_str = 'id=[{0}], file name=[{1}]' |
| 55 | + # Iterating over each script received. |
| 56 | + for script in response_body: |
| 57 | + |
| 58 | + retrieved_id = str(script['id']) |
| 59 | + file_name = str(script['file_name']) |
| 60 | + |
| 61 | + print(format_str.format(retrieved_id, file_name)) |
| 62 | + # Setting for use later with the /scripts/{id} |
| 63 | + # endpoint. |
| 64 | + script_id = script['id'] |
| 65 | + |
| 66 | + print() |
| 67 | + |
| 68 | + # Demonstrating getting specific scripts using |
| 69 | + # an id retrieved from the previous call. |
| 70 | + if (script_id is not None): |
| 71 | + scripts_endpoint += '/' + str(script_id) |
| 72 | + SampleUtilities.pretty_print_request(restClient, |
| 73 | + scripts_endpoint, |
| 74 | + 'GET') |
| 75 | + response = restClient.call_api(scripts_endpoint, |
| 76 | + 'GET') |
| 77 | + # Checking for a successful response code. |
| 78 | + if (response.code != 200): |
| 79 | + print('Failed to retrieve custom action script with id ' + |
| 80 | + script_id) |
| 81 | + SampleUtilities.pretty_print_response(response) |
| 82 | + sys.exit(1) |
| 83 | + # Extracting script from response. |
| 84 | + response_body = json.loads(response.read().decode('utf-8')) |
| 85 | + received_id = response_body['id'] |
| 86 | + print('Successfully retrieved custom action script with id ' + |
| 87 | + str(received_id)) |
| 88 | + |
| 89 | + else: |
| 90 | + print('No available scripts found to demonstrate' |
| 91 | + 'the GET /scripts/{id} endpoint.') |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments