-
Notifications
You must be signed in to change notification settings - Fork 11
Add request timeouts #18
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
Open
massy-o
wants to merge
1
commit into
bananaml:main
Choose a base branch
from
massy-o:codex/add-request-timeouts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+78
−10
Open
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -14,20 +14,23 @@ def __init__(self, message = "" , res: requests.Response = None): | |
|
|
||
| class Client(): | ||
| "The Banana client class is for interacting with a specific project on Banana." | ||
| def __init__(self, api_key, url, verbosity = "DEBUG"): | ||
| def __init__(self, api_key, url, verbosity = "DEBUG", request_timeout = 300): | ||
| self.api_key = api_key | ||
| self.url = url | ||
| self.verbosity = verbosity | ||
| self.request_timeout = request_timeout | ||
|
|
||
| def warmup(self) -> Tuple[dict, dict]: | ||
| "Warm up the Potassium server" | ||
| return self.call("/_k/warmup", json={}, headers={}, retry=False) | ||
|
|
||
| "Call a route on the Banana server with a POST request" | ||
| def call(self, route: str, json: dict = {}, headers: dict = {}, retry=True, retry_timeout = 300) -> Tuple[dict, dict]: | ||
| headers["Content-Type"] = "application/json" | ||
| headers['X-BANANA-API-KEY'] = self.api_key | ||
| headers['X-BANANA-REQUEST-ID'] = str(uuid4()) # we use the same uuid to track all retries | ||
| def call(self, route: str, json: dict = {}, headers: dict = {}, retry=True, retry_timeout = 300, request_timeout = None) -> Tuple[dict, dict]: | ||
| request_timeout = self.request_timeout if request_timeout is None else request_timeout | ||
| request_headers = dict(headers) | ||
|
Author
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. Self-review: Copying the caller-provided headers keeps the timeout change from also mutating user-owned dictionaries when Banana auth headers are injected. |
||
| request_headers["Content-Type"] = "application/json" | ||
| request_headers['X-BANANA-API-KEY'] = self.api_key | ||
| request_headers['X-BANANA-REQUEST-ID'] = str(uuid4()) # we use the same uuid to track all retries | ||
|
|
||
| endpoint = self.url.rstrip("/") + "/" + route.lstrip("/") | ||
|
|
||
|
|
@@ -47,7 +50,7 @@ def call(self, route: str, json: dict = {}, headers: dict = {}, retry=True, retr | |
| print("Retrying...") | ||
|
|
||
| backoff_interval = min(backoff_interval*2, 3) | ||
| res = requests.post(endpoint, json=json, headers=headers) | ||
| res = requests.post(endpoint, json=json, headers=request_headers, timeout=request_timeout) | ||
|
|
||
| if self.verbosity == "DEBUG": | ||
| if res.status_code != 200: | ||
|
|
||
This file contains hidden or 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,64 @@ | ||
| import unittest | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| from banana_dev import API, Client | ||
|
|
||
|
|
||
| class RequestTimeoutTests(unittest.TestCase): | ||
| def test_client_call_passes_timeout_and_preserves_headers(self): | ||
| response = Mock(status_code=200, headers={"x-test": "ok"}) | ||
| response.json.return_value = {"ok": True} | ||
| headers = {"X-CALLER": "present"} | ||
|
|
||
| with patch("banana_dev.client.requests.post", return_value=response) as post: | ||
| result, meta = Client("secret", "https://example.test", request_timeout=12).call( | ||
| "/run", | ||
| json={"input": "value"}, | ||
| headers=headers, | ||
| retry=False, | ||
| ) | ||
|
|
||
| self.assertEqual(result, {"ok": True}) | ||
| self.assertEqual(meta, {"headers": {"x-test": "ok"}}) | ||
| self.assertEqual(headers, {"X-CALLER": "present"}) | ||
| self.assertEqual(post.call_args.kwargs["timeout"], 12) | ||
| self.assertEqual(post.call_args.kwargs["headers"]["X-BANANA-API-KEY"], "secret") | ||
| self.assertEqual(post.call_args.kwargs["headers"]["X-CALLER"], "present") | ||
|
|
||
| def test_client_call_allows_per_call_timeout_override(self): | ||
| response = Mock(status_code=200, headers={}) | ||
| response.json.return_value = {"ok": True} | ||
|
|
||
| with patch("banana_dev.client.requests.post", return_value=response) as post: | ||
| Client("secret", "https://example.test", request_timeout=12).call( | ||
| "/run", | ||
| retry=False, | ||
| request_timeout=3, | ||
| ) | ||
|
|
||
| self.assertEqual(post.call_args.kwargs["timeout"], 3) | ||
|
|
||
| def test_api_methods_pass_timeout(self): | ||
| response = Mock(status_code=200) | ||
| response.json.return_value = {"results": []} | ||
| api = API(" secret ", request_timeout=7) | ||
|
|
||
| with patch("banana_dev.api.requests.post", return_value=response) as post: | ||
| api._API__call("POST", "projects", {"name": "example"}) | ||
|
|
||
| with patch("banana_dev.api.requests.put", return_value=response) as put: | ||
| api._API__call("PUT", "projects/example", {"name": "renamed"}) | ||
|
|
||
| with patch("banana_dev.api.requests.get", return_value=response) as get: | ||
| result, status = api.list_projects() | ||
|
|
||
| self.assertEqual(result, {"results": []}) | ||
| self.assertEqual(status, 200) | ||
| self.assertEqual(post.call_args.kwargs["timeout"], 7) | ||
| self.assertEqual(put.call_args.kwargs["timeout"], 7) | ||
| self.assertEqual(get.call_args.kwargs["timeout"], 7) | ||
| self.assertEqual(get.call_args.kwargs["headers"]["X-BANANA-API-KEY"], "secret") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.
Self-review: I kept the default at 300 seconds to line up with the existing retry_timeout and 5-minute timeout behavior, while still exposing request_timeout for callers that need a stricter bound.