28
28
import copy
29
29
import logging
30
30
31
- from typing import (TYPE_CHECKING , Generic , TypeVar , cast , IO , List , Union , Any , Mapping , Dict , Optional , # pylint: disable=unused-import
32
- Tuple , Callable , Iterator )
31
+ from typing import (
32
+ Generic ,
33
+ TypeVar ,
34
+ Union ,
35
+ Any ,
36
+ Dict ,
37
+ Optional ,
38
+ ) # pylint: disable=unused-import
39
+
40
+ try :
41
+ from typing import Awaitable # pylint: disable=unused-import
42
+ except ImportError :
43
+ pass
33
44
34
45
from azure .core .pipeline import ABC , PipelineRequest , PipelineResponse
35
46
47
+
36
48
HTTPResponseType = TypeVar ("HTTPResponseType" )
37
49
HTTPRequestType = TypeVar ("HTTPRequestType" )
38
50
39
51
_LOGGER = logging .getLogger (__name__ )
40
52
41
53
42
- class HTTPPolicy (ABC , Generic [HTTPRequestType , HTTPResponseType ]): # type: ignore
54
+ class HTTPPolicy (ABC , Generic [HTTPRequestType , HTTPResponseType ]): # type: ignore
43
55
"""An HTTP policy ABC.
44
56
45
57
Use with a synchronous pipeline.
@@ -48,6 +60,7 @@ class HTTPPolicy(ABC, Generic[HTTPRequestType, HTTPResponseType]): # type: ignor
48
60
instantiated and all policies chained.
49
61
:type next: ~azure.core.pipeline.policies.HTTPPolicy or ~azure.core.pipeline.transport.HTTPTransport
50
62
"""
63
+
51
64
def __init__ (self ):
52
65
self .next = None
53
66
@@ -64,6 +77,7 @@ def send(self, request):
64
77
:rtype: ~azure.core.pipeline.PipelineResponse
65
78
"""
66
79
80
+
67
81
class SansIOHTTPPolicy (Generic [HTTPRequestType , HTTPResponseType ]):
68
82
"""Represents a sans I/O policy.
69
83
@@ -72,18 +86,20 @@ class SansIOHTTPPolicy(Generic[HTTPRequestType, HTTPResponseType]):
72
86
on the specifics of any particular transport. SansIOHTTPPolicy
73
87
subclasses will function in either a Pipeline or an AsyncPipeline,
74
88
and can act either before the request is done, or after.
89
+ You can optionally make these methods coroutines (or return awaitable objects)
90
+ but they will then be tied to AsyncPipeline usage.
75
91
"""
76
92
77
93
def on_request (self , request ):
78
- # type: (PipelineRequest) -> None
94
+ # type: (PipelineRequest) -> Union[ None, Awaitable[None]]
79
95
"""Is executed before sending the request from next policy.
80
96
81
97
:param request: Request to be modified before sent from next policy.
82
98
:type request: ~azure.core.pipeline.PipelineRequest
83
99
"""
84
100
85
101
def on_response (self , request , response ):
86
- # type: (PipelineRequest, PipelineResponse) -> None
102
+ # type: (PipelineRequest, PipelineResponse) -> Union[ None, Awaitable[None]]
87
103
"""Is executed after the request comes back from the policy.
88
104
89
105
:param request: Request to be modified after returning from the policy.
@@ -92,9 +108,9 @@ def on_response(self, request, response):
92
108
:type response: ~azure.core.pipeline.PipelineResponse
93
109
"""
94
110
95
- #pylint: disable=no-self-use
96
- def on_exception (self , _request ): #pylint: disable=unused-argument
97
- # type: (PipelineRequest) -> bool
111
+ # pylint: disable=no-self-use
112
+ def on_exception (self , _request ): # pylint: disable=unused-argument
113
+ # type: (PipelineRequest) -> Union[ bool, Awaitable[bool]]
98
114
"""Is executed if an exception is raised while executing the next policy.
99
115
100
116
Developer can optionally implement this method to return True
@@ -129,6 +145,7 @@ class RequestHistory(object):
129
145
:param Exception error: An error encountered during the request, or None if the response was received successfully.
130
146
:param dict context: The pipeline context.
131
147
"""
148
+
132
149
def __init__ (self , http_request , http_response = None , error = None , context = None ):
133
150
# type: (PipelineRequest, Optional[PipelineResponse], Exception, Optional[Dict[str, Any]]) -> None
134
151
self .http_request = copy .deepcopy (http_request )
0 commit comments