Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Description for the python script

...

Info

NOTE! The provided scripts are fully experimental and built a single use case scenario in mind and are not meant to be used in a production environment!

There is no guarantee or warranty that these scripts will work as intended in a customer specific environment.

Running this script is only recommended in a controlled, non-production environment.

Python

Requirements:

  • Python
  • Requests library

This script will export multiple pages of reports one report at a time printing the results and giving a summary after exporting the last page/report.

The endpoint URL, API key, export filter and other parameters are provided as part of the script and can be modified.

Code Block
languagepy
titleExample python script to export multiple pages of reports automaticallya single report at a time
linenumberstrue
import requests

def execute_curl_request(cursor=None):
    url = 'https://api.eu-west-1.blancco.cloud/v1/report/export'
    headers = {
        'Content-Type': 'application/json',
        'X-BLANCCO-API-KEY': 'YOUR-API-KEY'
    }
    data = {
        "filter": {
            "date": {
                "gte": "2023-07-01T00:00:00Z"
            }
        },
        "format": "XML",
        "container": "NONE",
        "size": 1
    }

    if cursor:
        url += f"?cursor={cursor}"

    response = requests.post(url, headers=headers, json=data)
    return response

def check_and_print_response(response):
    if response.status_code == 200:
        #print("\nResponse:")
        print(response.text)

        cursor_header = response.headers.get('x-blancco-cursor')
        return cursor_header, True
    else:
        print("Request failed with status code:", response.status_code)
        return None, False

if __name__ == "__main__":
    cursor = None
    success_count = 0
    failure_count = 0

    while True:
        response = execute_curl_request(cursor)

        if success_count == 0:
            print("\n--------------------------------------------------")

        new_cursor, success = check_and_print_response(response)

        if success:
            success_count += 1
        else:
            failure_count += 1

        if new_cursor:
            cursor = new_cursor
        else:
            #print("No more reports available")
            break

    print("\n--------------------------------------------------")
    print("All matching reports exported. Summary of exported reports:")
    print(f"Successful exports: {success_count}")
    print(f"Failed exports: {failure_count}")