File size: 861 Bytes
394aac6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from __future__ import annotations

import json
from pathlib import Path

import mock_api
from polling_policy import wait_for_next_poll
from report_client import create_report_task, fetch_report_result, fetch_report_status
from status_gate import is_result_ready

REQUEST_FILE = Path(__file__).with_name('request.json')


def run(output_path: str | Path = 'report_output.json'):
    month = json.loads(REQUEST_FILE.read_text(encoding='utf-8'))['month']
    task = create_report_task(mock_api, month)
    while True:
        status = fetch_report_status(mock_api, task['task_id'])
        if is_result_ready(status):
            break
        wait_for_next_poll()
    report = fetch_report_result(mock_api, task['task_id'])
    Path(output_path).write_text(json.dumps(report, indent=2), encoding='utf-8')
    return report


if __name__ == '__main__':
    run()