|  | 
|  | 1 | +"""HTTP-based tap class for Singer SDK.""" | 
|  | 2 | + | 
|  | 3 | +from __future__ import annotations | 
|  | 4 | + | 
|  | 5 | +import typing as t | 
|  | 6 | + | 
|  | 7 | +import requests | 
|  | 8 | + | 
|  | 9 | +from singer_sdk.authenticators import NoopAuth | 
|  | 10 | +from singer_sdk.connectors.base import BaseConnector | 
|  | 11 | + | 
|  | 12 | +if t.TYPE_CHECKING: | 
|  | 13 | +    import sys | 
|  | 14 | + | 
|  | 15 | +    from requests.adapters import BaseAdapter | 
|  | 16 | + | 
|  | 17 | +    if sys.version_info >= (3, 10): | 
|  | 18 | +        from typing import TypeAlias  # noqa: ICN003 | 
|  | 19 | +    else: | 
|  | 20 | +        from typing_extensions import TypeAlias | 
|  | 21 | + | 
|  | 22 | +_Auth: TypeAlias = t.Callable[[requests.PreparedRequest], requests.PreparedRequest] | 
|  | 23 | + | 
|  | 24 | + | 
|  | 25 | +class HTTPConnector(BaseConnector[requests.Session]): | 
|  | 26 | +    """Base class for all HTTP-based connectors.""" | 
|  | 27 | + | 
|  | 28 | +    def __init__(self, config: t.Mapping[str, t.Any] | None) -> None: | 
|  | 29 | +        """Initialize the HTTP connector. | 
|  | 30 | +
 | 
|  | 31 | +        Args: | 
|  | 32 | +            config: Connector configuration parameters. | 
|  | 33 | +        """ | 
|  | 34 | +        super().__init__(config) | 
|  | 35 | +        self._session = self.get_session() | 
|  | 36 | +        self.refresh_auth() | 
|  | 37 | + | 
|  | 38 | +    def get_connection(self, *, authenticate: bool = True) -> requests.Session: | 
|  | 39 | +        """Return a new HTTP session object. | 
|  | 40 | +
 | 
|  | 41 | +        Adds adapters and optionally authenticates the session. | 
|  | 42 | +
 | 
|  | 43 | +        Args: | 
|  | 44 | +            authenticate: Whether to authenticate the request. | 
|  | 45 | +
 | 
|  | 46 | +        Returns: | 
|  | 47 | +            A new HTTP session object. | 
|  | 48 | +        """ | 
|  | 49 | +        for prefix, adapter in self.adapters.items(): | 
|  | 50 | +            self._session.mount(prefix, adapter) | 
|  | 51 | + | 
|  | 52 | +        self._session.auth = self._auth if authenticate else None | 
|  | 53 | + | 
|  | 54 | +        return self._session | 
|  | 55 | + | 
|  | 56 | +    def get_session(self) -> requests.Session: | 
|  | 57 | +        """Return a new HTTP session object. | 
|  | 58 | +
 | 
|  | 59 | +        Returns: | 
|  | 60 | +            A new HTTP session object. | 
|  | 61 | +        """ | 
|  | 62 | +        return requests.Session() | 
|  | 63 | + | 
|  | 64 | +    def get_authenticator(self) -> _Auth: | 
|  | 65 | +        """Authenticate the HTTP session. | 
|  | 66 | +
 | 
|  | 67 | +        Returns: | 
|  | 68 | +            An auth callable. | 
|  | 69 | +        """ | 
|  | 70 | +        return NoopAuth() | 
|  | 71 | + | 
|  | 72 | +    def refresh_auth(self) -> None: | 
|  | 73 | +        """Refresh the HTTP session authentication.""" | 
|  | 74 | +        self._auth = self.get_authenticator() | 
|  | 75 | + | 
|  | 76 | +    @property | 
|  | 77 | +    def adapters(self) -> dict[str, BaseAdapter]: | 
|  | 78 | +        """Return a mapping of URL prefixes to adapter objects. | 
|  | 79 | +
 | 
|  | 80 | +        Returns: | 
|  | 81 | +            A mapping of URL prefixes to adapter objects. | 
|  | 82 | +        """ | 
|  | 83 | +        return {} | 
|  | 84 | + | 
|  | 85 | +    @property | 
|  | 86 | +    def default_request_kwargs(self) -> dict[str, t.Any]: | 
|  | 87 | +        """Return default kwargs for HTTP requests. | 
|  | 88 | +
 | 
|  | 89 | +        Returns: | 
|  | 90 | +            A mapping of default kwargs for HTTP requests. | 
|  | 91 | +        """ | 
|  | 92 | +        return {} | 
|  | 93 | + | 
|  | 94 | +    def request( | 
|  | 95 | +        self, | 
|  | 96 | +        *args: t.Any, | 
|  | 97 | +        authenticate: bool = True, | 
|  | 98 | +        **kwargs: t.Any, | 
|  | 99 | +    ) -> requests.Response: | 
|  | 100 | +        """Make an HTTP request. | 
|  | 101 | +
 | 
|  | 102 | +        Args: | 
|  | 103 | +            *args: Positional arguments to pass to the request method. | 
|  | 104 | +            authenticate: Whether to authenticate the request. | 
|  | 105 | +            **kwargs: Keyword arguments to pass to the request method. | 
|  | 106 | +
 | 
|  | 107 | +        Returns: | 
|  | 108 | +            The HTTP response object. | 
|  | 109 | +        """ | 
|  | 110 | +        with self._connect(authenticate=authenticate) as session: | 
|  | 111 | +            kwargs = {**self.default_request_kwargs, **kwargs} | 
|  | 112 | +            return session.request(*args, **kwargs) | 
0 commit comments