From 46e0fa18e76af9ce571d97590874282847d8e60f Mon Sep 17 00:00:00 2001 From: Fabian Schmitt Date: Thu, 4 Oct 2018 21:53:24 +0200 Subject: [PATCH] urequests: Added Basic Authentication (as in `requests.request()) --- urequests/metadata.txt | 2 +- urequests/setup.py | 2 +- urequests/urequests.py | 11 ++++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/urequests/metadata.txt b/urequests/metadata.txt index 8cd156c39..799b6c704 100644 --- a/urequests/metadata.txt +++ b/urequests/metadata.txt @@ -1,4 +1,4 @@ srctype = micropython-lib type = module -version = 0.6 +version = 0.6.1 author = Paul Sokolovsky diff --git a/urequests/setup.py b/urequests/setup.py index 43db04aba..21780e08e 100644 --- a/urequests/setup.py +++ b/urequests/setup.py @@ -7,7 +7,7 @@ import sdist_upip setup(name='micropython-urequests', - version='0.6', + version='0.6.1', description='urequests module for MicroPython', long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.", url='https://github.com/micropython/micropython-lib', diff --git a/urequests/urequests.py b/urequests/urequests.py index acb220e85..cb01e91ef 100644 --- a/urequests/urequests.py +++ b/urequests/urequests.py @@ -32,7 +32,10 @@ def json(self): return ujson.loads(self.content) -def request(method, url, data=None, json=None, headers={}, stream=None): +def request(method, url, data=None, json=None, headers={}, stream=None, auth=None): + if auth is not None: + headers.update(encode_basic_auth(auth[0], auth[1])) + try: proto, dummy, host, path = url.split("/", 3) except ValueError: @@ -122,3 +125,9 @@ def patch(url, **kw): def delete(url, **kw): return request("DELETE", url, **kw) + +def encode_basic_auth(username, password): + import ubinascii + formated = b"{}:{}".format(username, password) + formated = ubinascii.b2a_base64(formated)[:-1].decode("ascii") + return {'Authorization' : 'Basic {}'.format(formated)}