Files
platformio-core/platformio/registry/client.py
T

163 lines
5.6 KiB
Python
Raw Normal View History

# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2022-05-30 20:29:35 +03:00
# pylint: disable=too-many-arguments
2022-04-20 18:03:55 +03:00
from platformio import __registry_mirror_hosts__, fs
2022-05-30 21:00:22 +03:00
from platformio.account.client import AccountClient, AccountError
2022-05-30 20:29:35 +03:00
from platformio.http import HTTPClient, HTTPClientError
2020-07-31 15:42:26 +03:00
class RegistryClient(HTTPClient):
def __init__(self):
2022-04-20 18:03:55 +03:00
endpoints = [f"https://api.{host}" for host in __registry_mirror_hosts__]
super().__init__(endpoints)
@staticmethod
def allowed_private_packages():
private_permissions = set(
[
"service.registry.publish-private-tool",
"service.registry.publish-private-platform",
"service.registry.publish-private-library",
]
)
try:
info = AccountClient().get_account_info() or {}
for item in info.get("packages", []):
if set(item.keys()) & private_permissions:
return True
2021-12-20 19:05:12 +02:00
except AccountError:
pass
return False
2021-04-21 20:51:54 +03:00
def publish_package( # pylint: disable=redefined-builtin
self, owner, type, archive_path, released_at=None, private=False, notify=True
):
with open(archive_path, "rb") as fp:
2022-01-04 14:45:14 +02:00
return self.fetch_json_data(
"post",
2021-04-21 20:51:54 +03:00
"/v3/packages/%s/%s" % (owner, type),
params={
"private": 1 if private else 0,
"notify": 1 if notify else 0,
"released_at": released_at,
},
headers={
"Content-Type": "application/octet-stream",
"X-PIO-Content-SHA256": fs.calculate_file_hashsum(
"sha256", archive_path
),
},
data=fp,
2022-01-04 14:45:14 +02:00
x_with_authorization=True,
)
2020-05-27 01:10:35 +03:00
def unpublish_package( # pylint: disable=redefined-builtin
2021-04-21 20:51:54 +03:00
self, owner, type, name, version=None, undo=False
2020-05-27 14:30:27 +03:00
):
2020-07-24 20:57:18 +03:00
path = "/v3/packages/%s/%s/%s" % (owner, type, name)
2020-05-27 01:10:35 +03:00
if version:
2020-07-25 17:13:05 +03:00
path += "/" + version
2022-01-04 14:45:14 +02:00
return self.fetch_json_data(
"delete", path, params={"undo": 1 if undo else 0}, x_with_authorization=True
2020-05-27 01:10:35 +03:00
)
def update_resource(self, urn, private):
2022-01-04 14:45:14 +02:00
return self.fetch_json_data(
2020-09-09 16:27:36 +03:00
"put",
"/v3/resources/%s" % urn,
data={"private": int(private)},
2022-01-04 14:45:14 +02:00
x_with_authorization=True,
)
def grant_access_for_resource(self, urn, client, level):
2022-01-04 14:45:14 +02:00
return self.fetch_json_data(
"put",
"/v3/resources/%s/access" % urn,
data={"client": client, "level": level},
2022-01-04 14:45:14 +02:00
x_with_authorization=True,
)
def revoke_access_from_resource(self, urn, client):
2022-01-04 14:45:14 +02:00
return self.fetch_json_data(
2020-09-09 16:27:36 +03:00
"delete",
"/v3/resources/%s/access" % urn,
data={"client": client},
2022-01-04 14:45:14 +02:00
x_with_authorization=True,
)
2020-06-17 18:55:40 +03:00
def list_resources(self, owner):
2022-01-04 14:45:14 +02:00
return self.fetch_json_data(
"get",
"/v3/resources",
params={"owner": owner} if owner else None,
2022-04-20 18:03:55 +03:00
x_cache_valid="1h",
2022-01-04 14:45:14 +02:00
x_with_authorization=True,
2020-06-17 18:55:40 +03:00
)
2020-07-31 15:42:26 +03:00
2022-03-30 14:43:02 +03:00
def list_packages(self, query=None, qualifiers=None, page=None, sort=None):
2020-07-31 15:42:26 +03:00
search_query = []
2022-03-30 14:43:02 +03:00
if qualifiers:
valid_qualifiers = (
2020-07-31 15:42:26 +03:00
"authors",
"keywords",
"frameworks",
"platforms",
"headers",
"ids",
"names",
"owners",
"types",
)
2022-03-30 14:43:02 +03:00
assert set(qualifiers.keys()) <= set(valid_qualifiers)
for name, values in qualifiers.items():
2020-07-31 15:42:26 +03:00
for value in set(
values if isinstance(values, (list, tuple)) else [values]
):
search_query.append('%s:"%s"' % (name[:-1], value))
2020-07-31 15:42:26 +03:00
if query:
search_query.append(query)
params = dict(query=" ".join(search_query))
2020-07-31 15:42:26 +03:00
if page:
params["page"] = int(page)
2022-03-30 14:43:02 +03:00
if sort:
params["sort"] = sort
2022-01-04 14:45:14 +02:00
return self.fetch_json_data(
"get",
"/v3/search",
params=params,
2022-01-04 14:53:34 +02:00
x_cache_valid="1h",
2022-01-04 14:45:14 +02:00
x_with_authorization=self.allowed_private_packages(),
2020-08-22 17:52:12 +03:00
)
2020-07-31 15:42:26 +03:00
2023-02-02 17:46:27 +02:00
def get_package(self, type_, owner, name, version=None, extra_path=None):
try:
2022-01-04 14:45:14 +02:00
return self.fetch_json_data(
"get",
2023-02-02 17:46:27 +02:00
"/v3/packages/{owner}/{type}/{name}{extra_path}".format(
type=type_,
owner=owner.lower(),
name=name.lower(),
extra_path=extra_path or "",
),
params=dict(version=version) if version else None,
2022-01-04 14:53:34 +02:00
x_cache_valid="1h",
2022-01-04 14:45:14 +02:00
x_with_authorization=self.allowed_private_packages(),
)
2022-07-02 18:37:57 +03:00
except HTTPClientError as exc:
if exc.response is not None and exc.response.status_code == 404:
return None
2022-07-02 18:37:57 +03:00
raise exc