Skip to content

Commit

Permalink
Added multiple_property_values to fetch helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dwnoble committed Nov 16, 2024
1 parent 4299a7f commit df5bbcc
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions server/lib/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,45 @@ def property_values(nodes, prop, out=True, constraints=''):
return result


def multiple_property_values(nodes: List[str],
props: List[str],
out=True) -> Dict[str, Dict[str, List[str]]]:
"""
Fetches specified properties for given nodes using the /v2/node API.
Args:
nodes (List[str]): List of node identifiers.
props (List[str]): Properties to retrieve for each node.
out (bool): If True, fetches outgoing properties; otherwise, incoming (default: True).
Returns:
dict: A dictionary mapping each node to its properties and their values.
Example:
nodes = ["geoId/06", "country/USA"]
props = ["typeOf", "name"]
result = multiple_property_values(nodes, props)
# Example result:
# {
# "geoId/06": {"typeOf": ["State", "AdministrativeArea1], "name": ["California"]},
# "country/USA": {"typeOf": ["Country"], "name": ["United States of America"]}
# }
"""
props_expression = f"[{', '.join(props)}]"
resp = dc.v2node(nodes, '{}{}'.format('->' if out else '<-',
props_expression))

# Parse response into a structured dictionary
result = {}
for node in nodes:
resp_node_arcs = resp.get('data', {}).get(node).get('arcs', {})
result[node] = {}
for prop in props:
prop_nodes = resp_node_arcs.get(prop, {}).get('nodes', [])
result[node][prop] = [p.get('dcid') or p.get('value') for p in prop_nodes]
return result


def raw_property_values(nodes, prop, out=True, constraints=''):
"""Returns full property values data out of REST API response.
Expand Down

0 comments on commit df5bbcc

Please sign in to comment.