From 449722f08c070919738319285199cf08b203d7a6 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Sat, 18 Dec 2021 13:45:26 +0200 Subject: [PATCH] Improved support for private packages in PlatformIO Registry --- HISTORY.rst | 1 + platformio/clients/http.py | 4 +++- platformio/clients/registry.py | 32 +++++++++++++++++++++++++++++--- platformio/commands/access.py | 8 ++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 78f1603c..9fbfda97 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -11,6 +11,7 @@ PlatformIO Core 5 5.2.5 (2021-12-??) ~~~~~~~~~~~~~~~~~~ +- Improved support for private packages in `PlatformIO Registry `__ 5.2.4 (2021-12-15) ~~~~~~~~~~~~~~~~~~ diff --git a/platformio/clients/http.py b/platformio/clients/http.py index 0f3e3860..a2b92776 100644 --- a/platformio/clients/http.py +++ b/platformio/clients/http.py @@ -21,7 +21,7 @@ import requests.adapters from requests.packages.urllib3.util.retry import Retry # pylint:disable=import-error from platformio import __check_internet_hosts__, __default_requests_timeout__, app, util -from platformio.cache import ContentCache +from platformio.cache import ContentCache, cleanup_content_cache from platformio.exception import PlatformioException, UserSideException try: @@ -134,6 +134,8 @@ class HTTPClient(object): raise HTTPClientError(str(e)) def fetch_json_data(self, method, path, **kwargs): + if method != "get": + cleanup_content_cache("http") cache_valid = kwargs.pop("cache_valid") if "cache_valid" in kwargs else None if not cache_valid: return self._parse_json_response(self.send_request(method, path, **kwargs)) diff --git a/platformio/clients/registry.py b/platformio/clients/registry.py index 3a45a48e..be25736b 100644 --- a/platformio/clients/registry.py +++ b/platformio/clients/registry.py @@ -23,6 +23,21 @@ class RegistryClient(HTTPClient): def __init__(self): super(RegistryClient, self).__init__(__registry_api__) + @staticmethod + def allowed_private_packages(): + private_permissions = set( + [ + "service.registry.publish-private-tool", + "service.registry.publish-private-platform", + "service.registry.publish-private-library", + ] + ) + info = AccountClient().get_account_info() or {} + for item in info.get("packages", []): + if set(item.keys()) & private_permissions: + return True + return False + def send_auth_request(self, *args, **kwargs): headers = kwargs.get("headers", {}) if "Authorization" not in headers: @@ -116,13 +131,24 @@ class RegistryClient(HTTPClient): params = dict(query=" ".join(search_query)) if page: params["page"] = int(page) - return self.fetch_json_data( - "get", "/v3/search", params=params, cache_valid="1h" + return ( + self.send_auth_request + if self.allowed_private_packages() + else self.fetch_json_data + )( + "get", + "/v3/search", + params=params, + cache_valid="1h", ) def get_package(self, type_, owner, name, version=None): try: - return self.fetch_json_data( + return ( + self.send_auth_request + if self.allowed_private_packages() + else self.fetch_json_data + )( "get", "/v3/packages/{owner}/{type}/{name}".format( type=type_, owner=owner.lower(), name=name.lower() diff --git a/platformio/commands/access.py b/platformio/commands/access.py index d9fb3970..74f48e2b 100644 --- a/platformio/commands/access.py +++ b/platformio/commands/access.py @@ -134,6 +134,14 @@ def access_list(owner, urn_type, json_output): table_data = [] table_data.append(("URN:", resource.get("urn"))) table_data.append(("Owner:", resource.get("owner"))) + table_data.append( + ( + "Access:", + click.style("Private", fg="red") + if resource.get("private", False) + else "Public", + ) + ) table_data.append( ( "Access level(s):",