{ "info": { "author": "Michal Charemza", "author_email": "michal@charemza.name", "bugtrack_url": null, "classifiers": [ "Framework :: AsyncIO", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "# lowhaio-redirect [![CircleCI](https://circleci.com/gh/michalc/lowhaio-redirect.svg?style=svg)](https://circleci.com/gh/michalc/lowhaio-redirect)\n\nWrapper of lowhaio that follows HTTP redirects\n\n\n## Installation\n\n```bash\npip install lowhaio lowhaio_redirect\n```\n\n\n## Usage\n\nThe `request` function returned from `lowhaio.Pool` must be wrapped with `lowhaio_redirect.redirectable`, as in the below example.\n\n```python\nimport os\nfrom lowhaio import Pool\nfrom lowhaio_redirect import redirectable\n\nrequest, _ = Pool()\n\nredirectable_request = redirectable(request)\ncode, headers, body = await redirectable_request(b'GET', 'https://example.com/path')\n\nasync for chunk in body:\n print(chunk)\n```\n\n\n## Method and body changing\n\nThe default behavior is as below.\n\n- 301, 302 redirects from a POST are converted to GETs with empty bodies; all other requests do not change methods and bodies.\n\n- 303 redirects, all non HEAD and GET requests are converted to GETs with empty bodies.\n\n- 307 and 308 redirects, the method and bodies are always unchanged.\n\nNote however, that an unchanged body is actually _not_ guaranteed by this wrapper. For each request the function passed as the `body` argument is called, and it may return different things on each call. It is up the the developer to handle this case as needed: there is no one-size-fits all approach, since for streaming requests, the body may not be available again. In many cases, a redirect that expects a resubmission of a large upload may be an error; or if an API is never expected to return a redirect, _not_ using this wrapper at all may be a viable option, and instead a few lines of code that raises an exception if a redirect is returned from the server.\n\n\n## Customise redirects\n\nIt is possible to customise which redirects are followed, and how they affect the method and body. As an example, to recreate the default behaviour explicitly, the below code could be used.\n\n```python\ndef get(method, body, body_args, body_kwargs, headers):\n # Asynchronous generator that end immediately, and results in an empty body\n async def empty_body():\n while False:\n yield\n next_request_headers = tuple(\n (key, value)\n for key, value in headers if key.lower() not in (b'content-length', b'transfer-encoding')\n )\n\n return (b'GET', empty_body, (), (), next_request_headers)\n\n\ndef unchanged(method, body, body_args, body_kwargs, headers):\n return (method, body, body_args, body_kwargs, headers)\n\n\nredirectable_request = redirectable(request, redirects=(\n (b'301', lambda method: unchanged if method != b'POST' else get),\n (b'302', lambda method: unchanged if method != b'POST' else get),\n (b'303', lambda method: unchanged if method in (b'GET', b'HEAD') else get),\n (b'307', lambda method: unchanged),\n (b'308', lambda method: unchanged),\n))\n```\n\n\n## Authorization header\n\nBy default, the Authorization header is not passed to if redirected to another domain. This can be customised. As an example, to recreate the default behaviour explicitly, the below code can be used.\n\n```python\ndef strip_authorization_if_different_host(request_headers, request_host, redirect_host):\n forbidden = \\\n (b'authorization',) if request_host != redirect_host else \\\n ()\n return tuple(\n (key, value)\n for key, value in request_headers if key.lower() not in forbidden\n )\n\nredirectable_request = redirectable(request, transform_headers=strip_authorization_if_different_host)\n```\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/michalc/lowhaio-redirect", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "lowhaio-redirect", "package_url": "https://pypi.org/project/lowhaio-redirect/", "platform": "", "project_url": "https://pypi.org/project/lowhaio-redirect/", "project_urls": { "Homepage": "https://github.com/michalc/lowhaio-redirect" }, "release_url": "https://pypi.org/project/lowhaio-redirect/0.0.1/", "requires_dist": null, "requires_python": ">=3.6.3", "summary": "Lowhaio wrapper that follows HTTP redirects", "version": "0.0.1" }, "last_serial": 5435694, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "9cb9230d988e9f458c33d5402a612b1f", "sha256": "f9628e5b91e4bdb7510cdf806accd6bafd49413a213c754290116af2e88f2ca1" }, "downloads": -1, "filename": "lowhaio_redirect-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "9cb9230d988e9f458c33d5402a612b1f", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6.3", "size": 4495, "upload_time": "2019-06-22T21:29:47", "url": "https://files.pythonhosted.org/packages/47/72/e7de9f7f5ffc4b0f50e9cb3b44f14179af0498e8858d8a6f39e47ba062a2/lowhaio_redirect-0.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "bc99273869f9083a5c5cba3f687659d7", "sha256": "83904c220d4467c7b105b46387cd72931c8f6d4487d13730e4a1b6cfe1ed345b" }, "downloads": -1, "filename": "lowhaio_redirect-0.0.1.tar.gz", "has_sig": false, "md5_digest": "bc99273869f9083a5c5cba3f687659d7", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6.3", "size": 3248, "upload_time": "2019-06-22T21:29:49", "url": "https://files.pythonhosted.org/packages/2f/c6/a46234e87f913ad3cd76ec5f8e5396df7b51f167cfa918ad19026a7f5bb3/lowhaio_redirect-0.0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "9cb9230d988e9f458c33d5402a612b1f", "sha256": "f9628e5b91e4bdb7510cdf806accd6bafd49413a213c754290116af2e88f2ca1" }, "downloads": -1, "filename": "lowhaio_redirect-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "9cb9230d988e9f458c33d5402a612b1f", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6.3", "size": 4495, "upload_time": "2019-06-22T21:29:47", "url": "https://files.pythonhosted.org/packages/47/72/e7de9f7f5ffc4b0f50e9cb3b44f14179af0498e8858d8a6f39e47ba062a2/lowhaio_redirect-0.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "bc99273869f9083a5c5cba3f687659d7", "sha256": "83904c220d4467c7b105b46387cd72931c8f6d4487d13730e4a1b6cfe1ed345b" }, "downloads": -1, "filename": "lowhaio_redirect-0.0.1.tar.gz", "has_sig": false, "md5_digest": "bc99273869f9083a5c5cba3f687659d7", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6.3", "size": 3248, "upload_time": "2019-06-22T21:29:49", "url": "https://files.pythonhosted.org/packages/2f/c6/a46234e87f913ad3cd76ec5f8e5396df7b51f167cfa918ad19026a7f5bb3/lowhaio_redirect-0.0.1.tar.gz" } ] }