Skip to content
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

Store latest response in the browser state #358

Open
LVerneyPEReN opened this issue Feb 26, 2021 · 2 comments · May be fixed by #369
Open

Store latest response in the browser state #358

LVerneyPEReN opened this issue Feb 26, 2021 · 2 comments · May be fixed by #369

Comments

@LVerneyPEReN
Copy link
Contributor

Hi,

It seems the latest request and soup is stored in the browser state but not the latest response object. I have a use case where this would be useful, typically for getting access to headers defined in the last response (content-type for non HTML content for example).

Would it make sense to write something to store the last response in the browser state?

Best,

@hemberger
Copy link
Contributor

Thanks for your question, and sorry for the delayed response!

While the response is not stored in the browser state, it is returned by all methods that produce one. Is that sufficient, or do you really need it to be carried along with the StatefulBrowser instance?

That said, I can't think of any compelling reason to not store the response in the browser state. Does anyone else have any thoughts about this?

@LVerneyPEReN
Copy link
Contributor Author

Hi @hemberger,

Thanks for the feedback on this one! My use case is typically to consume the StatefulBrowser.response differently depending on the Content-Type of the response. I browse either HTML pages or JSON results and in the first case, the StatefulBrowser.page gets populated, but in the later case, the response is discarded from the browser state. Then, I have to deal manually with the response as returned by the calls to StatefulBrowser.open and I lose the main interest of the StatefulBrowser by reproducing part of its logic in my own app.

Storing the last response object in _BrowserState alongside the request object would let me check for the latest response content type and either consume StatefulBrowser.soup or StatefulBrowser.__state.response.json() (or even StatefulBrowser.__state.response.text to get the raw HTML/JSON text).

hemberger added a commit to hemberger/MechanicalSoup that referenced this issue May 1, 2021
Whenever we set the browser state, include the request response.
The `response` property will raise an `AttributeError` if the state
has not yet been set.

This is not strictly necessary, since we return the request response
in most functions that have one, but it is perhaps a convenience in
some scenarios.

Closes MechanicalSoup#358.
hemberger added a commit to hemberger/MechanicalSoup that referenced this issue May 1, 2021
Whenever we set the browser state, include the request response.
The `response` property will raise an `AttributeError` if the state
has not yet been set.

This is not strictly necessary, since we return the request response
in most functions that have one, but it is perhaps a convenience in
some scenarios.

Closes MechanicalSoup#358.
@hemberger hemberger linked a pull request May 1, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants