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
limited
staticmethod
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
Source code in rnet/redirect.py
none
staticmethod
Create a Policy that does not follow any redirect.
Returns:
| Name | Type | Description |
|---|---|---|
Policy |
Policy
|
A redirect policy that doesn't follow redirects |
Example
Source code in rnet/redirect.py
custom
staticmethod
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
Attempt
A type that holds information on the next request and previous requests in redirect chain.
Source code in rnet/redirect.py
follow
Returns an action meaning the client should follow the next URI.
Returns:
| Name | Type | Description |
|---|---|---|
Action |
Action
|
An action to follow the redirect |
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 |
error
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
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
History
An entry in the redirect history.