Extension points¶
Customize URL generation by subclassing these components and assigning them
to imgproxykit.Client.path_serializer_cls,
imgproxykit.Client.url_signer_cls, or
imgproxykit.Client.variants_resolver_cls.
Path serializer¶
- class imgproxykit.serialization.PathSerializer(*, argument_separator: str, use_short_options: bool, source_encoding: SourceEncoding)[source]¶
Serialize processing options into an unsigned imgproxy path.
Used by
Clientbefore signing the resulting path. Sources use URL-safe Base64 without padding or full percent-encoding. Processing options precede the source in the supplied order.Pass processing options through
Transformas option instances or scalar keyword arguments. Preset options reference names configured on the imgproxy server and are not expanded here.Subclass this serializer and override
__call__()to customize path construction. Set the subclass onimgproxykit.Client.path_serializer_clsto use it in a client.Configure processing options and select the source URL serializer.
- Parameters:¶
- argument_separator: str¶
Separator between processing option names and their arguments. Must match the server’s IMGPROXY_ARGUMENTS_SEPARATOR.
- use_short_options: bool¶
Use short aliases when available, otherwise full names.
- source_encoding: SourceEncoding¶
Source URL representation:
'base64'without padding or'plain'with full UTF-8 percent-encoding. See imgproxy source URLs.
-
__call__(source: str, *, transform: Transform | None =
None) str[source]¶ Build an unsigned path using processing options and preset references.
Serialize each option using the configured arguments separator before the encoded source. Output formats are included as processing segments. Omitted processing parameters are left to the server.
- Parameters:¶
- Return type:¶
- Returns:¶
Unsigned path beginning with
/, ready for signing. Contains neither the imgproxy address nor a signature segment.
Examples
Build a square WebP thumbnail path:
>>> from imgproxykit import Transform >>> from imgproxykit.serialization import PathSerializer >>> from imgproxykit.options import Resize >>> serializer = PathSerializer( ... argument_separator=':', ... use_short_options=False, ... source_encoding='base64', ... ) >>> thumbnail = Transform( ... Resize.fill(width=96, height=96), ... format='webp', ... ) >>> serializer('s3://images/photo.jpg', transform=thumbnail) '/resize:fill:96:96/format:webp/czM6Ly9pbWFnZXMvcGhvdG8uanBn'
URL signer¶
- class imgproxykit.signing.UrlSigner(*, key: str | None, salt: str | None, signature_size: int, unsigned_marker: str)[source]¶
Sign imgproxy paths using a reusable key and salt pair.
Used by
Clientafter path construction. Credentials are decoded once during initialization and reused for subsequent signatures. PassingNonefor both credentials enables unsigned mode.Subclass this signer and override
__call__()to customize signing. Set the subclass onimgproxykit.Client.url_signer_clsto use it in a client.Configure signing credentials or enable unsigned mode.
Supply both
keyandsaltto generate signatures. PassNonefor both to return the configured unsigned marker. The server must accept unsigned requests or trust that marker.- Parameters:¶
- key: str | None¶
Nonempty hex-encoded signing key matching the server. Supply together with
salt;Noneselects unsigned mode.- salt: str | None¶
Nonempty hex-encoded signing salt matching the server. Supply together with
key;Noneselects unsigned mode.- signature_size: int¶
Positive digest byte count to encode. Must match the server’s IMGPROXY_SIGNATURE_SIZE. Values of 32 or more use the full digest.
- unsigned_marker: str¶
URL marker used when key and salt are both
None. With server signing enabled, configure this marker in IMGPROXY_TRUSTED_SIGNATURES.
- Raises:¶
ValueError – If only one signing credential is supplied, either is empty or invalid hex, or
signature_sizeis not positive, even in unsigned mode.
- __call__(path: str) str[source]¶
Sign the exact supplied path or return the unsigned marker.
The signature is an HMAC-SHA256 digest of the salt followed by the UTF-8 encoded path. The path is neither normalized nor modified.
Dataclass variants resolver¶
- class imgproxykit.variants.DataclassVariantsResolver(*, model: type[DataclassInstance])[source]¶
Validate a dataclass variant declaration and resolve its transformations.
Each constructor field must be annotated with
Annotated[str, Transform(...)]and contain exactly oneTransform. Other annotation metadata is ignored.InitVarand fields declared withinit=Falseare unsupported.This component only resolves the declaration. URL generation and result construction are handled by
imgproxykit.Client.variants().