75
75
Sequence [Tuple [str , FileType ]]
76
76
]
77
77
78
- from azure .core .pipeline import Pipeline
78
+ from azure .core .pipeline import Pipeline , AsyncPipeline
79
79
from azure .core .pipeline .transport import (
80
80
HttpRequest as _PipelineTransportHttpRequest ,
81
81
)
@@ -214,7 +214,7 @@ def _parse_lines_from_text(text):
214
214
class _StreamContextManagerBase :
215
215
def __init__ (
216
216
self ,
217
- client : Union [_PipelineClient , _AsyncPipelineClient ],
217
+ pipeline : Union [Pipeline , AsyncPipeline ],
218
218
request : "HttpRequest" ,
219
219
** kwargs
220
220
):
@@ -226,7 +226,7 @@ def __init__(
226
226
227
227
Heavily inspired from httpx, we want the same behavior for it to feel consistent for users
228
228
"""
229
- self .client = client
229
+ self .pipeline = pipeline
230
230
self .request = request
231
231
self .kwargs = kwargs
232
232
@@ -237,7 +237,7 @@ def close(self):
237
237
class _StreamContextManager (_StreamContextManagerBase ):
238
238
def __enter__ (self ) -> "HttpResponse" :
239
239
"""Actually make the call only when we enter. For sync stream_response calls"""
240
- pipeline_transport_response = self .client . _pipeline .run (
240
+ pipeline_transport_response = self .pipeline .run (
241
241
self .request ._internal_request ,
242
242
stream = True ,
243
243
** self .kwargs
@@ -258,12 +258,12 @@ def close(self):
258
258
class _AsyncStreamContextManager (_StreamContextManagerBase ):
259
259
async def __aenter__ (self ) -> "AsyncHttpResponse" :
260
260
"""Actually make the call only when we enter. For async stream_response calls."""
261
- if not isinstance (self .client , _AsyncPipelineClient ):
261
+ if not isinstance (self .pipeline , AsyncPipeline ):
262
262
raise TypeError (
263
- "Only sync calls should enter here. If you mean to do a sync call, "
263
+ "Only async calls should enter here. If you mean to do a sync call, "
264
264
"make sure to use 'with' instead."
265
265
)
266
- pipeline_transport_response = (await self .client . _pipeline .run (
266
+ pipeline_transport_response = (await self .pipeline .run (
267
267
self .request ._internal_request ,
268
268
stream = True ,
269
269
** self .kwargs
@@ -446,11 +446,6 @@ def reason(self) -> str:
446
446
"""Returns the reason phrase for the response"""
447
447
return self ._internal_response .reason
448
448
449
- @property
450
- def content (self ) -> bytes :
451
- """Returns the response content in bytes"""
452
- raise NotImplementedError ()
453
-
454
449
@property
455
450
def url (self ) -> str :
456
451
"""Returns the URL that resulted in this response"""
@@ -529,6 +524,14 @@ def raise_for_status(self) -> None:
529
524
if self .status_code >= 400 :
530
525
raise HttpResponseError (response = self )
531
526
527
+ @property
528
+ def content (self ) -> bytes :
529
+ """Return the response's content in bytes."""
530
+ try :
531
+ return self ._content
532
+ except AttributeError :
533
+ raise ResponseNotReadError ()
534
+
532
535
def __repr__ (self ) -> str :
533
536
content_type_str = (
534
537
", Content-Type: {}" .format (self .content_type ) if self .content_type else ""
@@ -545,19 +548,12 @@ def _validate_streaming_access(self) -> None:
545
548
546
549
class HttpResponse (_HttpResponseBase ):
547
550
548
- @property
549
- def content (self ):
550
- # type: (...) -> bytes
551
- try :
552
- return self ._content
553
- except AttributeError :
554
- raise ResponseNotReadError ()
555
-
556
551
def close (self ) -> None :
557
552
self .is_closed = True
558
553
self ._internal_response .internal_response .close ()
559
554
560
555
def __exit__ (self , * args ) -> None :
556
+ self .is_closed = True
561
557
self ._internal_response .internal_response .__exit__ (* args )
562
558
563
559
def read (self ) -> bytes :
@@ -619,13 +615,6 @@ def iter_raw(self, chunk_size: int = None) -> Iterator[bytes]:
619
615
620
616
class AsyncHttpResponse (_HttpResponseBase ):
621
617
622
- @property
623
- def content (self ) -> bytes :
624
- try :
625
- return self ._content
626
- except AttributeError :
627
- raise ResponseNotReadError ()
628
-
629
618
async def _close_stream (self ) -> None :
630
619
self .is_stream_consumed = True
631
620
await self .close ()
@@ -686,6 +675,7 @@ async def close(self) -> None:
686
675
await asyncio .sleep (0 )
687
676
688
677
async def __aexit__ (self , * args ) -> None :
678
+ self .is_closed = True
689
679
await self ._internal_response .internal_response .__aexit__ (* args )
690
680
691
681
0 commit comments