Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
syu-w committed Jul 21, 2023
1 parent 86e3c52 commit 856f330
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
28 changes: 22 additions & 6 deletions craft_store/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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]

Expand Down Expand Up @@ -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:
Expand All @@ -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"]
26 changes: 26 additions & 0 deletions craft_store/store_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
9 changes: 7 additions & 2 deletions craft_store/ubuntu_one_store_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 856f330

Please sign in to comment.