Add Fetch API to PythonMonkey
Describe your feature request here.
Add the Fetch API to PythonMonkey as per its spec (or a subset of it)
- Spec: https://fetch.spec.whatwg.org/
- MDN: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
Work for fetch will include:
fetchfunction (https://fetch.spec.whatwg.org/#fetch-method)RequestinterfaceResponseinterfaceHeadersinterface- and potentially more - please refer to the spec
This will likely be implemented as a wrapper over Python's requests module.
Using fetch from JavaScript code would be very familiar for Web developers using PythonMonkey. It will also provide fetch to Python developers as an alternative promise based request api.
Example usage (in Python):
import pythonmonkey
import asyncio
async def logMovies():
response = await pythonmonkey.fetch("http://example.com/movies.json")
movies = await response.json()
print(movies)
asyncio.run(logMovies)
Example usage (in JavaScript):
async function logMovies() {
const response = await fetch("http://example.com/movies.json");
const movies = await response.json();
console.log(movies);
}
logMovies()
Code example
Promise<Response> fetch(RequestInfo input, optional RequestInit init = {});
I think this is a great idea. Current plan, subject to change --
- implement XHR via Python's aiohttp -- team should be starting this week, I'm guessing it will probably ship in a release this month.
- implement fetch via https://www.npmjs.com/package/whatwg-fetch, which polyfills atop XHR
Somebody needing a simple version of fetch in JS can probably use the Python requests library from JS in the meantime.
Here are some instructions for adding new API to JavaScript's global namespace:
For instance, if you wanted to add fetch, you could write a js file in python/pythonmonkey/builtin_modules/fetch.js
python.exec(`
def fetch():
return "Hello, World!"
`)
fetch = python.eval('fetch');
if (!globalThis.fetch) {
globalThis.fetch = fetch
}
exports.fetch = fetch;
And then just require this file in python/pythonmonkey/__init.py:
10
11 # Load the module by default to expose global APIs
12 require("console")
13 require("base64")
14 require("timers")
+ 15 require("fetch")
16
You'll be able to use npm packages, pip packages and more to help implement any of the features. The example above just demonstrates adding a python function inside a commonjs module to globalThis for JavaScript code. Another approach would be to leverage the XHR implementation and some potential polyfills for fetch using xhr that may exist to implement fetch. Another approach could be to write it in Python or even C++.
PythonMonkey team, let me know if I'm missing anything or got anything wrong here.
PythonMonkey team here.
It's recommended to write a py file in python/pythonmonkey/builtin_modules/fetch.py if most of your code is implemented in Python.
def fetch():
...
from pythonmonkey import globalThis
if not globalThis['fetch']:
globalThis['fetch'] = fetch
exports['fetch'] = fetch
In python/pythonmonkey/__init__.py, you can use everything our require function supports, besides builtin_modules.
It just makes pythonmonkey preload the polyfill specified in that module.
More documentation is coming at https://docs.pythonmonkey.io/ (not available yet).