Skip to content

Commit 8c39f3f

Browse files
Two ways to make generic requests using the api client
1 parent a464853 commit 8c39f3f

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/groundlight/experimental_api.py

+66
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
from io import BufferedReader, BytesIO
1212
from pathlib import Path
1313
from typing import Any, Dict, List, Optional, Tuple, Union
14+
from urllib3.response import HTTPResponse
1415

1516
import requests
17+
from groundlight.internalapi import _generate_request_id
1618
from groundlight_openapi_client.api.actions_api import ActionsApi
1719
from groundlight_openapi_client.api.detector_groups_api import DetectorGroupsApi
1820
from groundlight_openapi_client.api.detector_reset_api import DetectorResetApi
@@ -1046,3 +1048,67 @@ 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_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+
)
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from groundlight import ExperimentalApi
2+
from datetime import datetime
3+
4+
gl = ExperimentalApi(endpoint="https://api.groundlight.ai/")
5+
6+
def test_invalid_endpoint_config():
7+
print(gl.make_generic_api_request("/v1/me", "GET"))
8+
print(gl.make_generic_api_request("/v1/detectors", "GET"))
9+
name = f"Test {datetime.utcnow()}"
10+
print(gl.make_generic_api_request("/v1/detector-groups", "POST", body={"name": name}))

0 commit comments

Comments
 (0)