-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_client_with_simple_locks.py
More file actions
56 lines (47 loc) · 1.75 KB
/
Copy pathapi_client_with_simple_locks.py
File metadata and controls
56 lines (47 loc) · 1.75 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import asyncio
from argparse import ArgumentParser
from typing import List
from huntflow_api_client import HuntflowAPI
from huntflow_api_client.tokens.locker import AsyncioLockLocker
from huntflow_api_client.tokens.proxy import HuntflowTokenProxy
from huntflow_api_client.tokens.storage import HuntflowTokenFileStorage
async def get_and_print_org_info(api_client: HuntflowAPI):
response = await api_client.request("GET", "/accounts")
print(response.json())
async def main(concurrent_client_count: int, token_filename: str, base_url: str):
token_storage = HuntflowTokenFileStorage(token_filename)
locker = AsyncioLockLocker()
api_clients: List[HuntflowAPI] = []
for _ in range(concurrent_client_count):
token_proxy = HuntflowTokenProxy(token_storage, locker)
client = HuntflowAPI(
base_url,
token_proxy=token_proxy,
auto_refresh_tokens=True,
)
api_clients.append(client)
calls = [get_and_print_org_info(client) for client in api_clients]
await asyncio.gather(*calls)
def parse_args():
parser = ArgumentParser()
parser.add_argument(
"--count",
type=int,
default=3,
help="Number of concurrent requests",
)
parser.add_argument("--url", type=str, help="https://<api domain>")
parser.add_argument(
"--token-file",
type=str,
help=(
"Path to json file. File must include 'access_token' and 'refresh_token' keys."
" File will be rewritten when token is refreshed."
),
)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
loop = asyncio.get_event_loop()
loop.run_until_complete(main(args.count, args.token_file, args.url))