Distributive-Network / PythonMonkey

A Mozilla SpiderMonkey JavaScript engine embedded into the Python VM, using the Python engine to provide the JS host environment.
https://pythonmonkey.io
Other
830 stars 37 forks source link

Add Fetch API to PythonMonkey #161

Open wiwichips opened 1 year ago

wiwichips commented 1 year ago

Describe your feature request here.

Add the Fetch API to PythonMonkey as per its spec (or a subset of it)

Work for fetch will include:


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 = {});
wesgarland commented 1 year ago

I think this is a great idea. Current plan, subject to change --

Somebody needing a simple version of fetch in JS can probably use the Python requests library from JS in the meantime.

wiwichips commented 1 year ago

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.

Xmader commented 1 year ago

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).