frappex-client v0.1.1

Reference

Architecture#

frappex-client is about 1,700 lines of Python, docstrings included. This page explains how they fit together, for contributors and for anyone deciding whether to trust it.

The big idea: the protocol has no network code#

Most API clients mix three things in every method: building the request, sending it, and decoding the reply. frappex-client separates them.

  • Building and decoding are plain functions with no I/O. They know the Frappe wire format: routes, filter encoding, the error JSON.
  • Sending is the only part that differs between the blocking and the asyncio client, and it is the only part they implement.

So the sync and async clients cannot drift apart: there is nothing for them to disagree about. This pattern is often called sans-I/O.

The modules#

Module Role Does I/O?
_operations.py One function per API call, returning an Operation No
_protocol.py Request, RawResponse, safe URL paths, decode() No
_config.py Address validation, header checks, RetryPolicy No
_auth.py Secret and the three auth strategies No
_base.py ClientCore: settings shared by both clients No
_client.py Client: sends with httpx.Client Yes
_async_client.py AsyncClient: sends with httpx.AsyncClient Yes
exceptions.py, types.py Public exceptions and type aliases No

Life of one call#

What happens inside client.get_doc("ToDo", "T/1"):

  1. Describe_operations.get_doc returns an Operation: a Request("GET", "/api/resource/ToDo/T%2F1") plus a function that extracts the document from the reply.
  2. Log in if neededWith PasswordAuth, the first call logs in once, under a lock shared by concurrent callers.
  3. SendThe client streams the response, enforcing the timeout and the size limit. Redirects are not followed.
  4. Decide on a retryRetryPolicy looks at the method and the outcome and returns a delay, or None to stop.
  5. Decode_protocol.decode turns the reply into the JSON object, or raises the exception that matches the status.
  6. ParseThe operation's parser picks the document out of {"data": ...} and checks its shape.

Steps 1, 4, 5 and 6 are shared pure functions. Only steps 2 and 3 are written twice, once with await.

An operation, concretely#

@dataclass(frozen=True)
class Operation(Generic[T]):
    request: Request
    parse: Callable[[dict[str, Any]], T]


def get_doc(doctype: str, name: str) -> Operation[Document]:
    return Operation(Request("GET", resource_path(doctype, name)), _document(_data))

And the whole public method on the client:

def get_doc(self, doctype: str, name: str) -> Document:
    return self._execute(ops.get_doc(doctype, name))

Adding a new API call means one operation function and two one-line methods.

Why this design#

  • Testability. The protocol is tested without a network: hundreds of unit and property-based tests run in a few seconds.
  • Safety in one place. Path encoding, header checks and retry decisions live in single functions, so a fix applies to every call and both clients.
  • Small, auditable surface. One runtime dependency, no metaclasses, no generated code.

Errors are decided by status first#

decode() maps the HTTP status to an exception class (404 → NotFoundError and so on), then fills in what the body reports: the server's exception name, its messages and, if exposed, its traceback. Using the status first keeps the mapping stable across Frappe versions, which report error details differently.

Further reading#

Found a mistake? Open an issue.