Skip to content

Add Fetch API to PythonMonkey #161

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

Open
wiwichips opened this issue Aug 8, 2023 · 3 comments
Open

Add Fetch API to PythonMonkey #161

wiwichips opened this issue Aug 8, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@wiwichips
Copy link
Collaborator

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 = {});
@wiwichips wiwichips added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Aug 8, 2023
@wesgarland
Copy link
Collaborator

wesgarland commented Aug 8, 2023

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.

@wiwichips
Copy link
Collaborator Author

wiwichips commented Aug 15, 2023

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
Copy link
Member

Xmader commented Aug 15, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
Status: Backlog
Development

No branches or pull requests

3 participants