Repository navigation
[CHANGE] Reuse the same request session on same instance #110
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
base: master
Are you sure you want to change the base?
Changes from all commits
57069db
a26482e
ae6487e
f2b5117
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,12 @@ | |||||||||||||
|
|
||||||||||||||
| class DataApi(object): | ||||||||||||||
|
|
||||||||||||||
| def __init__(self) -> None: | ||||||||||||||
| self._session = requests.Session() | ||||||||||||||
|
Comment on lines
+15
to
+16
Contributor
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. ❗️ Pressing: Adding
Contributor
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. Fair point, if _session is not an attr when we try to do the request, we can create the request. Fixed on f2b5117 |
||||||||||||||
|
|
||||||||||||||
| def __del__(self) -> None: | ||||||||||||||
| self.close() | ||||||||||||||
|
|
||||||||||||||
| def _extract_consumer(self, security_packet: Dict[str, str]) -> str: | ||||||||||||||
| """ | ||||||||||||||
| Extract the consumer key from the security packet. | ||||||||||||||
|
|
@@ -55,6 +61,12 @@ def _derive_action(self, endpoint: str, action: str) -> str: | |||||||||||||
|
|
||||||||||||||
| return f"{action}_{path}" | ||||||||||||||
|
|
||||||||||||||
| def close(self) -> None: | ||||||||||||||
|
Copilot marked this conversation as resolved.
|
||||||||||||||
| """Close pooled connections; later requests on this client remain supported.""" | ||||||||||||||
| session = getattr(self, '_session', None) | ||||||||||||||
| if session is not None: | ||||||||||||||
| session.close() | ||||||||||||||
|
Comment on lines
+66
to
+68
Contributor
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. ❓ Question: Why allow later requests? Would we not just fully drop it and have an actual blank slate?
Suggested change
Contributor
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. 2 reasons:
it will be functionally equivalent, so happy to change if you want |
||||||||||||||
|
|
||||||||||||||
| def request(self, endpoint: str, security_packet: Dict[str, str], | ||||||||||||||
| secret: str, request_packet:Dict[str, Any] = {}, action: str = 'get') -> Response: | ||||||||||||||
| """ | ||||||||||||||
|
|
@@ -91,8 +103,12 @@ def request(self, endpoint: str, security_packet: Dict[str, str], | |||||||||||||
| 'X-Learnosity-Action': derived_action, | ||||||||||||||
| 'X-Learnosity-SDK': f'Python:{sdk_version}' | ||||||||||||||
| } | ||||||||||||||
| # Lazily create the session so subclasses that override __init__ | ||||||||||||||
| # without calling super().__init__() still benefit from connection reuse. | ||||||||||||||
| if getattr(self, '_session', None) is None: | ||||||||||||||
| self._session = requests.Session() | ||||||||||||||
|
|
||||||||||||||
| return requests.post(endpoint, data=init.generate(), headers=headers) | ||||||||||||||
| return self._session.post(endpoint, data=init.generate(), headers=headers) | ||||||||||||||
|
|
||||||||||||||
| def results_iter(self, endpoint: str, security_packet: Dict[str, str], | ||||||||||||||
| secret: str, request_packet: Dict[str, Any] = {}, | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.