Skip to content

rnet.redirect

Configuration for handling HTTP redirects.

rnet.redirect

Policy

Represents the redirect policy for HTTP requests.

The default value will catch redirect loops, and has a maximum of 10 redirects it will follow in a chain before returning an error.

Source code in rnet/redirect.py
@final
class Policy:
    """
    Represents the redirect policy for HTTP requests.

    The default value will catch redirect loops, and has a maximum of 10
    redirects it will follow in a chain before returning an error.
    """

    """
    Create a default Policy instance.
    """

    @staticmethod
    def limited(max: int | None = None) -> "Policy":
        """
        Create a Policy with a maximum number of redirects.

        An error will be returned if the max is reached.

        Args:
            max: Maximum number of redirects to follow

        Returns:
            Policy: A redirect policy with the specified limit

        Example:
            ```python
            import rnet
            from rnet import Client, redirect
            policy = redirect.Policy.limited(5)
            client = Client(redirect=policy)
            ```
        """
        ...

    @staticmethod
    def none() -> "Policy":
        """
        Create a Policy that does not follow any redirect.

        Returns:
            Policy: A redirect policy that doesn't follow redirects

        Example:
            ```python
            from rnet import Client, redirect

            policy = redirect.Policy.none()
            client = Client(redirect=policy)
            ```
        """
        ...

    @staticmethod
    def custom(callback: Callable[["Attempt"], "Action"]) -> "Policy":
        """
        Create a custom Policy using the passed function.

        Args:
            callback: A callable that takes an Attempt and returns an Action

        Returns
            Policy: A custom redirect policy

        Example:
            ```python
            from rnet import Client, redirect

            def policy(attempt: redirect.Attempt) -> redirect.Action:
                if len(attempt.previous) > 5:
                    return attempt.error("too many redirects")
                elif "example.com" in attempt.uri:
                    return attempt.stop()
                else:
                    return attempt.follow()

            policy = redirect.Policy.custom(policy)
            client = Client(redirect=policy)
            ```
        """
        ...

    def __str__(self) -> str: ...

limited staticmethod

limited(max=None)

Create a Policy with a maximum number of redirects.

An error will be returned if the max is reached.

Parameters:

Name Type Description Default
max int | None

Maximum number of redirects to follow

None

Returns:

Name Type Description
Policy Policy

A redirect policy with the specified limit

Example
import rnet
from rnet import Client, redirect
policy = redirect.Policy.limited(5)
client = Client(redirect=policy)
Source code in rnet/redirect.py
@staticmethod
def limited(max: int | None = None) -> "Policy":
    """
    Create a Policy with a maximum number of redirects.

    An error will be returned if the max is reached.

    Args:
        max: Maximum number of redirects to follow

    Returns:
        Policy: A redirect policy with the specified limit

    Example:
        ```python
        import rnet
        from rnet import Client, redirect
        policy = redirect.Policy.limited(5)
        client = Client(redirect=policy)
        ```
    """
    ...

none staticmethod

none()

Create a Policy that does not follow any redirect.

Returns:

Name Type Description
Policy Policy

A redirect policy that doesn't follow redirects

Example
from rnet import Client, redirect

policy = redirect.Policy.none()
client = Client(redirect=policy)
Source code in rnet/redirect.py
@staticmethod
def none() -> "Policy":
    """
    Create a Policy that does not follow any redirect.

    Returns:
        Policy: A redirect policy that doesn't follow redirects

    Example:
        ```python
        from rnet import Client, redirect

        policy = redirect.Policy.none()
        client = Client(redirect=policy)
        ```
    """
    ...

custom staticmethod

custom(callback)

Create a custom Policy using the passed function.

Parameters:

Name Type Description Default
callback Callable[[Attempt], Action]

A callable that takes an Attempt and returns an Action

required

Returns Policy: A custom redirect policy

Example
from rnet import Client, redirect

def policy(attempt: redirect.Attempt) -> redirect.Action:
    if len(attempt.previous) > 5:
        return attempt.error("too many redirects")
    elif "example.com" in attempt.uri:
        return attempt.stop()
    else:
        return attempt.follow()

policy = redirect.Policy.custom(policy)
client = Client(redirect=policy)
Source code in rnet/redirect.py
@staticmethod
def custom(callback: Callable[["Attempt"], "Action"]) -> "Policy":
    """
    Create a custom Policy using the passed function.

    Args:
        callback: A callable that takes an Attempt and returns an Action

    Returns
        Policy: A custom redirect policy

    Example:
        ```python
        from rnet import Client, redirect

        def policy(attempt: redirect.Attempt) -> redirect.Action:
            if len(attempt.previous) > 5:
                return attempt.error("too many redirects")
            elif "example.com" in attempt.uri:
                return attempt.stop()
            else:
                return attempt.follow()

        policy = redirect.Policy.custom(policy)
        client = Client(redirect=policy)
        ```
    """
    ...

Attempt

A type that holds information on the next request and previous requests in redirect chain.

Source code in rnet/redirect.py
@final
class Attempt:
    """
    A type that holds information on the next request and previous requests
    in redirect chain.
    """

    status: StatusCode
    """
    The HTTP status code of the redirect response.
    """

    headers: HeaderMap
    """
    The headers of the redirect response.
    """

    next: str
    """
    The next URI to which the client is being redirected.
    """

    previous: Sequence[str]
    """
    The list of previous URIs in the redirect chain.
    """

    def follow(self) -> "Action":
        """
        Returns an action meaning the client should follow the next URI.

        Returns:
            Action: An action to follow the redirect
        """
        ...

    def stop(self) -> "Action":
        """
        Returns an action meaning the client should not follow the next URI.

        The 30x response will be returned as the result.

        Returns:
            Action: An action to stop following redirects
        """
        ...

    def error(self, message: str) -> "Action":
        """
        Returns an action failing the redirect with an error.

        The error will be returned for the result of the sent request.

        Args:
            message: Error message

        Returns:
            Action: An action that will raise an error
        """
        ...

    def __str__(self) -> str: ...

status instance-attribute

status

The HTTP status code of the redirect response.

headers instance-attribute

headers

The headers of the redirect response.

next instance-attribute

next

The next URI to which the client is being redirected.

previous instance-attribute

previous

The list of previous URIs in the redirect chain.

follow

follow()

Returns an action meaning the client should follow the next URI.

Returns:

Name Type Description
Action Action

An action to follow the redirect

Source code in rnet/redirect.py
def follow(self) -> "Action":
    """
    Returns an action meaning the client should follow the next URI.

    Returns:
        Action: An action to follow the redirect
    """
    ...

stop

stop()

Returns an action meaning the client should not follow the next URI.

The 30x response will be returned as the result.

Returns:

Name Type Description
Action Action

An action to stop following redirects

Source code in rnet/redirect.py
def stop(self) -> "Action":
    """
    Returns an action meaning the client should not follow the next URI.

    The 30x response will be returned as the result.

    Returns:
        Action: An action to stop following redirects
    """
    ...

error

error(message)

Returns an action failing the redirect with an error.

The error will be returned for the result of the sent request.

Parameters:

Name Type Description Default
message str

Error message

required

Returns:

Name Type Description
Action Action

An action that will raise an error

Source code in rnet/redirect.py
def error(self, message: str) -> "Action":
    """
    Returns an action failing the redirect with an error.

    The error will be returned for the result of the sent request.

    Args:
        message: Error message

    Returns:
        Action: An action that will raise an error
    """
    ...

Action

An action to perform when a redirect status code is found.

This class is typically created by calling methods on Attempt: - attempt.follow() - attempt.stop() - attempt.error(message)

Source code in rnet/redirect.py
@final
class Action:
    """
    An action to perform when a redirect status code is found.

    This class is typically created by calling methods on Attempt:
    - attempt.follow()
    - attempt.stop()
    - attempt.error(message)
    """

    ...

    def __str__(self) -> str: ...

History

An entry in the redirect history.

Source code in rnet/redirect.py
@final
class History:
    """
    An entry in the redirect history.
    """

    status: int
    """Get the status code of the redirect response."""

    url: str
    """Get the URL of the redirect response."""

    previous: str
    """Get the previous URL before the redirect response."""

    headers: HeaderMap
    """Get the headers of the redirect response."""

    def __str__(self) -> str: ...

status instance-attribute

status

Get the status code of the redirect response.

url instance-attribute

url

Get the URL of the redirect response.

previous instance-attribute

previous

Get the previous URL before the redirect response.

headers instance-attribute

headers

Get the headers of the redirect response.