Skip to content
Myr

Allow disabling auth per-request using `auth=None` (#1115)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Florimond Manca · committed by GitHub · parent db4e417 · Browse files · 3 files+35 added−20 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: Tom Christie <tom@tomchristie.com>

Diff stats

3 files+35 added−20 removed

  • tests:+12 added−0 removed
  • other:+23 added−20 removed

3 files+35 added−20 removed

modified:httpx/_client.py+22 added-20 removed
282 unmodified lines
283 merged_queryparams.update(params)284 return merged_queryparams285 return params286287 def _build_auth(self, request: Request, auth: AuthTypes = None) -> Auth:288 auth = self.auth if auth is None else auth287 def _build_auth(288 self, request: Request, auth: typing.Union[AuthTypes, UnsetType] = UNSET289 ) -> Auth:290 auth = self.auth if isinstance(auth, UnsetType) else auth291292 if auth is not None:293 if isinstance(auth, tuple):294 return BasicAuth(username=auth[0], password=auth[1])
313 unmodified lines
608 json: typing.Any = None,609 params: QueryParamTypes = None,610 headers: HeaderTypes = None,611 cookies: CookieTypes = None,610 auth: AuthTypes = None,612 auth: typing.Union[AuthTypes, UnsetType] = UNSET,613 allow_redirects: bool = True,614 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,615 ) -> Response:616 """
30 unmodified lines
647 self,648 request: Request,649 *,650 stream: bool = False,649 auth: AuthTypes = None,651 auth: typing.Union[AuthTypes, UnsetType] = UNSET,652 allow_redirects: bool = True,653 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,654 ) -> Response:655 """
137 unmodified lines
793 *,794 params: QueryParamTypes = None,795 headers: HeaderTypes = None,796 cookies: CookieTypes = None,795 auth: AuthTypes = None,797 auth: typing.Union[AuthTypes, UnsetType] = UNSET,798 allow_redirects: bool = True,799 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,800 ) -> Response:801 """
18 unmodified lines
820 *,821 params: QueryParamTypes = None,822 headers: HeaderTypes = None,823 cookies: CookieTypes = None,822 auth: AuthTypes = None,824 auth: typing.Union[AuthTypes, UnsetType] = UNSET,825 allow_redirects: bool = True,826 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,827 ) -> Response:828 """
18 unmodified lines
847 *,848 params: QueryParamTypes = None,849 headers: HeaderTypes = None,850 cookies: CookieTypes = None,849 auth: AuthTypes = None,851 auth: typing.Union[AuthTypes, UnsetType] = UNSET,852 allow_redirects: bool = False, # NOTE: Differs to usual default.853 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,854 ) -> Response:855 """
21 unmodified lines
877 json: typing.Any = None,878 params: QueryParamTypes = None,879 headers: HeaderTypes = None,880 cookies: CookieTypes = None,879 auth: AuthTypes = None,881 auth: typing.Union[AuthTypes, UnsetType] = UNSET,882 allow_redirects: bool = True,883 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,884 ) -> Response:885 """
24 unmodified lines
910 json: typing.Any = None,911 params: QueryParamTypes = None,912 headers: HeaderTypes = None,913 cookies: CookieTypes = None,912 auth: AuthTypes = None,914 auth: typing.Union[AuthTypes, UnsetType] = UNSET,915 allow_redirects: bool = True,916 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,917 ) -> Response:918 """
24 unmodified lines
943 json: typing.Any = None,944 params: QueryParamTypes = None,945 headers: HeaderTypes = None,946 cookies: CookieTypes = None,945 auth: AuthTypes = None,947 auth: typing.Union[AuthTypes, UnsetType] = UNSET,948 allow_redirects: bool = True,949 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,950 ) -> Response:951 """
21 unmodified lines
973 *,974 params: QueryParamTypes = None,975 headers: HeaderTypes = None,976 cookies: CookieTypes = None,975 auth: AuthTypes = None,977 auth: typing.Union[AuthTypes, UnsetType] = UNSET,978 allow_redirects: bool = True,979 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,980 ) -> Response:981 """
227 unmodified lines
1209 json: typing.Any = None,1210 params: QueryParamTypes = None,1211 headers: HeaderTypes = None,1212 cookies: CookieTypes = None,1211 auth: AuthTypes = None,1213 auth: typing.Union[AuthTypes, UnsetType] = UNSET,1214 allow_redirects: bool = True,1215 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1216 ) -> Response:1217 """
31 unmodified lines
1249 self,1250 request: Request,1251 *,1252 stream: bool = False,1251 auth: AuthTypes = None,1253 auth: typing.Union[AuthTypes, UnsetType] = UNSET,1254 allow_redirects: bool = True,1255 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1256 ) -> Response:1257 """
139 unmodified lines
1397 *,1398 params: QueryParamTypes = None,1399 headers: HeaderTypes = None,1400 cookies: CookieTypes = None,1399 auth: AuthTypes = None,1401 auth: typing.Union[AuthTypes, UnsetType] = UNSET,1402 allow_redirects: bool = True,1403 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1404 ) -> Response:1405 """
18 unmodified lines
1424 *,1425 params: QueryParamTypes = None,1426 headers: HeaderTypes = None,1427 cookies: CookieTypes = None,1426 auth: AuthTypes = None,1428 auth: typing.Union[AuthTypes, UnsetType] = UNSET,1429 allow_redirects: bool = True,1430 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1431 ) -> Response:1432 """
18 unmodified lines
1451 *,1452 params: QueryParamTypes = None,1453 headers: HeaderTypes = None,1454 cookies: CookieTypes = None,1453 auth: AuthTypes = None,1455 auth: typing.Union[AuthTypes, UnsetType] = UNSET,1456 allow_redirects: bool = False, # NOTE: Differs to usual default.1457 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1458 ) -> Response:1459 """
21 unmodified lines
1481 json: typing.Any = None,1482 params: QueryParamTypes = None,1483 headers: HeaderTypes = None,1484 cookies: CookieTypes = None,1483 auth: AuthTypes = None,1485 auth: typing.Union[AuthTypes, UnsetType] = UNSET,1486 allow_redirects: bool = True,1487 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1488 ) -> Response:1489 """
24 unmodified lines
1514 json: typing.Any = None,1515 params: QueryParamTypes = None,1516 headers: HeaderTypes = None,1517 cookies: CookieTypes = None,1516 auth: AuthTypes = None,1518 auth: typing.Union[AuthTypes, UnsetType] = UNSET,1519 allow_redirects: bool = True,1520 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1521 ) -> Response:1522 """
24 unmodified lines
1547 json: typing.Any = None,1548 params: QueryParamTypes = None,1549 headers: HeaderTypes = None,1550 cookies: CookieTypes = None,1549 auth: AuthTypes = None,1551 auth: typing.Union[AuthTypes, UnsetType] = UNSET,1552 allow_redirects: bool = True,1553 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1554 ) -> Response:1555 """
21 unmodified lines
1577 *,1578 params: QueryParamTypes = None,1579 headers: HeaderTypes = None,1580 cookies: CookieTypes = None,1579 auth: AuthTypes = None,1581 auth: typing.Union[AuthTypes, UnsetType] = UNSET,1582 allow_redirects: bool = True,1583 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1584 ) -> Response:1585 """
95 unmodified lines
modified:httpx/_types.py+1 added
58 unmodified lines
59AuthTypes = Union[60 Tuple[Union[str, bytes], Union[str, bytes]],61 Callable[["Request"], "Request"],62 "Auth",63 None,64]6566RequestData = Union[dict, str, bytes, Iterator[bytes], AsyncIterator[bytes]]67
10 unmodified lines
modified:tests/client/test_auth.py+12 added
277 unmodified lines
278 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"279 }280281282@pytest.mark.asyncio283async def test_auth_disable_per_request() -> None:284 url = "https://example.org/"285 auth = ("tomchristie", "password123")286287 client = AsyncClient(transport=AsyncMockTransport(), auth=auth)288 response = await client.get(url, auth=None)289290 assert response.status_code == 200291 assert response.json() == {"auth": None}292293294def test_auth_hidden_url() -> None:295 url = "http://example-username:example-password@example.org/"296 expected = "URL('http://example-username:[secure]@example.org/')"297 assert url == URL(url)
325 unmodified lines