@@ -47,6 +47,19 @@ class SimpleRequestsClient(HttpClient):
47
47
def __init__ (self , session : requests .Session = None ):
48
48
self .session = session or requests .Session ()
49
49
50
+ @staticmethod
51
+ def _get_destination_url (request : Request , server : str | None = None ) -> str :
52
+ if server :
53
+ # accepts "http://localhost:5000" or "localhost:5000"
54
+ if "://" in server :
55
+ parts = urlparse (server )
56
+ scheme , server = parts .scheme , parts .netloc
57
+ else :
58
+ scheme = request .scheme
59
+ return get_raw_current_url (scheme , server , request .root_path , get_raw_path (request ))
60
+
61
+ return get_raw_base_url (request )
62
+
50
63
def request (self , request : Request , server : str | None = None ) -> Response :
51
64
"""
52
65
Very naive implementation to make the given HTTP request using the requests library, i.e., process the request
@@ -61,16 +74,7 @@ def request(self, request: Request, server: str | None = None) -> Response:
61
74
:return: the response.
62
75
"""
63
76
64
- if server :
65
- # accepts "http://localhost:5000" or "localhost:5000"
66
- if "://" in server :
67
- parts = urlparse (server )
68
- scheme , server = parts .scheme , parts .netloc
69
- else :
70
- scheme = request .scheme
71
- url = get_raw_current_url (scheme , server , request .root_path , get_raw_path (request ))
72
- else :
73
- url = get_raw_base_url (request )
77
+ url = self ._get_destination_url (request , server )
74
78
75
79
response = self .session .request (
76
80
method = request .method ,
@@ -97,6 +101,53 @@ def close(self):
97
101
self .session .close ()
98
102
99
103
104
+ class SimpleStreamingRequestsClient (SimpleRequestsClient ):
105
+ def request (self , request : Request , server : str | None = None ) -> Response :
106
+ """
107
+ Very naive implementation to make the given HTTP request using the requests library, i.e., process the request
108
+ as a client.
109
+
110
+ :param request: the request to perform
111
+ :param server: the URL to send the request to, which defaults to the host component of the original Request.
112
+ :return: the response.
113
+ """
114
+
115
+ url = self ._get_destination_url (request , server )
116
+
117
+ response = self .session .request (
118
+ method = request .method ,
119
+ # use raw base url to preserve path url encoding
120
+ url = url ,
121
+ # request.args are only the url parameters
122
+ params = [(k , v ) for k , v in request .args .items (multi = True )],
123
+ headers = dict (request .headers .items ()),
124
+ data = restore_payload (request ),
125
+ stream = True ,
126
+ )
127
+
128
+ if request .method == "HEAD" :
129
+ # for HEAD requests we have to keep the original content-length, but it will be re-calculated when creating
130
+ # the final_response object
131
+ final_response = Response (
132
+ response = response .content ,
133
+ status = response .status_code ,
134
+ headers = Headers (dict (response .headers )),
135
+ )
136
+ final_response .content_length = response .headers .get ("Content-Length" , 0 )
137
+ return final_response
138
+
139
+ response_headers = Headers (dict (response .headers ))
140
+ response_headers .pop ("Content-Length" , None )
141
+
142
+ final_response = Response (
143
+ response = (chunk for chunk in response .raw .stream (1024 , decode_content = False )),
144
+ status = response .status_code ,
145
+ headers = response_headers ,
146
+ )
147
+
148
+ return final_response
149
+
150
+
100
151
def make_request (request : Request ) -> Response :
101
152
"""
102
153
Convenience method to make the given HTTP as a client.
0 commit comments