diff --git a/src/groundlight/experimental_api.py b/src/groundlight/experimental_api.py index c464e299..73511dcc 100644 --- a/src/groundlight/experimental_api.py +++ b/src/groundlight/experimental_api.py @@ -47,8 +47,10 @@ Rule, WebhookAction, ) +from urllib3.response import HTTPResponse from groundlight.images import parse_supported_image_types +from groundlight.internalapi import _generate_request_id from groundlight.optional_imports import Image, np from .client import DEFAULT_REQUEST_TIMEOUT, Groundlight, GroundlightClientError, logger @@ -1046,3 +1048,53 @@ def get_detector_metrics(self, detector: Union[str, Detector]) -> dict: detector = detector.id obj = self.detectors_api.get_detector_metrics(detector) return obj.to_dict() + + def get_raw_headers(self) -> dict: + """ + Get the raw headers for the current API client + + :return: a dictionary containing the raw headers + """ + headers = {} + # see generated/groundlight_openapi_client/api_client.py update_params_for_auth + headers["x-api-token"] = self.api_client.configuration.api_key["ApiToken"] + # We generate a unique request ID client-side for each request + headers["X-Request-Id"] = _generate_request_id() + headers["User-Agent"] = self.api_client.default_headers["User-Agent"] + headers["Accept"] = "application/json" + return headers + + def make_generic_api_request( # noqa: PLR0913 # pylint: disable=too-many-arguments + self, + *, + endpoint: str, + method: str, + headers: Union[dict, None] = None, + body: Union[dict, None] = None, + files=None, + ) -> HTTPResponse: + """ + Make a generic API request to the specified endpoint, utilizing many of the provided tools + from the generated api client + + :param endpoint: the endpoint to send the request to - the url path appended after the + endpoint including a / at the beginging + :param method: the HTTP method to use + :param body: the request body + + :return: a dictionary containing the response + """ + # HEADERS MUST BE THE 4TH ARGUMENT, 0 INDEXED + if not headers: + headers = self.get_raw_headers() + return self.api_client.call_api( + endpoint, + method, + None, # Path Params + None, # Query params + headers, # header params + body=body, # body + files=files, # files + auth_settings=["ApiToken"], + _preload_content=False, # This returns the urllib3 response rather than trying any type of processing + ) diff --git a/test/unit/test_customizable_request.py b/test/unit/test_customizable_request.py new file mode 100644 index 00000000..9f62d73c --- /dev/null +++ b/test/unit/test_customizable_request.py @@ -0,0 +1,12 @@ +from datetime import datetime + +from groundlight import ExperimentalApi + +gl = ExperimentalApi() + + +def test_invalid_endpoint_config(): + print(gl.make_generic_api_request(endpoint="/v1/me", method="GET")) + print(gl.make_generic_api_request(endpoint="/v1/detectors", method="GET")) + name = f"Test {datetime.utcnow()}" + print(gl.make_generic_api_request(endpoint="/v1/detector-groups", method="POST", body={"name": name}))