-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8c39f3f
Two ways to make generic requests using the api client
brandon-groundlight cea8dc3
Automatically reformatting code
bc53846
point to the right test server
brandon-groundlight 673afb0
Merge branch 'generic_requests' of github.com:groundlight/python-sdk …
brandon-groundlight 4a2e0fb
linting
brandon-groundlight 0296c2b
good suggestions
brandon-groundlight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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,68 @@ 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_raw_rest_request(self, method: str, endpoint: str, body: Union[dict, None] = None) -> dict: | ||||||||
""" | ||||||||
Make a raw REST request to the specified URL | ||||||||
|
||||||||
:param method: the HTTP method to use | ||||||||
:param endpoint: the endpoint to send the request to - the url path appended after the | ||||||||
endpoint including a / at the beginging | ||||||||
:param body: the request body | ||||||||
|
||||||||
:return: a dictionary containing the raw response | ||||||||
""" | ||||||||
headers = self.get_raw_headers() | ||||||||
url = f"{self.api_client.configuration.host}{endpoint}" | ||||||||
response = requests.request(method, url, headers=headers, json=body) | ||||||||
return response.json() | ||||||||
|
||||||||
def make_generic_api_request( # noqa: PLR0913 # pylint: disable=too-many-arguments | ||||||||
self, | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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 | ||||||||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("/v1/me", "GET")) | ||
print(gl.make_generic_api_request("/v1/detectors", "GET")) | ||
name = f"Test {datetime.utcnow()}" | ||
print(gl.make_generic_api_request("/v1/detector-groups", "POST", body={"name": name})) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd leave this out unless we have a really good reason to include it. One of these feels helpful, two starts to sound confusing.