diff --git a/src/ahttpx/_content.py b/src/ahttpx/_content.py index e2c4aaa..ee7e0be 100644 --- a/src/ahttpx/_content.py +++ b/src/ahttpx/_content.py @@ -30,7 +30,7 @@ class Content: - def encode(self) -> Stream: + def open(self) -> Stream: raise NotImplementedError() def content_type(self) -> str: @@ -76,7 +76,7 @@ def __init__( # Content API - def encode(self) -> Stream: + def open(self) -> Stream: content = str(self).encode("ascii") return ByteStream(content) @@ -177,7 +177,7 @@ def name(self) -> str: def size(self) -> int: return os.path.getsize(self._path) - def encode(self) -> Stream: + def open(self) -> Stream: return FileStream(self._path) def content_type(self) -> str: @@ -254,8 +254,8 @@ def get_list(self, key: str) -> list[File]: return list(self._dict.get(key, [])) # Content interface - def encode(self) -> Stream: - return MultiPart(files=self).encode() + def open(self) -> Stream: + return MultiPart(files=self).open() def content_type(self) -> str: return f"multipart/form-data; boundary={self._boundary}" @@ -290,7 +290,7 @@ class JSON(Content): def __init__(self, data: typing.Any) -> None: self._data = data - def encode(self) -> Stream: + def open(self) -> Stream: content = json.dumps( self._data, ensure_ascii=False, @@ -310,7 +310,7 @@ class Text(Content): def __init__(self, text: str) -> None: self._text = text - def encode(self) -> Stream: + def open(self) -> Stream: content = self._text.encode("utf-8") return ByteStream(content) @@ -325,7 +325,7 @@ class HTML(Content): def __init__(self, text: str) -> None: self._text = text - def encode(self) -> Stream: + def open(self) -> Stream: content = self._text.encode("utf-8") return ByteStream(content) @@ -366,7 +366,7 @@ def form(self) -> Form: def files(self) -> Files: return self._files - def encode(self) -> Stream: + def open(self) -> Stream: form = [(key, value) for key, value in self._form.items()] files = [(key, file._path) for key, file in self._files.items()] return MultiPartStream(form, files, boundary=self._boundary) diff --git a/src/ahttpx/_request.py b/src/ahttpx/_request.py index 8f836b5..3062f40 100644 --- a/src/ahttpx/_request.py +++ b/src/ahttpx/_request.py @@ -57,7 +57,7 @@ def __init__( self.stream = content elif isinstance(content, Content): ct = content.content_type() - self.stream = content.encode() + self.stream = content.open() self.headers = self.headers.copy_set("Content-Type", ct) else: raise TypeError(f'Expected `Content | Stream | bytes | None` got {type(content)}') diff --git a/src/ahttpx/_response.py b/src/ahttpx/_response.py index c2420a7..777b7d3 100644 --- a/src/ahttpx/_response.py +++ b/src/ahttpx/_response.py @@ -148,7 +148,7 @@ def __init__( self.stream = content elif isinstance(content, Content): ct = content.content_type() - self.stream = content.encode() + self.stream = content.open() self.headers = self.headers.copy_set("Content-Type", ct) else: raise TypeError(f'Expected `Content | Stream | bytes | None` got {type(content)}') diff --git a/src/httpx/_content.py b/src/httpx/_content.py index e2c4aaa..ee7e0be 100644 --- a/src/httpx/_content.py +++ b/src/httpx/_content.py @@ -30,7 +30,7 @@ class Content: - def encode(self) -> Stream: + def open(self) -> Stream: raise NotImplementedError() def content_type(self) -> str: @@ -76,7 +76,7 @@ def __init__( # Content API - def encode(self) -> Stream: + def open(self) -> Stream: content = str(self).encode("ascii") return ByteStream(content) @@ -177,7 +177,7 @@ def name(self) -> str: def size(self) -> int: return os.path.getsize(self._path) - def encode(self) -> Stream: + def open(self) -> Stream: return FileStream(self._path) def content_type(self) -> str: @@ -254,8 +254,8 @@ def get_list(self, key: str) -> list[File]: return list(self._dict.get(key, [])) # Content interface - def encode(self) -> Stream: - return MultiPart(files=self).encode() + def open(self) -> Stream: + return MultiPart(files=self).open() def content_type(self) -> str: return f"multipart/form-data; boundary={self._boundary}" @@ -290,7 +290,7 @@ class JSON(Content): def __init__(self, data: typing.Any) -> None: self._data = data - def encode(self) -> Stream: + def open(self) -> Stream: content = json.dumps( self._data, ensure_ascii=False, @@ -310,7 +310,7 @@ class Text(Content): def __init__(self, text: str) -> None: self._text = text - def encode(self) -> Stream: + def open(self) -> Stream: content = self._text.encode("utf-8") return ByteStream(content) @@ -325,7 +325,7 @@ class HTML(Content): def __init__(self, text: str) -> None: self._text = text - def encode(self) -> Stream: + def open(self) -> Stream: content = self._text.encode("utf-8") return ByteStream(content) @@ -366,7 +366,7 @@ def form(self) -> Form: def files(self) -> Files: return self._files - def encode(self) -> Stream: + def open(self) -> Stream: form = [(key, value) for key, value in self._form.items()] files = [(key, file._path) for key, file in self._files.items()] return MultiPartStream(form, files, boundary=self._boundary) diff --git a/src/httpx/_request.py b/src/httpx/_request.py index 4e7d2b3..e6c7de8 100644 --- a/src/httpx/_request.py +++ b/src/httpx/_request.py @@ -57,7 +57,7 @@ def __init__( self.stream = content elif isinstance(content, Content): ct = content.content_type() - self.stream = content.encode() + self.stream = content.open() self.headers = self.headers.copy_set("Content-Type", ct) else: raise TypeError(f'Expected `Content | Stream | bytes | None` got {type(content)}') diff --git a/src/httpx/_response.py b/src/httpx/_response.py index 4f4fb13..3315ee4 100644 --- a/src/httpx/_response.py +++ b/src/httpx/_response.py @@ -148,7 +148,7 @@ def __init__( self.stream = content elif isinstance(content, Content): ct = content.content_type() - self.stream = content.encode() + self.stream = content.open() self.headers = self.headers.copy_set("Content-Type", ct) else: raise TypeError(f'Expected `Content | Stream | bytes | None` got {type(content)}') diff --git a/tests/test_ahttpx/test_content.py b/tests/test_ahttpx/test_content.py index f8a1500..f2f6dcb 100644 --- a/tests/test_ahttpx/test_content.py +++ b/tests/test_ahttpx/test_content.py @@ -10,7 +10,7 @@ async def test_html(): html = ahttpx.HTML("Hello, world") - stream = html.encode() + stream = html.open() content_type = html.content_type() assert await stream.read() == b'Hello, world' @@ -23,7 +23,7 @@ async def test_html(): async def test_text(): text = ahttpx.Text("Hello, world") - stream = text.encode() + stream = text.open() content_type = text.content_type() assert await stream.read() == b'Hello, world' @@ -36,7 +36,7 @@ async def test_text(): async def test_json(): data = ahttpx.JSON({'data': 123}) - stream = data.encode() + stream = data.open() content_type = data.content_type() assert await stream.read() == b'{"data":123}' @@ -131,7 +131,7 @@ async def test_form_encode(): form = ahttpx.Form({'email': 'address@example.com'}) assert form['email'] == "address@example.com" - stream = form.encode() + stream = form.open() content_type = form.content_type() assert await stream.read() == b"email=address%40example.com" @@ -272,7 +272,7 @@ async def test_multipart(): assert multipart.files['upload'] == ahttpx.File(f.name) fname = os.path.basename(f.name).encode('utf-8') - stream = multipart.encode() + stream = multipart.open() content_type = multipart.content_type() content_type == "multipart/form-data; boundary=BOUNDARY" diff --git a/tests/test_httpx/test_content.py b/tests/test_httpx/test_content.py index d5fc3de..bc740ce 100644 --- a/tests/test_httpx/test_content.py +++ b/tests/test_httpx/test_content.py @@ -9,7 +9,7 @@ def test_html(): html = httpx.HTML("Hello, world") - stream = html.encode() + stream = html.open() content_type = html.content_type() assert stream.read() == b'Hello, world' @@ -21,7 +21,7 @@ def test_html(): def test_text(): text = httpx.Text("Hello, world") - stream = text.encode() + stream = text.open() content_type = text.content_type() assert stream.read() == b'Hello, world' @@ -33,7 +33,7 @@ def test_text(): def test_json(): data = httpx.JSON({'data': 123}) - stream = data.encode() + stream = data.open() content_type = data.content_type() assert stream.read() == b'{"data":123}' @@ -127,7 +127,7 @@ def test_form_encode(): form = httpx.Form({'email': 'address@example.com'}) assert form['email'] == "address@example.com" - stream = form.encode() + stream = form.open() content_type = form.content_type() assert stream.read() == b"email=address%40example.com" @@ -267,7 +267,7 @@ def test_multipart(): assert multipart.files['upload'] == httpx.File(f.name) fname = os.path.basename(f.name).encode('utf-8') - stream = multipart.encode() + stream = multipart.open() content_type = multipart.content_type() content_type == "multipart/form-data; boundary=BOUNDARY"