Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generic requests using the api client #330

Merged
merged 6 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/groundlight/experimental_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self,
self,
*,

We could require the arguments to be named, not positionally-ordered. Feels like a good practice for these methods with tons of arguments, although this one is mercifully more succinct than the openapi provided ones.

Is this the right syntax for that?

*,
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
)
12 changes: 12 additions & 0 deletions test/unit/test_customizable_request.py
Original file line number Diff line number Diff line change
@@ -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}))
Loading