From 8513bfbe9d6d6337a9e90f4ae1dfd89d3e28aa12 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 21 Jul 2023 16:39:28 +1000 Subject: [PATCH] requests: Rename urequests to requests. This module implements a subset of the Python requests module, and so it should have the same name. Added a backwards-compatibility wrapper to allow people to continue to use `import urequests`. This lives in micropython/urequests. Changed requests to be a package, so that we can implement extension packages in the future for optional functionality. Added a basic README.md to both. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- .../bundles/bundle-networking/manifest.py | 6 +++++- micropython/mip/manifest.py | 2 +- micropython/mip/mip/__init__.py | 2 +- micropython/urequests/README.md | 9 +++++++++ micropython/urequests/manifest.py | 5 +++++ micropython/urequests/urequests.py | 8 ++++++++ python-ecosys/requests/README.md | 16 ++++++++++++++++ .../{urequests => requests}/example_xively.py | 5 +---- python-ecosys/requests/manifest.py | 3 +++ .../requests/__init__.py} | 0 python-ecosys/urequests/manifest.py | 3 --- 11 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 micropython/urequests/README.md create mode 100644 micropython/urequests/manifest.py create mode 100644 micropython/urequests/urequests.py create mode 100644 python-ecosys/requests/README.md rename python-ecosys/{urequests => requests}/example_xively.py (80%) create mode 100644 python-ecosys/requests/manifest.py rename python-ecosys/{urequests/urequests.py => requests/requests/__init__.py} (100%) delete mode 100644 python-ecosys/urequests/manifest.py diff --git a/micropython/bundles/bundle-networking/manifest.py b/micropython/bundles/bundle-networking/manifest.py index 1db1b08..79d5e9d 100644 --- a/micropython/bundles/bundle-networking/manifest.py +++ b/micropython/bundles/bundle-networking/manifest.py @@ -5,5 +5,9 @@ metadata( require("mip") require("ntptime") -require("urequests") +require("requests") require("webrepl") + +# Provide urequests (which just forwards to requests) for backwards +# compatibility. +require("urequests") diff --git a/micropython/mip/manifest.py b/micropython/mip/manifest.py index f6d47e2..00efa54 100644 --- a/micropython/mip/manifest.py +++ b/micropython/mip/manifest.py @@ -1,5 +1,5 @@ metadata(version="0.2.0", description="On-device package installer for network-capable boards") -require("urequests") +require("requests") package("mip", opt=3) diff --git a/micropython/mip/mip/__init__.py b/micropython/mip/mip/__init__.py index 0593e2e..5f6f4fc 100644 --- a/micropython/mip/mip/__init__.py +++ b/micropython/mip/mip/__init__.py @@ -1,7 +1,7 @@ # MicroPython package installer # MIT license; Copyright (c) 2022 Jim Mussared -import urequests as requests +import requests import sys diff --git a/micropython/urequests/README.md b/micropython/urequests/README.md new file mode 100644 index 0000000..f6612b3 --- /dev/null +++ b/micropython/urequests/README.md @@ -0,0 +1,9 @@ +## urequests compatibility + +The MicroPython version of +[requests](https://requests.readthedocs.io/en/latest/) was previously called +`urequests` and a lot of existing code depends on being able to still +import the module by that name. + +This package provides a wrapper to allow this. Prefer to install and use the +`requests` package instead. diff --git a/micropython/urequests/manifest.py b/micropython/urequests/manifest.py new file mode 100644 index 0000000..3fbe61c --- /dev/null +++ b/micropython/urequests/manifest.py @@ -0,0 +1,5 @@ +metadata(version="0.8.0", pypi="requests") + +require("requests") + +module("urequests.py") diff --git a/micropython/urequests/urequests.py b/micropython/urequests/urequests.py new file mode 100644 index 0000000..227a1ae --- /dev/null +++ b/micropython/urequests/urequests.py @@ -0,0 +1,8 @@ +# This module provides a backwards-compatble import for `urequests`. +# It lazy-loads from `requests` without duplicating its globals dict. + + +def __getattr__(attr): + import requests + + return getattr(requests, attr) diff --git a/python-ecosys/requests/README.md b/python-ecosys/requests/README.md new file mode 100644 index 0000000..d6ceaad --- /dev/null +++ b/python-ecosys/requests/README.md @@ -0,0 +1,16 @@ +## requests + +This module provides a lightweight version of the Python +[requests](https://requests.readthedocs.io/en/latest/) library. + +It includes support for all HTTP verbs, https, json decoding of responses, +redirects, basic authentication. + +### Limitations + +* Certificate validation is not currently supported. +* A dictionary passed as post data will not do automatic JSON or + multipart-form encoding of post data (this can be done manually). +* Compressed requests/responses are not currently supported. +* File upload is not supported. +* Chunked encoding in responses is not supported. diff --git a/python-ecosys/urequests/example_xively.py b/python-ecosys/requests/example_xively.py similarity index 80% rename from python-ecosys/urequests/example_xively.py rename to python-ecosys/requests/example_xively.py index 88b890c..60e139b 100644 --- a/python-ecosys/urequests/example_xively.py +++ b/python-ecosys/requests/example_xively.py @@ -1,7 +1,4 @@ -try: - import urequests as requests -except ImportError: - import requests +import requests r = requests.get("http://api.xively.com/") print(r) diff --git a/python-ecosys/requests/manifest.py b/python-ecosys/requests/manifest.py new file mode 100644 index 0000000..7fc2d63 --- /dev/null +++ b/python-ecosys/requests/manifest.py @@ -0,0 +1,3 @@ +metadata(version="0.8.0", pypi="requests") + +package("requests") diff --git a/python-ecosys/urequests/urequests.py b/python-ecosys/requests/requests/__init__.py similarity index 100% rename from python-ecosys/urequests/urequests.py rename to python-ecosys/requests/requests/__init__.py diff --git a/python-ecosys/urequests/manifest.py b/python-ecosys/urequests/manifest.py deleted file mode 100644 index 4c13408..0000000 --- a/python-ecosys/urequests/manifest.py +++ /dev/null @@ -1,3 +0,0 @@ -metadata(version="0.7.0", pypi="requests") - -module("urequests.py")