Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PDS auto-switching for self-hosted instances #370

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/atproto_client/client/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def _invoke(self, invoke_type: 'InvokeType', **kwargs: t.Any) -> 'Response
return await super()._invoke(invoke_type, **kwargs)

async def _set_session(self, event: SessionEvent, session: SessionResponse) -> None:
session = self._set_session_common(session)
session = self._set_session_common(session, self._base_url)
await self._call_on_session_change_callbacks(event, session.copy())

async def _get_and_set_session(self, login: str, password: str) -> 'models.ComAtprotoServerCreateSession.Response':
Expand Down
2 changes: 1 addition & 1 deletion packages/atproto_client/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _invoke(self, invoke_type: 'InvokeType', **kwargs: t.Any) -> 'Response':
return super()._invoke(invoke_type, **kwargs)

def _set_session(self, event: SessionEvent, session: SessionResponse) -> None:
session = self._set_session_common(session)
session = self._set_session_common(session, self._base_url)
self._call_on_session_change_callbacks(event, session.copy())

def _get_and_set_session(self, login: str, password: str) -> 'models.ComAtprotoServerCreateSession.Response':
Expand Down
6 changes: 5 additions & 1 deletion packages/atproto_client/client/methods_mixin/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,18 @@ def _should_refresh_session(self) -> bool:

return self.get_current_time() > expired_at

def _set_session_common(self, session: SessionResponse) -> Session:
def _set_session_common(self, session: SessionResponse, current_pds: str) -> Session:
self._access_jwt = session.access_jwt
self._access_jwt_payload = get_jwt_payload(session.access_jwt)

self._refresh_jwt = session.refresh_jwt
self._refresh_jwt_payload = get_jwt_payload(session.refresh_jwt)

pds_endpoint = get_session_pds_endpoint(session)
if not pds_endpoint:
# current_pds ends with xrpc endpoint, but this is not a problem
# overhead is only 4-5 symbols in the exported session string
pds_endpoint = current_pds

self._session = Session(
access_jwt=session.access_jwt,
Expand Down
8 changes: 6 additions & 2 deletions packages/atproto_client/client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ def copy(self) -> 'Session':


def get_session_pds_endpoint(session: SessionResponse) -> t.Optional[str]:
"""Return the PDS endpoint of the given session."""
"""Return the PDS endpoint of the given session.

Note:
Return :obj:`None` for self-hosted PDSs.
"""
if isinstance(session, Session):
return session.pds_endpoint

if is_valid_did_doc(session.did_doc):
if session.did_doc and is_valid_did_doc(session.did_doc):
doc = DidDocument.from_dict(session.did_doc)
return doc.get_pds_endpoint()

Expand Down
8 changes: 4 additions & 4 deletions packages/atproto_identity/did/resolvers/base_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def resolve_without_validation(self, did: str) -> t.Optional[t.Dict[str, t.Any]]
raise NotImplementedError

def resolve_no_cache(self, did: str) -> t.Optional['DidDocument']:
"""Resolve DID without cache.
"""Resolve DID without a cache.

Args:
did: DID.
Expand Down Expand Up @@ -103,7 +103,7 @@ def ensure_resolve(self, did: str, force_refresh: bool = False) -> 'DidDocument'
:obj:`DidDocument`: DID document.

Raises:
:obj:`DidNotFoundError`: DID not found.
:obj:`DidNotFoundError`: DID not find.
"""
did_doc = self.resolve(did, force_refresh)
if did_doc is None:
Expand Down Expand Up @@ -158,7 +158,7 @@ async def resolve_without_validation(self, did: str) -> t.Optional[t.Dict[str, t
raise NotImplementedError

async def resolve_no_cache(self, did: str) -> t.Optional['DidDocument']:
"""Resolve DID without cache.
"""Resolve DID without a cache.

Args:
did: DID.
Expand Down Expand Up @@ -225,7 +225,7 @@ async def ensure_resolve(self, did: str, force_refresh: bool = False) -> 'DidDoc
:obj:`DidDocument`: DID document.

Raises:
:obj:`DidNotFoundError`: DID not found.
:obj:`DidNotFoundError`: DID not find.
"""
did_doc = await self.resolve(did, force_refresh)
if did_doc is None:
Expand Down
Loading