Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/ahttpx/_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


class Content:
def encode(self) -> Stream:
def open(self) -> Stream:
raise NotImplementedError()

def content_type(self) -> str:
Expand Down Expand Up @@ -76,7 +76,7 @@ def __init__(

# Content API

def encode(self) -> Stream:
def open(self) -> Stream:
content = str(self).encode("ascii")
return ByteStream(content)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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,
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/ahttpx/_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}')
Expand Down
2 changes: 1 addition & 1 deletion src/ahttpx/_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}')
Expand Down
18 changes: 9 additions & 9 deletions src/httpx/_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


class Content:
def encode(self) -> Stream:
def open(self) -> Stream:
raise NotImplementedError()

def content_type(self) -> str:
Expand Down Expand Up @@ -76,7 +76,7 @@ def __init__(

# Content API

def encode(self) -> Stream:
def open(self) -> Stream:
content = str(self).encode("ascii")
return ByteStream(content)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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,
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/httpx/_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}')
Expand Down
2 changes: 1 addition & 1 deletion src/httpx/_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}')
Expand Down
10 changes: 5 additions & 5 deletions tests/test_ahttpx/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
async def test_html():
html = ahttpx.HTML("<html><body>Hello, world</body></html>")

stream = html.encode()
stream = html.open()
content_type = html.content_type()

assert await stream.read() == b'<html><body>Hello, world</body></html>'
Expand All @@ -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'
Expand All @@ -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}'
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions tests/test_httpx/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def test_html():
html = httpx.HTML("<html><body>Hello, world</body></html>")

stream = html.encode()
stream = html.open()
content_type = html.content_type()

assert stream.read() == b'<html><body>Hello, world</body></html>'
Expand All @@ -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'
Expand All @@ -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}'
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down