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

feat: add login_as_guest shortcut #8142

Merged
merged 5 commits into from
Oct 12, 2023
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
13 changes: 12 additions & 1 deletion packages/hagrid/hagrid/orchestra.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,24 @@ def __init__(
def client(self) -> Any:
if self.port:
sy = get_syft_client()
return sy.login(url=self.url, port=self.port, verbose=False) # type: ignore
return sy.login(url=self.url, port=self.port) # type: ignore
elif self.deployment_type == DeploymentType.PYTHON:
return self.python_node.get_guest_client(verbose=False) # type: ignore
else:
raise NotImplementedError(
f"client not implemented for the deployment type:{self.deployment_type}"
)

def login_as_guest(self, **kwargs: Any) -> Optional[Any]:
client = self.client

session = client.login_as_guest(**kwargs)

if isinstance(session, SyftError):
return session

return session

def login(
self, email: Optional[str] = None, password: Optional[str] = None, **kwargs: Any
) -> Optional[Any]:
Expand All @@ -182,6 +192,7 @@ def login(
password = getpass.getpass("Password: ")

session = client.login(email=email, password=password, **kwargs)

if isinstance(session, SyftError):
return session

Expand Down
1 change: 1 addition & 0 deletions packages/syft/src/syft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .abstract_node import NodeType # noqa: F401
from .client.client import connect # noqa: F401
from .client.client import login # noqa: F401
from .client.client import login_as_guest # noqa: F401
from .client.client import register # noqa: F401
from .client.deploy import Orchestra # noqa: F401
from .client.domain_client import DomainClient # noqa: F401
Expand Down
42 changes: 33 additions & 9 deletions packages/syft/src/syft/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,16 @@ def me(self) -> Optional[Union[UserView, SyftError]]:
return self.api.services.user.get_current_user()
return None

def login_as_guest(self) -> Self:
_guest_client = self.guest()

print(
f"Logged into <{self.name}: {self.metadata.node_side_type.capitalize()}-side "
f"{self.metadata.node_type.capitalize()}> as GUEST"
)

return _guest_client

def login(
self, email: str, password: str, cache: bool = True, register=False, **kwargs
) -> Self:
Expand Down Expand Up @@ -805,6 +815,27 @@ def register(
)


@instrument
def login_as_guest(
url: Union[str, GridURL] = DEFAULT_PYGRID_ADDRESS,
node: Optional[AbstractNode] = None,
port: Optional[int] = None,
verbose: bool = True,
):
_client = connect(url=url, node=node, port=port)

if isinstance(_client, SyftError):
return _client

if verbose:
print(
f"Logged into <{_client.name}: {_client.metadata.node_side_type.capitalize()}-"
f"side {_client.metadata.node_type.capitalize()}> as GUEST"
)

return _client.guest()


@instrument
def login(
url: Union[str, GridURL] = DEFAULT_PYGRID_ADDRESS,
Expand All @@ -813,11 +844,12 @@ def login(
email: Optional[str] = None,
password: Optional[str] = None,
cache: bool = True,
verbose: bool = True,
) -> SyftClient:
_client = connect(url=url, node=node, port=port)

if isinstance(_client, SyftError):
return _client

connection = _client.connection

login_credentials = None
Expand All @@ -827,14 +859,6 @@ def login(
password = getpass("Password: ")
login_credentials = UserLoginCredentials(email=email, password=password)

if login_credentials is None:
if verbose:
print(
f"Logged into <{_client.name}: {_client.metadata.node_side_type.capitalize()}-"
f"side {_client.metadata.node_type.capitalize()}> as GUEST"
)
return _client.guest()

if cache and login_credentials:
_client_cache = SyftClientSessionCache.get_client(
login_credentials.email,
Expand Down
Loading