Skip to content

Conversation

WebReflection
Copy link
Contributor

@WebReflection WebReflection commented Mar 14, 2024

Description

This MR fixes #1993 by providing the described API.

Changes

  • create a fetch.py file in our stdlib folder
  • expose such fetch utility through our stdlib
  • test that both Pyodide and MicroPython work as expected via integration tests

Checklist

  • All tests pass locally
  • I have updated CHANGELOG.md
  • I have created documentation for this(if applicable)

@hoodmane
Copy link
Contributor

How different is this from Pyodide's pyfetch?
https://pyodide.org/en/stable/usage/api/python-api/http.html

@WebReflection
Copy link
Contributor Author

WebReflection commented Mar 14, 2024

@hoodmane the requirements were discussed in here and the issue describes the final goal.

The idea was to provide a cross interpreter easy-peasy way to simply fetch data and with the following goals:

  • it should work consistently in both Pyodide and MicroPython without asking MicroPython to implement the whole Pyodide API
  • it should be the easiest possible way for users that don't necessarily expect to handle Response results, meaning it requires a single await fetch(url).text() instead of double await ... this has been recently proven to be desired most of the cases and if users need to fine-tune or better handle in a more robust way all possible cases and quirks, they can just use the native js.fetch API or use pyfetch from Pyodide or do whatever they need to do

I hope this reasons well but happy to be corrected from @ntoll or maybe he'd like to add more to it 👋

edit btw, I do think we should mention that for broader use cases the pyodide.http.pyfetch API should be considered, I hope Nicholas agrees here. I don't think we have any equivalent in MicroPython though so that would be Pyodide specific.

@WebReflection
Copy link
Contributor Author

WebReflection commented Mar 14, 2024

edit never mind, I've already implemented and tested this, we should be good now


@ntoll one thing maybe we can also do is to check response.ok for all methods and provide a nice error in case stuff didn't work as expected ... this is specially handy in case the request was a 404 or 500 and we don't want the rest of the code to eventually parse or evaluate those HTML pages (think a .text() that actually failed).

I don't have a strong opinion about this but it was the only thing users were slightly worried about in my fetch module case.

@WebReflection WebReflection merged commit a1268f1 into pyscript:main Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a fetch function to the pyscript namespace in the "standard library"
3 participants