From 856f3304e9d7bdd98e59b514711c023b42bdfaf6 Mon Sep 17 00:00:00 2001 From: Sheng Yu Date: Fri, 21 Jul 2023 14:02:15 -0400 Subject: [PATCH] test --- craft_store/base_client.py | 28 ++++++++++++++++++++------ craft_store/store_client.py | 26 ++++++++++++++++++++++++ craft_store/ubuntu_one_store_client.py | 9 +++++++-- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/craft_store/base_client.py b/craft_store/base_client.py index 8a1e1e5..7432ed4 100644 --- a/craft_store/base_client.py +++ b/craft_store/base_client.py @@ -151,6 +151,7 @@ def request( url: str, params: Optional[Dict[str, str]] = None, headers: Optional[Dict[str, str]] = None, + request_auth: Optional[bool] = None, **kwargs, ) -> requests.Response: """Perform an authenticated request if auth_headers are True. @@ -169,7 +170,12 @@ def request( if headers is None: headers = {} - headers["Authorization"] = self._get_authorization_header() + if request_auth is None: + auth_header = self._get_authorization_header() + if auth_header: + headers["Authorization"] = auth_header + elif request_auth is True: + headers["Authorization"] = self._get_authorization_header() return self.http_client.request( method, @@ -181,7 +187,9 @@ def request( def whoami(self) -> Dict[str, Any]: """Return whoami json data queyring :attr:`.endpoints.Endpoints.whoami`.""" - return self.request("GET", self._base_url + self._endpoints.whoami).json() + return self.request( + "GET", self._base_url + self._endpoints.whoami, request_auth=True + ).json() def logout(self) -> None: """Clear credentials. @@ -274,7 +282,10 @@ def notify_revision( """ endpoint = f"/v1/{self._endpoints.namespace}/{name}/revisions" response = self.request( - "POST", self._base_url + endpoint, json=revision_request.marshal() + "POST", + self._base_url + endpoint, + json=revision_request.marshal(), + request_auth=True, ).json() return cast( @@ -306,6 +317,7 @@ def release( "POST", self._base_url + endpoint, json=[r.marshal() for r in release_request], + request_auth=True, ) def list_registered_names( @@ -320,7 +332,9 @@ def list_registered_names( params = { "include-collaborations": "true" if include_collaborations else "false", } - response = self.request("GET", self._base_url + endpoint, params=params) + response = self.request( + "GET", self._base_url + endpoint, params=params, request_auth=True + ) results = response.json().get("results", []) return [models.RegisteredNameModel.unmarshal(item) for item in results] @@ -352,7 +366,9 @@ def register_name( if entity_type is not None: request_json["type"] = entity_type - response = self.request("POST", self._base_url + endpoint, json=request_json) + response = self.request( + "POST", self._base_url + endpoint, json=request_json, request_auth=True + ) return response.json()["id"] def unregister_name(self, name: str) -> str: @@ -363,6 +379,6 @@ def unregister_name(self, name: str) -> str: :returns: the ID of the deleted name. """ endpoint = f"/v1/{self._endpoints.namespace}/{name}" - response = self.request("DELETE", self._base_url + endpoint) + response = self.request("DELETE", self._base_url + endpoint, request_auth=True) return response.json()["package-id"] diff --git a/craft_store/store_client.py b/craft_store/store_client.py index b1b179a..2aabdf0 100644 --- a/craft_store/store_client.py +++ b/craft_store/store_client.py @@ -131,3 +131,29 @@ def _get_discharged_macaroon(self, root_macaroon: str, **kwargs) -> str: credentials = self._authorize_token(candid_discharged_macaroon) return creds.marshal_candid_credentials(credentials) + + +class CharmhubStoreClient(StoreClient): + """Encapsulates API calls for Charmhub.""" + + def __init__( + self, + *, + base_url: str, + storage_base_url: str, + endpoints: endpoints.Endpoints = endpoints.CHARMHUB, # pylint: disable=W0621 + application_name: str, + user_agent: str, + environment_auth: Optional[str] = None, + ephemeral: bool = False, + ) -> None: + """Encapsulates API calls for Charmhub.""" + super().__init__( + base_url=base_url, + storage_base_url=storage_base_url, + endpoints=endpoints, + application_name=application_name, + user_agent=user_agent, + environment_auth=environment_auth, + ephemeral=ephemeral, + ) diff --git a/craft_store/ubuntu_one_store_client.py b/craft_store/ubuntu_one_store_client.py index fd3ab21..8a127e2 100644 --- a/craft_store/ubuntu_one_store_client.py +++ b/craft_store/ubuntu_one_store_client.py @@ -138,14 +138,19 @@ def request( url: str, params: Optional[Dict[str, str]] = None, headers: Optional[Dict[str, str]] = None, + request_auth: Optional[bool] = None, **kwargs, ) -> requests.Response: try: - response = super().request(method, url, params, headers, **kwargs) + response = super().request( + method, url, params, headers, request_auth=request_auth, **kwargs + ) except errors.StoreServerError as store_error: if "macaroon-needs-refresh" in store_error.error_list: self._refresh_token() - response = super().request(method, url, params, headers, **kwargs) + response = super().request( + method, url, params, headers, request_auth=request_auth, **kwargs + ) else: raise