/httpx-file

Transport adapter fort httpx to allow file:// URI fetching in the local filesystem

Primary LanguagePythonBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

httpx-file

Transport adapter fort httpx to allow file:// URI fetching in the local filesystem.

Installation

pip install httpx-file

Usage

Synchronous

httpx-file subclasses httpx.Client, so you can just replace httpx.Client with httpx_file.Client to get the same behavior with added file:// protocol support.

from httpx_file import Client

client = Client()
client.get('file:///etc/fstab)

Or you can also mount FileTransport in a httpx.Client instance.

from httpx_file import FileTransport
from httpx import Client

client = Client(mounts={'file://': FileTransport()})
client.get('file:///etc/fstab)

Asynchronous

It is also possible to use httpx-file possibilities asynchronous way. To do this, you can just replace 'httpx.AsyncClient' with 'httpx_file.AsyncClient'.

from httpx_file import AsyncClient

# Taken from tests/test_transport.py

from pathlib import Path

THIS = Path(__file__)

async def test_async_client():
    async_client = AsyncClient()
    async_response = await async_client.get(THIS.as_uri())

    assert async_response.content == THIS.read_bytes()

Or you can also mount FileTransport in a httpx.AsyncClient instance.

from httpx_file import FileTransport
from httpx import AsyncClient

client = AsyncClient(mounts={'file://': FileTransport()})
client.get('file:///etc/fstab)