|
| 1 | +from docusign_monitor import DataSetApi |
| 2 | +from flask import session, json |
| 3 | + |
| 4 | +from app.monitor.utils import create_monitor_api_client |
| 5 | + |
| 6 | + |
| 7 | +class Eg001Controller: |
| 8 | + @staticmethod |
| 9 | + def get_args(): |
| 10 | + """Get required session and request arguments""" |
| 11 | + return { |
| 12 | + "account_id": session["ds_account_id"], # Represents your {ACCOUNT_ID} |
| 13 | + "access_token": session["ds_access_token"], # Represents your {ACCESS_TOKEN} |
| 14 | + } |
| 15 | + |
| 16 | + @staticmethod |
| 17 | + def worker(args): |
| 18 | + """ |
| 19 | + 1. Create an API client with headers |
| 20 | + 2. Get your monitor data via SDK |
| 21 | + 3. Use while loop for collecting rest records |
| 22 | + """ |
| 23 | + |
| 24 | + # Step 1. Create an API client with headers |
| 25 | + api_client = create_monitor_api_client( |
| 26 | + access_token=args["access_token"] |
| 27 | + ) |
| 28 | + |
| 29 | + # Step 1 end |
| 30 | + |
| 31 | + # Step 2. Get your monitor data |
| 32 | + dataset_api = DataSetApi(api_client=api_client) |
| 33 | + response = dataset_api.get_stream_for_dataset( |
| 34 | + data_set_name="monitor", |
| 35 | + version="2.0", |
| 36 | + _preload_content=False, |
| 37 | + ) |
| 38 | + response = json.loads(response.data) |
| 39 | + |
| 40 | + # Step 2 end |
| 41 | + |
| 42 | + # Step 3. Use while loop for collecting rest records |
| 43 | + |
| 44 | + result = response["data"] |
| 45 | + cursor = response["endCursor"] |
| 46 | + |
| 47 | + # If the endCursor from the response is the same as the one |
| 48 | + # that you already have, |
| 49 | + # it means that you have reached the end of the records |
| 50 | + |
| 51 | + while True: |
| 52 | + response = dataset_api.get_stream_for_dataset( |
| 53 | + data_set_name="monitor", |
| 54 | + version="2.0", |
| 55 | + _preload_content=False, |
| 56 | + cursor=cursor |
| 57 | + ) |
| 58 | + |
| 59 | + response = json.loads(response.data) |
| 60 | + |
| 61 | + if response["endCursor"] == cursor: |
| 62 | + break |
| 63 | + |
| 64 | + result.extend(response["data"]) |
| 65 | + cursor = response["endCursor"] |
| 66 | + |
| 67 | + # Step 3 end |
| 68 | + |
| 69 | + return result |
0 commit comments