Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/cli/client.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from __future__ import annotations | |||||
import logging | import logging | ||||
# WARNING: do not import unnecessary things here to keep cli startup time under | # WARNING: do not import unnecessary things here to keep cli startup time under | ||||
# control | # control | ||||
import os | import os | ||||
import sys | import sys | ||||
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple | from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple | ||||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | ) -> str: | ||||
logging.debug("Metadata dict to generate as xml: %s", codemetadata) | logging.debug("Metadata dict to generate as xml: %s", codemetadata) | ||||
s = xmltodict.unparse(codemetadata, pretty=True) | s = xmltodict.unparse(codemetadata, pretty=True) | ||||
logging.debug("Metadata dict as xml generated: %s", s) | logging.debug("Metadata dict as xml generated: %s", s) | ||||
with open(path, "w") as fp: | with open(path, "w") as fp: | ||||
fp.write(s) | fp.write(s) | ||||
return path | return path | ||||
def _client(url: str, username: str, password: str) -> "PublicApiDepositClient": | def _client(url: str, username: str, password: str) -> PublicApiDepositClient: | ||||
"""Instantiate a client to access the deposit api server | """Instantiate a client to access the deposit api server | ||||
Args: | Args: | ||||
url (str): Deposit api server | url (str): Deposit api server | ||||
username (str): User | username (str): User | ||||
password (str): User's password | password (str): User's password | ||||
""" | """ | ||||
from swh.deposit.client import PublicApiDepositClient | from swh.deposit.client import PublicApiDepositClient | ||||
return PublicApiDepositClient( | return PublicApiDepositClient( | ||||
{"url": url, "auth": {"username": username, "password": password},} | {"url": url, "auth": {"username": username, "password": password},} | ||||
) | ) | ||||
def _collection(client: "PublicApiDepositClient") -> str: | def _collection(client: PublicApiDepositClient) -> str: | ||||
"""Retrieve the client's collection | """Retrieve the client's collection | ||||
""" | """ | ||||
# retrieve user's collection | # retrieve user's collection | ||||
sd_content = client.service_document() | sd_content = client.service_document() | ||||
if "error" in sd_content: | if "error" in sd_content: | ||||
raise InputError("Service document retrieval: %s" % (sd_content["error"],)) | raise InputError("Service document retrieval: %s" % (sd_content["error"],)) | ||||
collection = sd_content["service"]["workspace"]["collection"]["sword:name"] | collection = sd_content["service"]["workspace"]["collection"]["sword:name"] | ||||
▲ Show 20 Lines • Show All 391 Lines • Show Last 20 Lines |