|
4 | 4 |
|
5 | 5 | import os |
6 | 6 | import platform |
7 | | -from typing import Optional, Callable |
| 7 | +from typing import Callable, Optional |
| 8 | + |
8 | 9 | import httpx |
9 | 10 |
|
10 | 11 | from src.config.defaults import BZM_APIM_BASE_URL |
11 | 12 | from src.config.token import BzmApimToken |
12 | 13 | from src.config.version import __version__ |
13 | 14 | from src.models import BaseResult |
14 | 15 |
|
15 | | - |
16 | | -so = platform.system() # "Windows", "Linux", "Darwin" |
| 16 | +so = platform.system() # "Windows", "Linux", "Darwin" |
17 | 17 | version = platform.version() # kernel / build version |
18 | 18 | release = platform.release() # ex. "10", "5.15.0-76-generic" |
19 | 19 | machine = platform.machine() # ex. "x86_64", "AMD64", "arm64" |
20 | 20 |
|
21 | 21 | ua_part = f"{so} {release}; {machine}" |
22 | 22 |
|
23 | 23 |
|
24 | | -async def api_request(token: Optional[BzmApimToken], method: str, endpoint: str, |
25 | | - result_formatter: Callable = None, |
26 | | - result_formatter_params: Optional[dict] = None, |
27 | | - **kwargs) -> BaseResult: |
| 24 | +async def api_request( |
| 25 | + token: Optional[BzmApimToken], |
| 26 | + method: str, |
| 27 | + endpoint: str, |
| 28 | + result_formatter: Callable = None, |
| 29 | + result_formatter_params: Optional[dict] = None, |
| 30 | + **kwargs, |
| 31 | +) -> BaseResult: |
28 | 32 | """ |
29 | 33 | Make an authenticated request to the BlazeMeter APIM APIs. |
30 | 34 | Handles authentication errors gracefully. |
31 | 35 | """ |
32 | 36 | if not token: |
33 | 37 | return BaseResult( |
34 | 38 | error="No API token. Set BZM_API_TEST_TOKEN env var with the token or BZM_API_TEST_TOKEN_FILE " |
35 | | - "with the file path or BZM_API_TEST_TOKEN secrets in docker catalog configuration.") |
| 39 | + "with the file path or BZM_API_TEST_TOKEN secrets in docker catalog configuration." |
| 40 | + ) |
36 | 41 |
|
37 | 42 | headers = kwargs.pop("headers", {}) |
38 | 43 | headers["Authorization"] = f"Bearer {token}" |
39 | 44 | headers["User-Agent"] = f"bzm-apitest-mcp/{__version__} ({ua_part})" |
40 | 45 |
|
41 | | - timeout = httpx.Timeout( |
42 | | - connect=15.0, |
43 | | - read=60.0, |
44 | | - write=15.0, |
45 | | - pool=60.0 |
46 | | - ) |
| 46 | + timeout = httpx.Timeout(connect=15.0, read=60.0, write=15.0, pool=60.0) |
47 | 47 |
|
48 | | - async with (httpx.AsyncClient(base_url=BZM_APIM_BASE_URL, timeout=timeout) as client): |
| 48 | + async with httpx.AsyncClient(base_url=BZM_APIM_BASE_URL, timeout=timeout) as client: |
49 | 49 | try: |
50 | 50 | resp = await client.request(method, endpoint, headers=headers, **kwargs) |
51 | 51 | resp.raise_for_status() |
52 | 52 | response_dict = resp.json() |
53 | 53 | result = response_dict.get("data", []) |
54 | 54 | default_total = 0 |
55 | | - if not isinstance( |
56 | | - result, list): # Generalize result always as a list |
| 55 | + if not isinstance(result, list): # Generalize result always as a list |
57 | 56 | result = [result] |
58 | 57 | default_total = 1 |
59 | 58 | elif "total" not in response_dict: |
60 | 59 | default_total = len(result) |
61 | | - final_result = result_formatter( |
62 | | - result, result_formatter_params) if result_formatter else result |
| 60 | + final_result = result_formatter(result, result_formatter_params) if result_formatter else result |
63 | 61 | return BaseResult( |
64 | 62 | result=final_result, |
65 | 63 | error=response_dict.get("error", None), |
66 | 64 | total=response_dict.get("total", default_total), |
67 | | - has_more=response_dict.get("total", 0) - ( |
68 | | - response_dict.get("skip", 0) + response_dict.get("limit", 0)) > 0, |
69 | | - hint=kwargs.get("hint", []) |
| 65 | + has_more=response_dict.get("total", 0) |
| 66 | + - (response_dict.get("skip", 0) + response_dict.get("limit", 0)) |
| 67 | + > 0, |
| 68 | + hint=kwargs.get("hint", []), |
70 | 69 | ) |
71 | 70 | except httpx.HTTPStatusError as e: |
72 | 71 | if e.response.status_code == 403: |
73 | 72 | return BaseResult( |
74 | | - error=e.response.json().get("error", {}).get('message', 'Invalid Credentials') |
| 73 | + error=e.response.json().get("error", {}).get("message", "Invalid Credentials") |
75 | 74 | ) |
76 | 75 | elif e.response.status_code == 401: |
77 | | - return BaseResult( |
78 | | - error="Unauthorized to perform this action" |
79 | | - ) |
| 76 | + return BaseResult(error="Unauthorized to perform this action") |
80 | 77 | raise |
0 commit comments