Client method mixins

The behaviour shared by Client and AsyncClient.

class atproto_client.client.methods_mixin.session.SessionDispatchMixin

Bases: object

on_session_change(callback: Callable[[SessionEvent, Session], None]) None

Register a callback for session change event.

Parameters:

callback – A callback to be called when the session changes. The callback must accept two arguments: event and session.

Note

Possible events: SessionEvent.IMPORT, SessionEvent.CREATE, SessionEvent.REFRESH.

Tip

You should save the session string to persistent storage on SessionEvent.CREATE and SessionEvent.REFRESH event.

Example

>>> from atproto import Client, SessionEvent, Session
>>>
>>> client = Client()
>>>
>>> @client.on_session_change
>>> def on_session_change(event: SessionEvent, session: Session):
>>>     print(event, session)
>>>
>>> # or you can use this syntax:
>>> # client.on_session_change(on_session_change)
Returns:

None

class atproto_client.client.methods_mixin.session.AsyncSessionDispatchMixin

Bases: object

on_session_change(callback: Callable[[SessionEvent, Session], Coroutine[Any, Any, None]] | Callable[[SessionEvent, Session], None]) None

Register a callback for session change event.

Parameters:

callback – A callback to be called when the session changes. The callback must accept two arguments: event and session.

Note

Possible events: SessionEvent.IMPORT, SessionEvent.CREATE, SessionEvent.REFRESH.

Note

You can register both synchronous and asynchronous callbacks.

Tip

You should save the session string to persistent storage on SessionEvent.CREATE and SessionEvent.REFRESH event.

Example

>>> from atproto import AsyncClient, SessionEvent, Session
>>>
>>> client = AsyncClient()
>>>
>>> @client.on_session_change
>>> async def on_session_change(event: SessionEvent, session: Session):
>>>     print(event, session)
>>>
>>> # or you can use this syntax:
>>> # client.on_session_change(on_session_change)
Returns:

None

class atproto_client.client.methods_mixin.session.SessionMethodsMixin(*args: Any, session_dispatcher: SessionDispatcher | None = None, **kwargs: Any)

Bases: TimeMethodsMixin

export_session_string() str

Export session string.

Note

This method is useful for storing the session and reusing it later.

Warning

You should use it if you create the client instance often. Because of server rate limits for createSession. Rate limited by handle. 30/5 min, 300/day.

Attention

You must export session at the end of the Client`s life cycle! Alternatively, you can subscribe to the session change event. Use on_session_change() to register handler.

Example

>>> from atproto import Client
>>> # the first time login with login and password
>>> client = Client()
>>> client.login('login', 'password')
>>> session_string = client.export_session_string()
>>> # store session_string somewhere.
>>> # for example, in env and next time use it for login
>>> client2 = Client()
>>> client2.login(session_string=session_string)
Returns:

Session string.

Return type:

str

class atproto_client.client.methods_mixin.headers.HeadersConfigurationMethodsMixin

Bases: object

BSKY_CHAT_DID: ClassVar[Literal['did:web:api.bsky.chat']] = 'did:web:api.bsky.chat'
BSKY_LABELER_DID: ClassVar[Literal['did:plc:ar7c4by46qjdydhdevvrndac']] = 'did:plc:ar7c4by46qjdydhdevvrndac'
class AtprotoServiceType(*values)

Bases: Enum

The type of atproto service.

ATPROTO_LABELER = 'atproto_labeler'
BSKY_CHAT = 'bsky_chat'
clone() Self

Clone the client instance.

Used to customize atproto proxy and set of labeler services.

Note

The clone shares the session and its dispatcher with the original, so it stays authenticated even when it is created before the first login.

Returns:

Cloned client instance.

with_proxy(service_type: AtprotoServiceType | str, did: str) Self

Get a new client instance with the atproto-proxy header configured.

Parameters:
  • service_type – The type of service.

  • did – The DID of the proxy.

Returns:

Configured client instance.

Return type:

self

with_labelers(labeler_dids: List[str]) Self

Get a new client instance with the atproto-accept-labelers header configured.

Parameters:

labeler_dids – The DIDs of the labelers.

Returns:

Configured client instance.

Return type:

self

configure_proxy_header(service_type: AtprotoServiceType | str, did: str) None

Configure the atproto-proxy header to be applied on requests.

Parameters:
  • service_type – The type of service.

  • did – The DID of the proxy.

configure_labelers_header(labeler_dids: List[str]) None

Configure the atproto-labelers header to be applied on requests.

Parameters:

labeler_dids – The DIDs of the labelers.

with_bsky_chat_proxy() Self

Get a new client instance with the atproto-proxy header configured for bsky.chat.

Returns:

Configured client instance.

Return type:

self

with_bsky_labeler() Self

Get a new client instance with the atproto-accept-labelers header configured for Bluesky Labeler.

Returns:

Configured client instance.

Return type:

self

class atproto_client.client.methods_mixin.time.TimeMethodsMixin

Bases: object

get_current_time() datetime

Get current time in Server Timezone (UTC).

get_current_time_iso() str

Get current time in Server Timezone (UTC) and ISO format.

get_time_from_timestamp(timestamp: int) datetime

Get datetime from timestamp in Server Timezone (UTC).