Skip to content

Commit

Permalink
- better error message
Browse files Browse the repository at this point in the history
- update docstring
  • Loading branch information
Amin committed Sep 23, 2024
1 parent 8691892 commit 767b729
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
10 changes: 10 additions & 0 deletions pasqal_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ def __init__(
email/password combination or a TokenProvider instance.
You may omit the password, you will then be prompted to enter one.
The SDK can be initialized with several authentication options:
- Option 1: No arguments -> Allows unauthenticated access to public
features.
- Option 2: `username` and `password` -> Authenticated access using a
username and password.
- Option 3: `username` only -> Prompts for password during initialization.
- Option 4 (for developers): Provide a custom `token_provider` for
token-based authentication.
Args:
username: Email of the user to login as.
password: Password of the user to login as.
Expand Down
28 changes: 15 additions & 13 deletions pasqal_cloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class EmptyFilter:


class Client:
authenticator: AuthBase

def __init__(
self,
project_id: str | None = None,
Expand All @@ -58,31 +60,25 @@ def __init__(
self.user_agent = f"PasqalCloudSDK/{sdk_version}"

if token_provider is not None:
self.project_id
self._check_token_provider(token_provider)

if username:
self.project_id
auth0 = self._make_auth0(auth0)
token_provider = self._credential_login(username, password, auth0)

self._authenticator = None
self.authenticator = None
if token_provider:
self._authenticator = HTTPBearerAuthenticator(token_provider)
self.authenticator = HTTPBearerAuthenticator(token_provider)

@property
def project_id(self) -> str:
if not self._project_id:
raise ValueError("You need to provide a project_id")
raise ValueError("You need to set a project_id.")
return self._project_id

@property
def authenticator(self) -> AuthBase:
if self._authenticator is None:
raise ValueError(
"At least a username or TokenProvider object should be provided."
)
return self._authenticator
@project_id.setter
def project_id(self, project_id: str):
self._project_id = project_id

@staticmethod
def _make_endpoints(endpoints: Optional[Endpoints]) -> Endpoints:
Expand Down Expand Up @@ -132,6 +128,12 @@ def _authenticated_request(
payload: Optional[Union[Mapping, Sequence[Mapping]]] = None,
params: Optional[Mapping[str, Any]] = None,
) -> JSendPayload:
if self.authenticator is None:
raise ValueError(
"Authentication required. Please provide your credentials when"
" initiating the client."
)

resp = requests.request(
method,
url,
Expand Down Expand Up @@ -337,7 +339,7 @@ def cancel_workload(self, workload_id: str) -> Dict[str, Any]:
return response

def get_device_specs_dict(self) -> Dict[str, str]:
if self._authenticator is not None:
if self.authenticator is not None:
response: Dict[str, str] = self._authenticated_request(
"GET", f"{self.endpoints.core}/api/v1/devices/specs"
)["data"]
Expand Down
22 changes: 13 additions & 9 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ def test_correct_new_auth0(self):
auth0=new_auth0,
)

def test_module_no_project_id(self):
sdk = SDK(username=self.username, password=self.password)
with pytest.raises(
ValueError,
match="You need to set a project_id",
):
sdk.create_batch("", [])


@patch("pasqal_cloud.client.Auth0TokenProvider", FakeAuth0AuthenticationFailure)
class TestAuthFailure(TestSDKCommonAttributes):
Expand All @@ -98,14 +106,8 @@ def test_module_bad_password(self):
)


@patch("pasqal_cloud.client.Auth0TokenProvider", FakeAuth0AuthenticationFailure)
class TestAuthInvalidClient(TestSDKCommonAttributes):
def test_module_no_project_id(self):
with pytest.raises(
ValueError,
match="You need to provide a project_id",
):
SDK(username=self.username, password=self.password)

def test_module_no_user_with_password(self):
sdk = SDK(
project_id=self.project_id,
Expand All @@ -114,7 +116,8 @@ def test_module_no_user_with_password(self):
)
with pytest.raises(
ValueError,
match="At least a username or TokenProvider object should be provided",
match="Authentication required. Please provide your credentials when "
"initiating the client.",
):
sdk.get_batch("fake-id")

Expand Down Expand Up @@ -158,7 +161,8 @@ def test_authentication_no_credentials_provided(self):
sdk = SDK(project_id=self.project_id)
with pytest.raises(
ValueError,
match="At least a username or TokenProvider object should be provided",
match="Authentication required. Please provide your credentials when "
"initiating the client.",
):
sdk.get_batch("fake-id")

Expand Down

0 comments on commit 767b729

Please sign in to comment.