Skip to content

Commit

Permalink
Allow disabling auth per-request using auth=None (#1115)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom Christie <tom@tomchristie.com>
  • Loading branch information
florimondmanca and tomchristie authored Aug 11, 2020
1 parent db4e417 commit a4463d0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
42 changes: 22 additions & 20 deletions httpx/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,10 @@ def _merge_queryparams(
return merged_queryparams
return params

def _build_auth(self, request: Request, auth: AuthTypes = None) -> Auth:
auth = self.auth if auth is None else auth
def _build_auth(
self, request: Request, auth: typing.Union[AuthTypes, UnsetType] = UNSET
) -> Auth:
auth = self.auth if isinstance(auth, UnsetType) else auth

if auth is not None:
if isinstance(auth, tuple):
Expand Down Expand Up @@ -607,7 +609,7 @@ def request(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -646,7 +648,7 @@ def send(
request: Request,
*,
stream: bool = False,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -792,7 +794,7 @@ def get(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand All @@ -819,7 +821,7 @@ def options(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand All @@ -846,7 +848,7 @@ def head(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = False, # NOTE: Differs to usual default.
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -876,7 +878,7 @@ def post(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -909,7 +911,7 @@ def put(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -942,7 +944,7 @@ def patch(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -972,7 +974,7 @@ def delete(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1208,7 +1210,7 @@ async def request(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1248,7 +1250,7 @@ async def send(
request: Request,
*,
stream: bool = False,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1396,7 +1398,7 @@ async def get(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand All @@ -1423,7 +1425,7 @@ async def options(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand All @@ -1450,7 +1452,7 @@ async def head(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = False, # NOTE: Differs to usual default.
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1480,7 +1482,7 @@ async def post(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1513,7 +1515,7 @@ async def put(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1546,7 +1548,7 @@ async def patch(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1576,7 +1578,7 @@ async def delete(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: AuthTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down
1 change: 1 addition & 0 deletions httpx/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
Tuple[Union[str, bytes], Union[str, bytes]],
Callable[["Request"], "Request"],
"Auth",
None,
]

RequestData = Union[dict, str, bytes, Iterator[bytes], AsyncIterator[bytes]]
Expand Down
12 changes: 12 additions & 0 deletions tests/client/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ async def test_trust_env_auth() -> None:
}


@pytest.mark.asyncio
async def test_auth_disable_per_request() -> None:
url = "https://example.org/"
auth = ("tomchristie", "password123")

client = AsyncClient(transport=AsyncMockTransport(), auth=auth)
response = await client.get(url, auth=None)

assert response.status_code == 200
assert response.json() == {"auth": None}


def test_auth_hidden_url() -> None:
url = "http://example-username:example-password@example.org/"
expected = "URL('http://example-username:[secure]@example.org/')"
Expand Down

0 comments on commit a4463d0

Please sign in to comment.