utils
Helpers for working with model instances.
- atproto_client.models.utils.get_or_create(model_data: M | Dict[str, Any] | None, model: Type[M] | None = None, *, strict: bool = True, strict_string_format: bool = False) M | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | Record | DotDict | None
Get model instance from raw data.
Note
The record could have additional fields and be completely custom. For example, third-party bsky clients add a βviaβ field to indicate that it was posted using a not official client. Such records are corresponding to the lexicon, but have additional fields. This is called βextended recordβ. Extended records will be decoded to proper models with extra, non-typehinted fields available only in runtime. Unknown record types will be decoded to
atproto.xrpc_client.models.base.DotDict. A union member with an unrecognized β$typeβ is decoded to a DotDict too, without failing the rest of the model.Note
By default, the method raises an exception on custom models if you have passed the expected model. To fall back to a
atproto.xrpc_client.models.base.DotDicttype, disable strict mode using the argument.Note
Model auto-resolve works only with a Record type for now.
- Parameters:
model_data β Raw data.
model β Class of the model or any another type. If None, it will be resolved automatically.
strict β Disable fallback to dictionary (
atproto.xrpc_client.models.base.DotDict) if it canβt be properly deserialized in provided model. Will raise the exception instead.strict_string_format β Enable strict string format validation.
- Returns:
Instance of
modelorNoneoratproto.xrpc_client.models.dot_dict.DotDictif strict is disabled.
- atproto_client.models.utils.load_json(json_data: str | bytes, strict: bool = True) Dict[str, Any] | None
- atproto_client.models.utils.is_record_type(model: ModelBase | DotDict, expected_type: _RecordModule[R]) TypeGuard[R]
- atproto_client.models.utils.is_record_type(model: ModelBase | DotDict, expected_type: Type[R]) TypeGuard[R]
- atproto_client.models.utils.is_record_type(model: ModelBase | DotDict, expected_type: str | ModuleType) bool
Verify that the model is the expected Record type.
Note
Passing a Python module or a Record class narrows the type of model for static type checkers. Narrowing is not performed for the NSID form because a string carries no type information.
Warning
A custom or extended record that failed validation is decoded to
atproto_client.models.dot_dict.DotDict. Such a model matches its own type by NSID, so the narrowed type is not guaranteed to be an instance of the Record class in runtime.- Parameters:
model β Model to be verified.
expected_type β Excepted type. Could be NSID, Python Module, or Record class.
Example
>>> from atproto import Client, models >>> from atproto_client.models import ids, is_record_type >>> client = Client() >>> client.login('username', 'pass') >>> record = client.com.atproto.repo.get_record(...) >>> # using NSID: >>> is_record_type(record.value, ids.AppBskyFeedPost) >>> # using Python module: >>> is_record_type(record.value, models.AppBskyFeedPost) >>> # using Record class: >>> is_record_type(record.value, models.AppBskyFeedPost.Record)- Returns:
Is record or not.
- Return type:
- atproto_client.models.utils.create_strong_ref(model: ModelBase) Main
Create a strong ref from the model.
- Parameters:
model β Any model with cid and uri fields.
Example
>>> from atproto import Client >>> client = Client() >>> client.login('my-handle', 'my-password') >>> response = client.send_post(text='Hello World from Python!') >>> strong_ref = create_strong_ref(response)- Returns:
Strong ref.
- Return type: