Configuration#
Every option is a keyword argument of Client and AsyncClient. All of them
are validated when the client is created, before any connection is made.
from frappex_client import Client, RetryPolicy, TokenAuth
client = Client(
"https://erp.example.com",
TokenAuth(key, secret),
timeout=30,
retry=RetryPolicy(),
verify=None,
allow_insecure_http=False,
max_response_bytes=64 * 1024 * 1024,
headers=None,
)
| Option | Default | Meaning |
|---|---|---|
base_url |
required | Root address of the site, without a path to /app or /api |
auth |
None (guest) |
See Authentication |
timeout |
30 |
Seconds per request, or an httpx.Timeout |
retry |
RetryPolicy() |
See Retries |
verify |
system CAs | A CA bundle path or an ssl.SSLContext |
allow_insecure_http |
False |
Permit http:// for local development |
max_response_bytes |
64 MiB | Largest response body accepted |
headers |
none | Extra headers for every request |
transport |
default | An httpx transport, mainly for tests |
The site address#
Use the root URL of the site: https://erp.example.com. frappex-client appends
/api/... itself. The address must be https://, must not contain a query
string or fragment, and must not contain credentials.
A trailing path is kept, for sites served under a prefix such as
https://example.com/erp.
Private certificate authorities#
If the site uses a certificate from your organisation's own CA, give frappex-client the CA bundle. Certificate checks stay on:
client = Client("https://erp.internal", auth, verify="/etc/ssl/certs/company-ca.pem")
Or build an ssl.SSLContext yourself:
import ssl
context = ssl.create_default_context(cafile="/etc/ssl/certs/company-ca.pem")
context.minimum_version = ssl.TLSVersion.TLSv1_2
client = Client("https://erp.internal", auth, verify=context)
There is no verify=False
Passing False (or True) raises ConfigurationError. Turning off
certificate checks lets anyone on the network read your API secret. Fix
the certificate, or trust the CA that issued it.
Local development over HTTP#
client = Client("http://localhost:8000", auth, allow_insecure_http=True)
Use this only for a site on your own machine or a private network. Over plain HTTP the API secret is readable by anyone in between.
Multi-site benches#
A bench that hosts several sites on one port picks the site from the Host
header. Point frappex-client at the bench and name the site:
client = Client(
"http://127.0.0.1:8000",
auth,
allow_insecure_http=True,
headers={"Host": "site1.localhost"},
)
Extra headers#
headers adds headers to every request, for example a tracing ID required by
a gateway in front of the site:
client = Client(url, auth, headers={"X-Request-Source": "billing-sync"})
Authorization, Cookie, Content-Length and Transfer-Encoding are
refused, because frappex-client manages them; so is any value containing a line
break.
Response size limit#
Replies larger than max_response_bytes raise ResponseTooLargeError. The
limit protects your program from a runaway query or a misconfigured proxy
returning an endless body. Raise it if you really fetch large pages, or page
the query instead:
client = Client(url, auth, max_response_bytes=256 * 1024 * 1024)
Logging#
frappex-client logs each request at DEBUG level on the frappex_client logger: the method,
the path and the status. It never logs headers, query strings or bodies,
because they can contain credentials or personal data.
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger("frappex_client").setLevel(logging.DEBUG)
# DEBUG:frappex_client:GET /api/resource/ToDo -> 200
Transports#
transport replaces the network layer with any httpx transport. Its main
use is testing with httpx.MockTransport; see Testing your code.
A custom transport is responsible for TLS itself, so verify no longer
applies to it.