|
11 | 11 | from io import BufferedReader, BytesIO
|
12 | 12 | from pathlib import Path
|
13 | 13 | from typing import Any, Dict, List, Optional, Tuple, Union
|
| 14 | +from urllib3.response import HTTPResponse |
14 | 15 |
|
15 | 16 | import requests
|
| 17 | +from groundlight.internalapi import _generate_request_id |
16 | 18 | from groundlight_openapi_client.api.actions_api import ActionsApi
|
17 | 19 | from groundlight_openapi_client.api.detector_groups_api import DetectorGroupsApi
|
18 | 20 | from groundlight_openapi_client.api.detector_reset_api import DetectorResetApi
|
@@ -1046,3 +1048,67 @@ def get_detector_metrics(self, detector: Union[str, Detector]) -> dict:
|
1046 | 1048 | detector = detector.id
|
1047 | 1049 | obj = self.detectors_api.get_detector_metrics(detector)
|
1048 | 1050 | return obj.to_dict()
|
| 1051 | + |
| 1052 | + def get_raw_headers(self) -> dict: |
| 1053 | + """ |
| 1054 | + Get the raw headers for the current API client |
| 1055 | +
|
| 1056 | + :return: a dictionary containing the raw headers |
| 1057 | + """ |
| 1058 | + headers = {} |
| 1059 | + # see generated/groundlight_openapi_client/api_client.py update_params_for_auth |
| 1060 | + headers["x-api-token"] = self.api_client.configuration.api_key["ApiToken"] |
| 1061 | + # We generate a unique request ID client-side for each request |
| 1062 | + headers["X-Request-Id"] = _generate_request_id() |
| 1063 | + headers["User-Agent"] = self.api_client.default_headers["User-Agent"] |
| 1064 | + headers['Accept'] = 'application/json' |
| 1065 | + return headers |
| 1066 | + |
| 1067 | + def make_raw_rest_request(self, method: str, endpoint: str, body: Union[dict, None] = None) -> dict: |
| 1068 | + """ |
| 1069 | + Make a raw REST request to the specified URL |
| 1070 | +
|
| 1071 | + :param method: the HTTP method to use |
| 1072 | + :param endpoint: the endpoint to send the request to - the url path appended after the |
| 1073 | + endpoint including a / at the beginging |
| 1074 | + :param body: the request body |
| 1075 | +
|
| 1076 | + :return: a dictionary containing the raw response |
| 1077 | + """ |
| 1078 | + headers = self.get_raw_headers() |
| 1079 | + url = f"{self.api_client.configuration.host}{endpoint}" |
| 1080 | + response = requests.request(method, url, headers=headers, json=body) |
| 1081 | + return response.json() |
| 1082 | + |
| 1083 | + def make_generic_api_request( |
| 1084 | + self, |
| 1085 | + endpoint: str, |
| 1086 | + method: str, |
| 1087 | + headers: dict = None, |
| 1088 | + body: Union[dict, None] = None, |
| 1089 | + files = None, |
| 1090 | + ) -> HTTPResponse: |
| 1091 | + """ |
| 1092 | + Make a generic API request to the specified endpoint, utilizing many of the provided tools from the generated api client |
| 1093 | +
|
| 1094 | + :param endpoint: the endpoint to send the request to - the url path appended after the |
| 1095 | + endpoint including a / at the beginging |
| 1096 | + :param method: the HTTP method to use |
| 1097 | + :param body: the request body |
| 1098 | +
|
| 1099 | + :return: a dictionary containing the response |
| 1100 | + """ |
| 1101 | + # HEADERS MUST BE THE 4TH ARGUMENT, 0 INDEXED |
| 1102 | + if not headers: |
| 1103 | + headers = self.get_raw_headers() |
| 1104 | + return self.api_client.call_api( |
| 1105 | + endpoint, |
| 1106 | + method, |
| 1107 | + None, # Path Params |
| 1108 | + None, # Query params |
| 1109 | + headers, # header params |
| 1110 | + body = body, # body |
| 1111 | + files = files, # files |
| 1112 | + auth_settings = ['ApiToken'], |
| 1113 | + _preload_content = False, # This returns the urllib3 response rather than trying any type of processing |
| 1114 | + ) |
0 commit comments