Skip to content

Commit

Permalink
Reworked to support PathLike protocol instead of pathlib.Path
Browse files Browse the repository at this point in the history
  • Loading branch information
wallseat committed Dec 29, 2024
1 parent 0d89df0 commit 6751edd
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/niquests/_typing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from pathlib import Path
from os import PathLike
import typing
from http.cookiejar import CookieJar

Expand Down Expand Up @@ -72,7 +72,7 @@
CookieJar,
]
#: Either Yes/No, or CA bundle pem location. Or directly the raw bundle content itself.
TLSVerifyType: typing.TypeAlias = typing.Union[bool, str, bytes, Path]
TLSVerifyType: typing.TypeAlias = typing.Union[bool, str, bytes, PathLike]
#: Accept a pem certificate (concat cert, key) or an explicit tuple of cert, key pair with an optional password.
TLSClientCertType: typing.TypeAlias = typing.Union[
str, typing.Tuple[str, str], typing.Tuple[str, str, str]
Expand Down
52 changes: 33 additions & 19 deletions src/niquests/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from __future__ import annotations

import os.path
from pathlib import Path
import socket # noqa: F401
import sys
import time
Expand Down Expand Up @@ -211,8 +210,9 @@ def send(
data before giving up, as a float, or a :ref:`(connect timeout,
read timeout) <timeouts>` tuple.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string/pathlib.Path, in which case it must be a path
to a CA bundle to use. It is also possible to put the certificates (directly) in a string or bytes.
the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
It is also possible to put the certificates (directly) in a string or bytes.
:param cert: (optional) Any user-provided SSL certificate to be trusted.
:param proxies: (optional) The proxies dictionary to apply to the request.
:param on_post_connection: (optional) A callable that should be invoked just after the pool mgr picked up a live
Expand Down Expand Up @@ -268,8 +268,9 @@ async def send(
data before giving up, as a float, or a :ref:`(connect timeout,
read timeout) <timeouts>` tuple.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string/pathlib.Path, in which case it must be a path
to a CA bundle to use. It is also possible to put the certificates (directly) in a string or bytes.
the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
It is also possible to put the certificates (directly) in a string or bytes.
:param cert: (optional) Any user-provided SSL certificate to be trusted.
:param proxies: (optional) The proxies dictionary to apply to the request.
:param on_post_connection: (optional) A callable that should be invoked just after the pool mgr picked up a live
Expand Down Expand Up @@ -555,8 +556,9 @@ def cert_verify(
:param conn: The urllib3 connection object associated with the cert.
:param url: The requested URL.
:param verify: Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string/pathlib.Path, in which case it must be a path
to a CA bundle to use. It is also possible to put the certificates (directly) in a string or bytes.
the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
It is also possible to put the certificates (directly) in a string or bytes.
:param cert: The SSL certificate to verify.
"""
if not parse_scheme(url) == "https":
Expand Down Expand Up @@ -589,10 +591,16 @@ def cert_verify(
cert_data = verify.decode("utf-8")
else:
# Allow self-specified cert location.
# Plain str path
if isinstance(verify, str):
cert_loc = verify
elif isinstance(verify, Path):
cert_loc = verify.absolute().as_posix()
# or path-like obj, that should have __fspath__
elif hasattr(verify, "__fspath__"):
verify_pathlike = typing.cast(os.PathLike, verify)
cert_loc = verify_pathlike.__fspath__()

if isinstance(cert_loc, bytes):
cert_loc = cert_loc.decode()

if isinstance(cert_loc, str) and not os.path.exists(cert_loc):
raise OSError(
Expand Down Expand Up @@ -852,9 +860,10 @@ def send(
data before giving up, as a float, or a :ref:`(connect timeout,
read timeout) <timeouts>` tuple.
:param verify: (optional) Either a boolean, in which case it controls whether
we verify the server's TLS certificate, or a string/pathlib.Path, in which case it
must be a path to a CA bundle to use. It is also possible to put the certificates
(directly) in a string or bytes.
we verify the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
Defaults to ``True``.
It is also possible to put the certificates (directly) in a string or bytes.
:param cert: (optional) Any user-provided SSL certificate to be trusted.
:param proxies: (optional) The proxies dictionary to apply to the request.
:param on_post_connection: (optional) A callable that contain a single positional argument for newly acquired
Expand Down Expand Up @@ -1648,8 +1657,9 @@ def cert_verify(
:param conn: The urllib3 connection object associated with the cert.
:param url: The requested URL.
:param verify: Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string/pathlib.Path, in which case it must be a path
to a CA bundle to use. It is also possible to put the certificates (directly) in a string or bytes.
the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
It is also possible to put the certificates (directly) in a string or bytes.
:param cert: The SSL certificate to verify.
"""
if not parse_scheme(url) == "https":
Expand Down Expand Up @@ -1685,8 +1695,12 @@ def cert_verify(
if isinstance(verify, str):
cert_loc = verify

elif isinstance(verify, Path):
cert_loc = verify.absolute().as_posix()
elif hasattr(verify, "__fspath__"):
verify_pathlike = typing.cast(os.PathLike, verify)
cerl_loc = verify_pathlike.__fspath__()

if isinstance(cerl_loc, bytes):
cert_loc = cerl_loc.decode()

if isinstance(cert_loc, str) and not os.path.exists(cert_loc):
raise OSError(
Expand Down Expand Up @@ -1941,9 +1955,9 @@ async def send(
data before giving up, as a float, or a :ref:`(connect timeout,
read timeout) <timeouts>` tuple.
:param verify: (optional) Either a boolean, in which case it controls whether
we verify the server's TLS certificate, or a string/pathlib.Path, in which case it
must be a path to a CA bundle to use. It is also possible to put the certificates
(directly) in a string or bytes.
we verify the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
It is also possible to put the certificates (directly) in a string or bytes.
:param cert: (optional) Any user-provided SSL certificate to be trusted.
:param proxies: (optional) The proxies dictionary to apply to the request.
:param on_post_connection: (optional) A callable that contain a single positional argument for newly acquired
Expand Down
42 changes: 25 additions & 17 deletions src/niquests/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ def request(
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string/pathlib.Path, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
Defaults to ``True``.
It is also possible to put the certificates (directly) in a string or bytes.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair, or ('cert', 'key', 'key_password').
Expand Down Expand Up @@ -157,8 +158,9 @@ def get(
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string/pathlib.Path, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
Defaults to ``True``.
It is also possible to put the certificates (directly) in a string or bytes.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair, or ('cert', 'key', 'key_password').
Expand Down Expand Up @@ -218,8 +220,9 @@ def options(
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string/pathlib.Path, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
Defaults to ``True``.
It is also possible to put the certificates (directly) in a string or bytes.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair, or ('cert', 'key', 'key_password').
Expand Down Expand Up @@ -280,8 +283,9 @@ def head(
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string/pathlib.Path, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
Defaults to ``True``.
It is also possible to put the certificates (directly) in a string or bytes.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair, or ('cert', 'key', 'key_password').
Expand Down Expand Up @@ -352,8 +356,9 @@ def post(
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string/pathlib.Path, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
Defaults to ``True``.
It is also possible to put the certificates (directly) in a string or bytes.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair, or ('cert', 'key', 'key_password').
Expand Down Expand Up @@ -426,8 +431,9 @@ def put(
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string/pathlib.Path, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
Defaults to ``True``.
It is also possible to put the certificates (directly) in a string or bytes.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair, or ('cert', 'key', 'key_password').
Expand Down Expand Up @@ -500,8 +506,9 @@ def patch(
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string/pathlib.Path, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
Defaults to ``True``.
It is also possible to put the certificates (directly) in a string or bytes.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair, or ('cert', 'key', 'key_password').
Expand Down Expand Up @@ -563,9 +570,10 @@ def delete(
timeout) <timeouts>` tuple.
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string/pathlib.Path, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
::param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a path passed as a string or os.Pathlike object,
in which case it must be a path to a CA bundle to use.
Defaults to ``True``.
It is also possible to put the certificates (directly) in a string or bytes.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair, or ('cert', 'key', 'key_password').
Expand Down
Loading

0 comments on commit 6751edd

Please sign in to comment.