|
47 | 47 | Rule,
|
48 | 48 | WebhookAction,
|
49 | 49 | )
|
| 50 | +from urllib3.response import HTTPResponse |
50 | 51 |
|
51 | 52 | from groundlight.images import parse_supported_image_types
|
| 53 | +from groundlight.internalapi import _generate_request_id |
52 | 54 | from groundlight.optional_imports import Image, np
|
53 | 55 |
|
54 | 56 | from .client import DEFAULT_REQUEST_TIMEOUT, Groundlight, GroundlightClientError, logger
|
@@ -1046,3 +1048,53 @@ 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_generic_api_request( # noqa: PLR0913 # pylint: disable=too-many-arguments |
| 1068 | + self, |
| 1069 | + *, |
| 1070 | + endpoint: str, |
| 1071 | + method: str, |
| 1072 | + headers: Union[dict, None] = None, |
| 1073 | + body: Union[dict, None] = None, |
| 1074 | + files=None, |
| 1075 | + ) -> HTTPResponse: |
| 1076 | + """ |
| 1077 | + Make a generic API request to the specified endpoint, utilizing many of the provided tools |
| 1078 | + from the generated api client |
| 1079 | +
|
| 1080 | + :param endpoint: the endpoint to send the request to - the url path appended after the |
| 1081 | + endpoint including a / at the beginging |
| 1082 | + :param method: the HTTP method to use |
| 1083 | + :param body: the request body |
| 1084 | +
|
| 1085 | + :return: a dictionary containing the response |
| 1086 | + """ |
| 1087 | + # HEADERS MUST BE THE 4TH ARGUMENT, 0 INDEXED |
| 1088 | + if not headers: |
| 1089 | + headers = self.get_raw_headers() |
| 1090 | + return self.api_client.call_api( |
| 1091 | + endpoint, |
| 1092 | + method, |
| 1093 | + None, # Path Params |
| 1094 | + None, # Query params |
| 1095 | + headers, # header params |
| 1096 | + body=body, # body |
| 1097 | + files=files, # files |
| 1098 | + auth_settings=["ApiToken"], |
| 1099 | + _preload_content=False, # This returns the urllib3 response rather than trying any type of processing |
| 1100 | + ) |
0 commit comments