This project contains ONLY the pydantic v2 bindings for Keycloak model representations for use in Python based projects utilizing Keycloak admin APIs.
For APIs, I recommend Mantelo. Since this library itself doesn't offer any model representation bindings, projects can use both these libraries together for a complete pydantic compliant experience.
-
Transform model representations to python dictionaries using pydantic
model_dump()
. Mantelo client expects python dictionaries for write operations. Keycloak API semantics expect that all unset attributes are excluded.class TenantAdapter: @classmethod def model_new(cls, schema: TenantReqSchema) -> OrganizationRepresentation: """Convert `TenantReqSchema` to `OrganizationRepresentation`.""" return OrganizationRepresentation( name=schema.name, alias=schema.name.replace(" ", "_"), domains=[OrganizationDomainRepresentation(name=f"{schema.name.replace(' ', '-').lower()}.org", verified=False)], enabled=schema.active, attributes={"tax_id": [schema.tax_id], "address": [schema.address]}, ).model_dump(exclude_unset=True)
-
Transform python dictionaries to model representations using python dictionary unpacking operator
**
. Mantelo client returns python dictionaries for read operations.class TenantAdapter: @classmethod def schema(cls, data: Any | None) -> TenantResSchema | None: """Convert `Dict` to `TenantResSchema`.""" if data is None: return None # Coerse data dict to `OrganizationRepresentation` # representation = OrganizationRepresentation(**data) return TenantResSchema( id=representation.id, name=representation.name, address=representation.attributes["address"][0], tax_id=representation.attributes["tax_id"][0], active=representation.enabled, )
I intend to keep the library versioning synced with the latest stable Keycloak release.
pip install keycloak-admin-client
poetry add keycloak-admin-client