Reuse and Composition

Goals

  • Balance reuse with readability.
  • Avoid unnecessary layers.

Guidance

  • Prefer thin wrappers and composition.
  • Use existing extension points before inventing new ones.
  • Preserve error context; avoid double serialization.

Example

Use requests' auth hook instead of duplicating request logic:

class AuthBase:
    """Base class that all auth implementations derive from"""

    def __call__(self, r):
        raise NotImplementedError("Auth hooks must be callable.")
class VolcAuth(AuthBase):
    def __init__(self, service_info, credentials):
        self.service_info = service_info
        self.credentials = credentials

    def __call__(self, r):
        # new_sign omitted
        new_sign(r, self.service_info, self.credentials)
        return r
auth = VolcAuth(service_info, credentials)
res = requests.post(url, json=body, auth=auth)

Sources & References