-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrequest_adapter.py
148 lines (123 loc) · 5.62 KB
/
request_adapter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from abc import ABC, abstractmethod
from datetime import datetime
from io import BytesIO
from typing import Dict, Generic, List, Optional, TypeVar
from .request_information import RequestInformation
from .serialization import Parsable, ParsableFactory, SerializationWriterFactory
from .store import BackingStoreFactory
ResponseType = TypeVar("ResponseType", str, int, float, bool, datetime, BytesIO)
ModelType = TypeVar("ModelType", bound=Parsable)
RequestType = TypeVar("RequestType")
class RequestAdapter(ABC, Generic[ResponseType, ModelType, RequestType]):
"""Service responsible for translating abstract Request Info into concrete native HTTP requests.
"""
# The base url for every request.
base_url = str
@abstractmethod
def get_serialization_writer_factory(self) -> SerializationWriterFactory:
"""Gets the serialization writer factory currently in use for the HTTP core service.
Returns:
SerializationWriterFactory: the serialization writer factory currently in use for the
HTTP core service.
"""
pass
@abstractmethod
async def send_async(
self, request_info: RequestInformation, parsable_factory: ParsableFactory,
error_map: Dict[str, Optional[ParsableFactory]]
) -> Optional[ModelType]:
"""Excutes the HTTP request specified by the given RequestInformation and returns the
deserialized response model.
Args:
request_info (RequestInformation): the request info to execute.
parsable_factory (ParsableFactory): the class of response model to
deserialize the response into.
error_map (Dict[str, Optional[ParsableFactory]]): the error dict to use in case
of a failed request.
Returns:
ModelType: the deserialized response model.
"""
pass
@abstractmethod
async def send_collection_async(
self,
request_info: RequestInformation,
parsable_factory: ParsableFactory,
error_map: Dict[str, Optional[ParsableFactory]],
) -> Optional[List[ModelType]]:
"""Excutes the HTTP request specified by the given RequestInformation and returns the
deserialized response model collection.
Args:
request_info (RequestInformation): the request info to execute.
parsable_factory (ParsableFactory): the class of response model to
deserialize the response into.
error_map (Dict[str, Optional[ParsableFactory]]): the error dict to use in
case of a failed request.
Returns:
ModelType: the deserialized response model collection.
"""
pass
@abstractmethod
async def send_collection_of_primitive_async(
self,
request_info: RequestInformation,
response_type: ResponseType,
error_map: Dict[str, Optional[ParsableFactory]],
) -> Optional[List[ResponseType]]:
"""Excutes the HTTP request specified by the given RequestInformation and returns the
deserialized response model collection.
Args:
request_info (RequestInformation): the request info to execute.
response_type (ResponseType): the class of the response model to deserialize the
response into.
error_map (Dict[str, Optional[ParsableFactory]]): the error dict to use in
case of a failed request.
Returns:
Optional[List[ModelType]]: The deserialized response model collection.
"""
pass
@abstractmethod
async def send_primitive_async(
self, request_info: RequestInformation, response_type: ResponseType,
error_map: Dict[str, Optional[ParsableFactory]]
) -> Optional[ResponseType]:
"""Excutes the HTTP request specified by the given RequestInformation and returns the
deserialized primitive response model.
Args:
request_info (RequestInformation): the request info to execute.
response_type (ResponseType): the class of the response model to deserialize the
response into.
error_map (Dict[str, Optional[ParsableFactory]]): the error dict to use in
case of a failed request.
Returns:
ResponseType: the deserialized primitive response model.
"""
pass
@abstractmethod
async def send_no_response_content_async(
self, request_info: RequestInformation, error_map: Dict[str, Optional[ParsableFactory]]
) -> None:
"""Excutes the HTTP request specified by the given RequestInformation and returns the
deserialized primitive response model.
Args:
request_info (RequestInformation):the request info to execute.
error_map (Dict[str, Optional[Optional[ParsableFactory]]): the error dict to use in
case of a failed request.
"""
pass
@abstractmethod
def enable_backing_store(self, backing_store_factory: Optional[BackingStoreFactory]) -> None:
"""Enables the backing store proxies for the SerializationWriters and ParseNodes in use.
Args:
backing_store_factory (Optional[BackingStoreFactory]): the backing store factory to use.
"""
pass
@abstractmethod
async def convert_to_native_async(self, request_info: RequestInformation) -> RequestType:
"""Translates the request information object into a native HTTP client request object.
Args:
request_info (RequestInformation): request information object to be converted.
Returns:
RequestType: the natively typed HTTP request of the client.
"""
pass