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.DotDict type, 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 model or None or atproto.xrpc_client.models.dot_dict.DotDict if strict is disabled.

atproto_client.models.utils.get_response_model(response: Response, model: Type[M]) β†’ M
atproto_client.models.utils.get_model_as_dict(model: DotDict | BlobRef | ModelBase) β†’ Dict[str, Any]
atproto_client.models.utils.get_model_as_json(model: DotDict | BlobRef | ModelBase) β†’ str
atproto_client.models.utils.is_json(json_data: str | bytes) β†’ bool
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:

bool

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:

atproto.xrpc_client.models.com.atproto.repo.strong_ref.Main