-
Notifications
You must be signed in to change notification settings - Fork 56
feat: Allow for request headers to be passed in #446
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
f9b9088
2dbcfeb
1fd2811
8c59e03
3cf2def
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 |
|---|---|---|
|
|
@@ -53,15 +53,20 @@ class _KaleidoTab: | |
| js_logger: _js_logger.JavascriptLogger | ||
| """A log for recording javascript.""" | ||
|
|
||
| def __init__(self, tab): | ||
| def __init__(self, tab, *, headers: dict[str, str] | None = None): | ||
| """ | ||
| Create a new _KaleidoTab. | ||
|
|
||
| Args: | ||
| tab: the choreographer tab to wrap. | ||
|
|
||
| headers (dict[str, str] | None, optional): | ||
| Extra HTTP headers to send with every request made by the | ||
| browser tab. Defaults to None. | ||
|
|
||
| """ | ||
| self.tab = tab | ||
| self._headers = headers | ||
| self.js_logger = _js_logger.JavascriptLogger(self.tab) | ||
|
|
||
| async def navigate(self, url: str | Path = ""): | ||
|
|
@@ -100,6 +105,8 @@ async def navigate(self, url: str | Path = ""): | |
| # requires a couple extra lines | ||
| self.js_logger.reset() | ||
|
|
||
| await self._apply_headers() | ||
|
|
||
| # reload is truly so close to navigate | ||
| async def reload(self): | ||
| """Reload the tab, and set the javascript runtime id.""" | ||
|
|
@@ -118,6 +125,20 @@ async def reload(self): | |
|
|
||
| self.js_logger.reset() | ||
|
|
||
| await self._apply_headers() | ||
|
|
||
| async def _apply_headers(self): | ||
| """Apply extra HTTP headers to the tab if configured.""" | ||
| if self._headers: | ||
| _logger.debug2(f"Setting extra HTTP headers on {self.tab}") | ||
| _raise_error(await self.tab.send_command("Network.enable")) | ||
|
Collaborator
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. What is this
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. That needs to be called first so that the call to |
||
| _raise_error( | ||
| await self.tab.send_command( | ||
| "Network.setExtraHTTPHeaders", | ||
| params={"headers": self._headers}, | ||
| ) | ||
| ) | ||
|
|
||
| async def _calc_fig( | ||
| self, | ||
| spec: fig_tools.Spec, | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Is there a potential for a race condition here, where some requests might get sent before the headers are applied? Should
_apply_headers()be called earlier in this function?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.
As we discussed, maybe? Moving this call earlier shouldn't hurt anything, so I'll do that.
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.
@camdecoster Are you still OK with moving this call earlier?
Also, should the headers be applied to the initial call to
Page.navigate?