Skip to content

Commit 76f0e33

Browse files
committed
unified interface as the base
1 parent 4886a3b commit 76f0e33

File tree

1 file changed

+89
-38
lines changed

1 file changed

+89
-38
lines changed

awscrt/aio/http_asyncio.py

Lines changed: 89 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from collections import deque
2525

2626

27-
class HttpClientConnectionAsync(HttpClientConnectionBase):
27+
class HttpClientConnectionAsyncUnified(HttpClientConnectionBase):
2828
"""
2929
An async HTTP client connection.
3030
@@ -38,9 +38,9 @@ async def new(cls,
3838
bootstrap: Optional[ClientBootstrap] = None,
3939
socket_options: Optional[SocketOptions] = None,
4040
tls_connection_options: Optional[TlsConnectionOptions] = None,
41-
proxy_options: Optional['HttpProxyOptions'] = None) -> "HttpClientConnectionAsync":
41+
proxy_options: Optional['HttpProxyOptions'] = None) -> "HttpClientConnectionAsyncUnified":
4242
"""
43-
Asynchronously establish a new HttpClientConnectionAsync.
43+
Asynchronously establish a new HttpClientConnectionAsyncUnified.
4444
4545
Args:
4646
host_name (str): Connect to host.
@@ -87,6 +87,75 @@ async def close(self) -> None:
8787

8888
def request(self,
8989
request: 'HttpRequest',
90+
async_body: AsyncIterator[bytes] = None,
91+
loop: Optional[asyncio.AbstractEventLoop] = None) -> 'HttpClientStreamAsyncUnified':
92+
"""Create `HttpClientStreamAsync` to carry out the request/response exchange.
93+
94+
Args:
95+
request (HttpRequest): Definition for outgoing request.
96+
loop (Optional[asyncio.AbstractEventLoop]): Event loop to use for async operations.
97+
If None, the current event loop is used.
98+
99+
Returns:
100+
HttpClientStreamAsync: Stream for the HTTP request/response exchange.
101+
"""
102+
# return HttpClientStreamAsyncBase(self, request, loop)
103+
pass
104+
105+
106+
class HttpClientConnectionAsync(HttpClientConnectionAsyncUnified):
107+
"""
108+
An async HTTP/1.1 client connection.
109+
110+
Use `HttpClientConnectionAsync.new()` to establish a new connection.
111+
"""
112+
113+
@classmethod
114+
async def new(cls,
115+
host_name: str,
116+
port: int,
117+
bootstrap: Optional[ClientBootstrap] = None,
118+
socket_options: Optional[SocketOptions] = None,
119+
tls_connection_options: Optional[TlsConnectionOptions] = None,
120+
proxy_options: Optional['HttpProxyOptions'] = None) -> "HttpClientConnectionAsync":
121+
"""
122+
Asynchronously establish a new HttpClientConnectionAsync.
123+
124+
Args:
125+
host_name (str): Connect to host.
126+
127+
port (int): Connect to port.
128+
129+
bootstrap (Optional [ClientBootstrap]): Client bootstrap to use when initiating socket connection.
130+
If None is provided, the default singleton is used.
131+
132+
socket_options (Optional[SocketOptions]): Optional socket options.
133+
If None is provided, then default options are used.
134+
135+
tls_connection_options (Optional[TlsConnectionOptions]): Optional TLS
136+
connection options. If None is provided, then the connection will
137+
be attempted over plain-text.
138+
139+
proxy_options (Optional[HttpProxyOptions]): Optional proxy options.
140+
If None is provided then a proxy is not used.
141+
142+
Returns:
143+
HttpClientConnectionAsync: A new HTTP client connection.
144+
"""
145+
future = cls._generic_new(
146+
host_name,
147+
port,
148+
bootstrap,
149+
socket_options,
150+
tls_connection_options,
151+
proxy_options,
152+
expected_version=HttpVersion.Http1_1,
153+
asyncio_connection=True)
154+
return await asyncio.wrap_future(future)
155+
156+
def request(self,
157+
request: 'HttpRequest',
158+
async_body: AsyncIterator[bytes] = None,
90159
loop: Optional[asyncio.AbstractEventLoop] = None) -> 'HttpClientStreamAsync':
91160
"""Create `HttpClientStreamAsync` to carry out the request/response exchange.
92161
@@ -101,7 +170,7 @@ def request(self,
101170
return HttpClientStreamAsync(self, request, loop)
102171

103172

104-
class Http2ClientConnectionAsync(HttpClientConnectionBase):
173+
class Http2ClientConnectionAsync(HttpClientConnectionAsyncUnified):
105174
"""
106175
An async HTTP/2 client connection.
107176
@@ -141,24 +210,12 @@ async def new(cls,
141210
socket_options,
142211
tls_connection_options,
143212
proxy_options,
144-
HttpVersion.Http2,
145-
initial_settings,
146-
on_remote_settings_changed,
213+
expected_version=HttpVersion.Http2,
214+
initial_settings=initial_settings,
215+
on_remote_settings_changed=on_remote_settings_changed,
147216
asyncio_connection=True)
148217
return await asyncio.wrap_future(future)
149218

150-
async def close(self) -> None:
151-
"""Close the connection asynchronously.
152-
153-
Shutdown is asynchronous. This call has no effect if the connection is already
154-
closing.
155-
156-
Returns:
157-
None: When shutdown is complete.
158-
"""
159-
_awscrt.http_connection_close(self._binding)
160-
await asyncio.wrap_future(self.shutdown_future)
161-
162219
def request(self,
163220
request: 'HttpRequest',
164221
async_body: AsyncIterator[bytes] = None,
@@ -177,7 +234,7 @@ def request(self,
177234
return Http2ClientStreamAsync(self, request, async_body, loop)
178235

179236

180-
class HttpClientStreamAsyncBase(HttClientStreamBase):
237+
class HttpClientStreamAsyncUnified(HttClientStreamBase):
181238
__slots__ = (
182239
'_response_status_future',
183240
'_response_headers_future',
@@ -188,10 +245,11 @@ class HttpClientStreamAsyncBase(HttClientStreamBase):
188245
'_status_code',
189246
'_loop')
190247

191-
def _init_common(self, connection: HttpClientConnectionBase,
192-
request: HttpRequest,
193-
async_body: AsyncIterator[bytes] = None,
194-
loop: Optional[asyncio.AbstractEventLoop] = None) -> None:
248+
def __init__(self,
249+
connection: HttpClientConnectionAsync,
250+
request: HttpRequest,
251+
async_body: AsyncIterator[bytes] = None,
252+
loop: Optional[asyncio.AbstractEventLoop] = None) -> None:
195253
# Initialize the parent class
196254
http2_manual_write = async_body is not None and connection.version is HttpVersion.Http2
197255
super()._init_common(connection, request, http2_manual_write=http2_manual_write)
@@ -300,8 +358,11 @@ async def wait_for_completion(self) -> int:
300358
"""
301359
return await self._completion_future
302360

361+
async def _set_async_body(self, body_iterator: AsyncIterator[bytes]):
362+
...
363+
303364

304-
class HttpClientStreamAsync(HttpClientStreamAsyncBase):
365+
class HttpClientStreamAsync(HttpClientStreamAsyncUnified):
305366
"""Async HTTP stream that sends a request and receives a response.
306367
307368
Create an HttpClientStreamAsync with `HttpClientConnectionAsync.request()`.
@@ -329,10 +390,10 @@ def __init__(self, connection: HttpClientConnectionAsync, request: HttpRequest,
329390
loop (Optional[asyncio.AbstractEventLoop]): Event loop to use for async operations.
330391
If None, the current event loop is used.
331392
"""
332-
super()._init_common(connection, request, loop=loop)
393+
super().__init__(connection, request, loop=loop)
333394

334395

335-
class Http2ClientStreamAsync(HttpClientStreamAsyncBase):
396+
class Http2ClientStreamAsync(HttpClientStreamAsyncUnified):
336397
"""HTTP/2 stream that sends a request and receives a response.
337398
338399
Create an Http2ClientStreamAsync with `Http2ClientConnectionAsync.request()`.
@@ -343,7 +404,7 @@ def __init__(self,
343404
request: HttpRequest,
344405
async_body: AsyncIterator[bytes] = None,
345406
loop: Optional[asyncio.AbstractEventLoop] = None) -> None:
346-
super()._init_common(connection, request, async_body=async_body, loop=loop)
407+
super().__init__(connection, request, async_body=async_body, loop=loop)
347408

348409
async def _write_data(self, body, end_stream):
349410
future: Future = Future()
@@ -362,16 +423,6 @@ def on_write_complete(error_code: int) -> None:
362423
await asyncio.wrap_future(future)
363424

364425
async def _set_async_body(self, body_iterator: AsyncIterator[bytes]):
365-
"""Write data to the stream asynchronously.
366-
367-
Args:
368-
data_stream (AsyncIterator[bytes]): Async iterator that yields bytes to write.
369-
Can be None to write an empty body, which is useful to finalize a request
370-
with end_stream=True.
371-
372-
Returns:
373-
None: When the write completes.
374-
"""
375426
try:
376427
async for chunk in body_iterator:
377428
await self._write_data(io.BytesIO(chunk), False)

0 commit comments

Comments
 (0)