-
Notifications
You must be signed in to change notification settings - Fork 41
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
Comments
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. |
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.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 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 def fetch():
...
from pythonmonkey import globalThis
if not globalThis['fetch']:
globalThis['fetch'] = fetch
exports['fetch'] = fetch In More documentation is coming at https://docs.pythonmonkey.io/ (not available yet). |
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:
fetch
function (https://fetch.spec.whatwg.org/#fetch-method)Request
interfaceResponse
interfaceHeaders
interfaceThis 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):
Example usage (in JavaScript):
Code example
The text was updated successfully, but these errors were encountered: