Fetching stored reports

To fetch stored reports

  1. Authenticate to the cloud platform via the Python shell.

    The following variables should be available now:

    >>> base_url  # the base URL of the API
    '<the data center URL>/api/2'
    >>> auth  # the 'Authorization' header value with the access token
    {'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6ImMwMD...'}
    >>> tenant_id  # the UUID of the tenant to which the token provides access
    'ede9f834-70b3-476c-83d9-736f9f8c7dae'
    
  2. Fetch the UUID of the stored report that you want to download as described in Fetching a usage report. As the result, the following variable with the report UUID should be available:

    >>> report_id
    '9af0d861-be72-4fab-a165-d11ad1c75f56'
    
  3. Send a GET request to the /reports/{report_id}/stored endpoint:

    >>> response = requests.get(f'{base_url}/reports/{report_id}/stored', headers=auth)
    
  4. Check the status code of the response:

    >>> response.status_code
    200
    

    Status code 200 means that the request was successful.

    Note

    A different status code means that an error has occurred. For details of the error, see HTTP status response codes and API error codes.

    Also, the response body contains the items array with stored reports data, formatted as a JSON text. When converted to an object, it will look like this:

    >>> pprint.pprint(response.json())
    {'items': [{'created_at': '2019-08-08T07:36:57+00:00',
                'id': 'af5946c3-7e5b-4480-8835-518efe1c3522',
                'report_format': 'csv',
                'size': 3605,
                'status': 'saved'},
               {'created_at': '2019-08-08T07:36:58+00:00',
                'id': 'a2316878-eae9-40f6-8ffe-07746af127e8',
                'report_format': 'json_v1',
                'size': 18898,
                'status': 'saved'},
               {'created_at': '2019-08-08T07:36:58+00:00',
                'id': '479040dc-e18e-4136-98ce-635c3bc7dbae',
                'report_format': 'csv_v2_0',
                'size': 4945,
                'status': 'saved'},
               {'created_at': '2019-08-08T07:36:58+00:00',
                'id': 'a10bd132-261d-47aa-b7c6-74d39bc2266b',
                'report_format': 'json_v2_0',
                'size': 5892,
                'status': 'saved'}]}
    
  5. Choose the report format, iterate over the items array and store the UUID of the stored report in a variable. As an example, we will get the UUID of the last stored csv report:

    >>> stored_reports = response.json()['items']
    >>> stored_report_id = [stored_report['id'] for stored_report in stored_reports if stored_report['report_format'] == 'csv' and stored_report['status'] == 'saved'][-1]
    >>> stored_report_id
    'af5946c3-7e5b-4480-8835-518efe1c3522'
    
  6. Send a GET request to the /reports/{report_id}/stored/{stored_report_id} endpoint:

    >>> response = requests.get(f'{base_url}/reports/{report_id}/stored/{stored_report_id}', headers=auth)
    
  7. Check the status code of the response:

    >>> response.status_code
    200
    

    Status code 200 means that the response body contains the stored report file in the specified format.

    Note

    A different status code means that an error has occurred. For details of the error, see HTTP status response codes and API error codes.

  8. [Optional] Store the response body in a variable:

    1. For CSV and HTML reports:

      >>> stored_report_data = response.text
      
    2. For JSON reports:

      >>> stored_report_data = response.json()