Skip to content

Commit cb3fd95

Browse files
brandon-groundlightAuto-format Bot
and
Auto-format Bot
authored
generic requests using the api client (#330)
Provides two ways to custom construct your own requests to groundlight. There's potential to clean it up when we deprecate the GroundlightApiClient class --------- Co-authored-by: Auto-format Bot <[email protected]>
1 parent a464853 commit cb3fd95

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/groundlight/experimental_api.py

+52
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
Rule,
4848
WebhookAction,
4949
)
50+
from urllib3.response import HTTPResponse
5051

5152
from groundlight.images import parse_supported_image_types
53+
from groundlight.internalapi import _generate_request_id
5254
from groundlight.optional_imports import Image, np
5355

5456
from .client import DEFAULT_REQUEST_TIMEOUT, Groundlight, GroundlightClientError, logger
@@ -1046,3 +1048,53 @@ def get_detector_metrics(self, detector: Union[str, Detector]) -> dict:
10461048
detector = detector.id
10471049
obj = self.detectors_api.get_detector_metrics(detector)
10481050
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+
)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from datetime import datetime
2+
3+
from groundlight import ExperimentalApi
4+
5+
gl = ExperimentalApi()
6+
7+
8+
def test_invalid_endpoint_config():
9+
print(gl.make_generic_api_request(endpoint="/v1/me", method="GET"))
10+
print(gl.make_generic_api_request(endpoint="/v1/detectors", method="GET"))
11+
name = f"Test {datetime.utcnow()}"
12+
print(gl.make_generic_api_request(endpoint="/v1/detector-groups", method="POST", body={"name": name}))

0 commit comments

Comments
 (0)