1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-09-21 17:56:19 +02:00
github.com_openSUSE_osc/osc/_private/api.py

75 lines
2.1 KiB
Python
Raw Normal View History

2022-10-12 15:19:17 +02:00
"""
Functions that communicate with OBS API
and work with related XML data.
"""
from .. import connection as osc_connection
from .. import core as osc_core
def get(apiurl, path, query=None):
"""
Send a GET request to OBS.
:param apiurl: OBS apiurl.
:type apiurl: str
:param path: URL path segments.
:type path: list(str)
:param query: URL query values.
:type query: dict(str, str)
:returns: Parsed XML root.
:rtype: xml.etree.ElementTree.Element
"""
assert apiurl
assert path
if not isinstance(path, (list, tuple)):
raise TypeError("Argument `path` expects a list of strings")
url = osc_core.makeurl(apiurl, path, query)
with osc_connection.http_GET(url) as f:
root = osc_core.ET.parse(f).getroot()
return root
def find_nodes(root, root_name, node_name):
"""
Find nodes with given `node_name`.
Also, verify that the root tag matches the `root_name`.
:param root: Root node.
:type root: xml.etree.ElementTree.Element
:param root_name: Expected (tag) name of the root node.
:type root_name: str
:param node_name: Name of the nodes we're looking for.
:type node_name: str
:returns: List of nodes that match the given `node_name`.
:rtype: list(xml.etree.ElementTree.Element)
"""
assert root.tag == root_name
return root.findall(node_name)
def find_node(root, root_name, node_name=None):
"""
Find a single node with given `node_name`.
If `node_name` is not specified, the root node is returned.
Also, verify that the root tag matches the `root_name`.
:param root: Root node.
:type root: xml.etree.ElementTree.Element
:param root_name: Expected (tag) name of the root node.
:type root_name: str
:param node_name: Name of the nodes we're looking for.
:type node_name: str
:returns: The node that matches the given `node_name`
or the root node if `node_name` is not specified.
:rtype: xml.etree.ElementTree.Element
"""
assert root.tag == root_name
if node_name:
return root.find(node_name)
return root