Skip to content

Commit 9ff90ce

Browse files
Content.encode() -> Content.open() (#144)
1 parent 03d2b34 commit 9ff90ce

File tree

8 files changed

+32
-32
lines changed

8 files changed

+32
-32
lines changed

src/ahttpx/_content.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131

3232
class Content:
33-
def encode(self) -> Stream:
33+
def open(self) -> Stream:
3434
raise NotImplementedError()
3535

3636
def content_type(self) -> str:
@@ -76,7 +76,7 @@ def __init__(
7676

7777
# Content API
7878

79-
def encode(self) -> Stream:
79+
def open(self) -> Stream:
8080
content = str(self).encode("ascii")
8181
return ByteStream(content)
8282

@@ -177,7 +177,7 @@ def name(self) -> str:
177177
def size(self) -> int:
178178
return os.path.getsize(self._path)
179179

180-
def encode(self) -> Stream:
180+
def open(self) -> Stream:
181181
return FileStream(self._path)
182182

183183
def content_type(self) -> str:
@@ -254,8 +254,8 @@ def get_list(self, key: str) -> list[File]:
254254
return list(self._dict.get(key, []))
255255

256256
# Content interface
257-
def encode(self) -> Stream:
258-
return MultiPart(files=self).encode()
257+
def open(self) -> Stream:
258+
return MultiPart(files=self).open()
259259

260260
def content_type(self) -> str:
261261
return f"multipart/form-data; boundary={self._boundary}"
@@ -290,7 +290,7 @@ class JSON(Content):
290290
def __init__(self, data: typing.Any) -> None:
291291
self._data = data
292292

293-
def encode(self) -> Stream:
293+
def open(self) -> Stream:
294294
content = json.dumps(
295295
self._data,
296296
ensure_ascii=False,
@@ -310,7 +310,7 @@ class Text(Content):
310310
def __init__(self, text: str) -> None:
311311
self._text = text
312312

313-
def encode(self) -> Stream:
313+
def open(self) -> Stream:
314314
content = self._text.encode("utf-8")
315315
return ByteStream(content)
316316

@@ -325,7 +325,7 @@ class HTML(Content):
325325
def __init__(self, text: str) -> None:
326326
self._text = text
327327

328-
def encode(self) -> Stream:
328+
def open(self) -> Stream:
329329
content = self._text.encode("utf-8")
330330
return ByteStream(content)
331331

@@ -366,7 +366,7 @@ def form(self) -> Form:
366366
def files(self) -> Files:
367367
return self._files
368368

369-
def encode(self) -> Stream:
369+
def open(self) -> Stream:
370370
form = [(key, value) for key, value in self._form.items()]
371371
files = [(key, file._path) for key, file in self._files.items()]
372372
return MultiPartStream(form, files, boundary=self._boundary)

src/ahttpx/_request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(
5757
self.stream = content
5858
elif isinstance(content, Content):
5959
ct = content.content_type()
60-
self.stream = content.encode()
60+
self.stream = content.open()
6161
self.headers = self.headers.copy_set("Content-Type", ct)
6262
else:
6363
raise TypeError(f'Expected `Content | Stream | bytes | None` got {type(content)}')

src/ahttpx/_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def __init__(
148148
self.stream = content
149149
elif isinstance(content, Content):
150150
ct = content.content_type()
151-
self.stream = content.encode()
151+
self.stream = content.open()
152152
self.headers = self.headers.copy_set("Content-Type", ct)
153153
else:
154154
raise TypeError(f'Expected `Content | Stream | bytes | None` got {type(content)}')

src/httpx/_content.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131

3232
class Content:
33-
def encode(self) -> Stream:
33+
def open(self) -> Stream:
3434
raise NotImplementedError()
3535

3636
def content_type(self) -> str:
@@ -76,7 +76,7 @@ def __init__(
7676

7777
# Content API
7878

79-
def encode(self) -> Stream:
79+
def open(self) -> Stream:
8080
content = str(self).encode("ascii")
8181
return ByteStream(content)
8282

@@ -177,7 +177,7 @@ def name(self) -> str:
177177
def size(self) -> int:
178178
return os.path.getsize(self._path)
179179

180-
def encode(self) -> Stream:
180+
def open(self) -> Stream:
181181
return FileStream(self._path)
182182

183183
def content_type(self) -> str:
@@ -254,8 +254,8 @@ def get_list(self, key: str) -> list[File]:
254254
return list(self._dict.get(key, []))
255255

256256
# Content interface
257-
def encode(self) -> Stream:
258-
return MultiPart(files=self).encode()
257+
def open(self) -> Stream:
258+
return MultiPart(files=self).open()
259259

260260
def content_type(self) -> str:
261261
return f"multipart/form-data; boundary={self._boundary}"
@@ -290,7 +290,7 @@ class JSON(Content):
290290
def __init__(self, data: typing.Any) -> None:
291291
self._data = data
292292

293-
def encode(self) -> Stream:
293+
def open(self) -> Stream:
294294
content = json.dumps(
295295
self._data,
296296
ensure_ascii=False,
@@ -310,7 +310,7 @@ class Text(Content):
310310
def __init__(self, text: str) -> None:
311311
self._text = text
312312

313-
def encode(self) -> Stream:
313+
def open(self) -> Stream:
314314
content = self._text.encode("utf-8")
315315
return ByteStream(content)
316316

@@ -325,7 +325,7 @@ class HTML(Content):
325325
def __init__(self, text: str) -> None:
326326
self._text = text
327327

328-
def encode(self) -> Stream:
328+
def open(self) -> Stream:
329329
content = self._text.encode("utf-8")
330330
return ByteStream(content)
331331

@@ -366,7 +366,7 @@ def form(self) -> Form:
366366
def files(self) -> Files:
367367
return self._files
368368

369-
def encode(self) -> Stream:
369+
def open(self) -> Stream:
370370
form = [(key, value) for key, value in self._form.items()]
371371
files = [(key, file._path) for key, file in self._files.items()]
372372
return MultiPartStream(form, files, boundary=self._boundary)

src/httpx/_request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(
5757
self.stream = content
5858
elif isinstance(content, Content):
5959
ct = content.content_type()
60-
self.stream = content.encode()
60+
self.stream = content.open()
6161
self.headers = self.headers.copy_set("Content-Type", ct)
6262
else:
6363
raise TypeError(f'Expected `Content | Stream | bytes | None` got {type(content)}')

src/httpx/_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def __init__(
148148
self.stream = content
149149
elif isinstance(content, Content):
150150
ct = content.content_type()
151-
self.stream = content.encode()
151+
self.stream = content.open()
152152
self.headers = self.headers.copy_set("Content-Type", ct)
153153
else:
154154
raise TypeError(f'Expected `Content | Stream | bytes | None` got {type(content)}')

tests/test_ahttpx/test_content.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
async def test_html():
1111
html = ahttpx.HTML("<html><body>Hello, world</body></html>")
1212

13-
stream = html.encode()
13+
stream = html.open()
1414
content_type = html.content_type()
1515

1616
assert await stream.read() == b'<html><body>Hello, world</body></html>'
@@ -23,7 +23,7 @@ async def test_html():
2323
async def test_text():
2424
text = ahttpx.Text("Hello, world")
2525

26-
stream = text.encode()
26+
stream = text.open()
2727
content_type = text.content_type()
2828

2929
assert await stream.read() == b'Hello, world'
@@ -36,7 +36,7 @@ async def test_text():
3636
async def test_json():
3737
data = ahttpx.JSON({'data': 123})
3838

39-
stream = data.encode()
39+
stream = data.open()
4040
content_type = data.content_type()
4141

4242
assert await stream.read() == b'{"data":123}'
@@ -131,7 +131,7 @@ async def test_form_encode():
131131
form = ahttpx.Form({'email': 'address@example.com'})
132132
assert form['email'] == "address@example.com"
133133

134-
stream = form.encode()
134+
stream = form.open()
135135
content_type = form.content_type()
136136

137137
assert await stream.read() == b"email=address%40example.com"
@@ -272,7 +272,7 @@ async def test_multipart():
272272
assert multipart.files['upload'] == ahttpx.File(f.name)
273273

274274
fname = os.path.basename(f.name).encode('utf-8')
275-
stream = multipart.encode()
275+
stream = multipart.open()
276276
content_type = multipart.content_type()
277277

278278
content_type == "multipart/form-data; boundary=BOUNDARY"

tests/test_httpx/test_content.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def test_html():
1010
html = httpx.HTML("<html><body>Hello, world</body></html>")
1111

12-
stream = html.encode()
12+
stream = html.open()
1313
content_type = html.content_type()
1414

1515
assert stream.read() == b'<html><body>Hello, world</body></html>'
@@ -21,7 +21,7 @@ def test_html():
2121
def test_text():
2222
text = httpx.Text("Hello, world")
2323

24-
stream = text.encode()
24+
stream = text.open()
2525
content_type = text.content_type()
2626

2727
assert stream.read() == b'Hello, world'
@@ -33,7 +33,7 @@ def test_text():
3333
def test_json():
3434
data = httpx.JSON({'data': 123})
3535

36-
stream = data.encode()
36+
stream = data.open()
3737
content_type = data.content_type()
3838

3939
assert stream.read() == b'{"data":123}'
@@ -127,7 +127,7 @@ def test_form_encode():
127127
form = httpx.Form({'email': 'address@example.com'})
128128
assert form['email'] == "address@example.com"
129129

130-
stream = form.encode()
130+
stream = form.open()
131131
content_type = form.content_type()
132132

133133
assert stream.read() == b"email=address%40example.com"
@@ -267,7 +267,7 @@ def test_multipart():
267267
assert multipart.files['upload'] == httpx.File(f.name)
268268

269269
fname = os.path.basename(f.name).encode('utf-8')
270-
stream = multipart.encode()
270+
stream = multipart.open()
271271
content_type = multipart.content_type()
272272

273273
content_type == "multipart/form-data; boundary=BOUNDARY"

0 commit comments

Comments
 (0)