Floki has been updated to support Mojo 1.0.0b2 and a few new Session configurations have been added. It’s really a first pass at this, I’m exploring options for the user interface to keep the number of ways you can configure options tight and focused, but also making it not so verbose. I’ll continue to iterate on it.
Timeouts
Configure granular connect/total timeouts (in seconds) on a Session. A bare number
is treated as the total timeout:
from floki.session import Session
from floki.timeout import Timeout
def main() raises -> None:
var session = Session(timeout=Timeout(connect=5.0, total=30.0))
var quick = Session(timeout=10) # 10 second total timeout
var r = session.get("https://example.com")
Retries with backoff
Retry failed transfers and retryable status codes with exponential backoff:
from floki.session import Session
from floki.retry import Retry
def main() raises -> None:
var session = Session(
retry=Retry(max_retries=3, backoff_factor=0.5, status_forcelist=[500, 502, 503, 504])
)
var r = session.get("https://example.com")
The delay before retry n is backoff_factor * 2 ** (n - 1) seconds.
Proxies
from floki.session import Session
from floki.proxy import Proxy
def main() raises -> None:
var session = Session(proxy=Proxy("http://proxy.example:8080"))
# With credentials and a bypass list:
var authed = Session(
proxy=Proxy(
"http://proxy.example:8080",
username="user",
password="secret",
no_proxy="localhost,127.0.0.1",
)
)
var r = session.get("https://example.com")
TLS verification
TLS certificate and hostname verification is enabled by default. It can be disabled
(use with great caution) or pointed at a custom certificate authority bundle:
from std.pathlib import Path
from floki.session import Session
from floki.tls import TLS
def main() raises -> None:
# Disable verification — only for testing or trusted networks.
var insecure = Session(tls=TLS(verify=False))
# Use a custom CA bundle (e.g. a private PKI or self-signed cert).
var custom = Session(tls=TLS(ca_bundle=Path("/path/to/ca-bundle.pem")))
var r = custom.get("https://internal.example.com")
timeout, retry, proxy, and tls are also accepted directly by the free
functions (e.g. floki.get(url, timeout=10)), which forward them to the one-shot
Session they create internally.
Examples
I’ve also added two examples of a server and client, using Flare for the server
. The todo example also includes usage of my Slight sqlite3 package to include a typical CRUD HTTP GET example of reading from a db and serializing the resource to a json response.