Tighten public client methods (#997)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent 09672a9 · Browse files · 2 files·+39 added−40 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
Diff stats
2 files+39 added−40 removed
- tests:+3 added−4 removed
- other:+36 added−36 removed
Provenance
- Commit
e107e0f- Tree
b5ae087- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
09672a9
2 files changed+39 added−40 removed
modified:httpx/_client.py+36 added-36 removed
181 unmodified lines182 ) -> Request:183 """184 Build and return a request instance.185 """186- url = self.merge_url(url)187- headers = self.merge_headers(headers)188- cookies = self.merge_cookies(cookies)189- params = self.merge_queryparams(params)186+ url = self._merge_url(url)187+ headers = self._merge_headers(headers)188+ cookies = self._merge_cookies(cookies)189+ params = self._merge_queryparams(params)190 return Request(191 method,192 url,193 data=data,3 unmodified lines197 headers=headers,198 cookies=cookies,199 )200 201- def merge_url(self, url: URLTypes) -> URL:201+ def _merge_url(self, url: URLTypes) -> URL:202 """203 Merge a URL argument together with any 'base_url' on the client,204 to create the URL used for the outgoing request.205 """2 unmodified lines208 port = None if url.port == 80 else url.port209 url = url.copy_with(scheme="https", port=port)210 return url211 212- def merge_cookies(212+ def _merge_cookies(213 self, cookies: CookieTypes = None214 ) -> typing.Optional[CookieTypes]:215 """216 Merge a cookies argument together with any cookies on the client,4 unmodified lines221 merged_cookies.update(cookies)222 return merged_cookies223 return cookies224 225- def merge_headers(225+ def _merge_headers(226 self, headers: HeaderTypes = None227 ) -> typing.Optional[HeaderTypes]:228 """229 Merge a headers argument together with any headers on the client,4 unmodified lines234 merged_headers.update(headers)235 return merged_headers236 return headers237 238- def merge_queryparams(238+ def _merge_queryparams(239 self, params: QueryParamTypes = None240 ) -> typing.Optional[QueryParamTypes]:241 """242 Merge a queryparams argument together with any queryparams on the client,4 unmodified lines247 merged_queryparams.update(params)248 return merged_queryparams249 return params250 251- def build_auth(self, request: Request, auth: AuthTypes = None) -> Auth:251+ def _build_auth(self, request: Request, auth: AuthTypes = None) -> Auth:252 auth = self.auth if auth is None else auth253 254 if auth is not None:255 if isinstance(auth, tuple):14 unmodified lines270 return BasicAuth(username=credentials[0], password=credentials[1])271 272 return Auth()273 274- def build_redirect_request(self, request: Request, response: Response) -> Request:274+ def _build_redirect_request(self, request: Request, response: Response) -> Request:275 """276 Given a request and a redirect response, return a new request that277 should be used to effect the redirect.278 """279- method = self.redirect_method(request, response)280- url = self.redirect_url(request, response)281- headers = self.redirect_headers(request, url, method)282- stream = self.redirect_stream(request, method)279+ method = self._redirect_method(request, response)280+ url = self._redirect_url(request, response)281+ headers = self._redirect_headers(request, url, method)282+ stream = self._redirect_stream(request, method)283 cookies = Cookies(self.cookies)284 return Request(285 method=method, url=url, headers=headers, cookies=cookies, stream=stream286 )287 288- def redirect_method(self, request: Request, response: Response) -> str:288+ def _redirect_method(self, request: Request, response: Response) -> str:289 """290 When being redirected we may want to change the method of the request291 based on certain specs or browser behavior.292 """14 unmodified lines307 method = "GET"308 309 return method310 311- def redirect_url(self, request: Request, response: Response) -> URL:311+ def _redirect_url(self, request: Request, response: Response) -> URL:312 """313 Return the URL for the redirect to follow.314 """315 location = response.headers["Location"]19 unmodified lines335 url = url.copy_with(fragment=request.url.fragment)336 337 return url338 339- def redirect_headers(self, request: Request, url: URL, method: str) -> Headers:339+ def _redirect_headers(self, request: Request, url: URL, method: str) -> Headers:340 """341 Return the headers that should be used for the redirect request.342 """343 headers = Headers(request.headers)15 unmodified lines359 headers.pop("Cookie", None)360 361 return headers362 363- def redirect_stream(363+ def _redirect_stream(364 self, request: Request, method: str365 ) -> typing.Optional[ContentStream]:366 """367 Return the body that should be used for the redirect request.228 unmodified lines596 raise InvalidURL('URL scheme must be "http" or "https".')597 598 timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout)599 600- auth = self.build_auth(request, auth)600+ auth = self._build_auth(request, auth)601 602- response = self.send_handling_redirects(602+ response = self._send_handling_redirects(603 request, auth=auth, timeout=timeout, allow_redirects=allow_redirects,604 )605 606 if not stream:3 unmodified lines610 response.close()611 612 return response613 614- def send_handling_redirects(614+ def _send_handling_redirects(615 self,616 request: Request,617 auth: Auth,618 timeout: Timeout,6 unmodified lines625 while True:626 if len(history) > self.max_redirects:627 raise TooManyRedirects()628 629- response = self.send_handling_auth(629+ response = self._send_handling_auth(630 request, auth=auth, timeout=timeout, history=history631 )632 response.history = list(history)633 1 unmodified line635 return response636 637 if allow_redirects:638 response.read()639- request = self.build_redirect_request(request, response)639+ request = self._build_redirect_request(request, response)640 history = history + [response]641 642 if not allow_redirects:643 response.call_next = functools.partial(644- self.send_handling_redirects,644+ self._send_handling_redirects,645 request=request,646 auth=auth,647 timeout=timeout,648 allow_redirects=False,649 history=history,650 )651 return response652 653- def send_handling_auth(653+ def _send_handling_auth(654 self,655 request: Request,656 history: typing.List[Response],657 auth: Auth,4 unmodified lines662 663 auth_flow = auth.auth_flow(request)664 request = next(auth_flow)665 while True:666- response = self.send_single_request(request, timeout)666+ response = self._send_single_request(request, timeout)667 if auth.requires_response_body:668 response.read()669 try:670 next_request = auth_flow.send(response)7 unmodified lines678 response.read()679 request = next_request680 history.append(response)681 682- def send_single_request(self, request: Request, timeout: Timeout) -> Response:682+ def _send_single_request(self, request: Request, timeout: Timeout) -> Response:683 """684 Sends a single request, without handling any redirections.685 """686 448 unmodified lines1135 raise InvalidURL('URL scheme must be "http" or "https".')1136 1137 timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout)1138 1139- auth = self.build_auth(request, auth)1139+ auth = self._build_auth(request, auth)1140 1141- response = await self.send_handling_redirects(1141+ response = await self._send_handling_redirects(1142 request, auth=auth, timeout=timeout, allow_redirects=allow_redirects,1143 )1144 1145 if not stream:3 unmodified lines1149 await response.aclose()1150 1151 return response1152 1153- async def send_handling_redirects(1153+ async def _send_handling_redirects(1154 self,1155 request: Request,1156 auth: Auth,1157 timeout: Timeout,6 unmodified lines1164 while True:1165 if len(history) > self.max_redirects:1166 raise TooManyRedirects()1167 1168- response = await self.send_handling_auth(1168+ response = await self._send_handling_auth(1169 request, auth=auth, timeout=timeout, history=history1170 )1171 response.history = list(history)1172 1 unmodified line1174 return response1175 1176 if allow_redirects:1177 await response.aread()1178- request = self.build_redirect_request(request, response)1178+ request = self._build_redirect_request(request, response)1179 history = history + [response]1180 1181 if not allow_redirects:1182 response.call_next = functools.partial(1183- self.send_handling_redirects,1183+ self._send_handling_redirects,1184 request=request,1185 auth=auth,1186 timeout=timeout,1187 allow_redirects=False,1188 history=history,1189 )1190 return response1191 1192- async def send_handling_auth(1192+ async def _send_handling_auth(1193 self,1194 request: Request,1195 history: typing.List[Response],1196 auth: Auth,4 unmodified lines1201 1202 auth_flow = auth.auth_flow(request)1203 request = next(auth_flow)1204 while True:1205- response = await self.send_single_request(request, timeout)1205+ response = await self._send_single_request(request, timeout)1206 if auth.requires_response_body:1207 await response.aread()1208 try:1209 next_request = auth_flow.send(response)7 unmodified lines1217 await response.aread()1218 request = next_request1219 history.append(response)1220 1221- async def send_single_request(1221+ async def _send_single_request(1222 self, request: Request, timeout: Timeout,1223 ) -> Response:1224 """1225 Sends a single request, without handling any redirections.290 unmodified linesmodified:tests/client/test_client.py+3 added-4 removed
157 unmodified lines158 159 160 def test_merge_url():161 client = httpx.Client(base_url="https://www.paypal.com/")162- url = client.merge_url("http://www.paypal.com")163-164- assert url.scheme == "https"165- assert url.is_ssl162+ request = client.build_request("GET", "http://www.paypal.com")163+ assert request.url.scheme == "https"164+ assert request.url.is_ssl