1+ # Copyright 2022 Google Inc.
2+ #
3+ # Licensed under the Apache License, Version 2.0 (the "License");
4+ # you may not use this file except in compliance with the License.
5+ # You may obtain a copy of the License at
6+ #
7+ # http://www.apache.org/licenses/LICENSE-2.0
8+ #
9+ # Unless required by applicable law or agreed to in writing, software
10+ # distributed under the License is distributed on an "AS IS" BASIS,
11+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ # See the License for the specific language governing permissions and
13+ # limitations under the License.
14+ """ API to request node information.
15+ """
16+
17+ from typing import Dict , List
18+
19+ from datacommons .requests import _post
20+ from datacommons .utils import _get_direction
21+
22+
23+ def properties (nodes : List [str ], is_out : bool = True ) -> Dict [str , List [str ]]:
24+ """Retrieves all the properties for a list of nodes.
25+
26+ Note this only returns the property labels, not the values.
27+ Args:
28+ nodes: List of DCIDs.
29+ is_out: Whether to return out going properties.
30+ Returns:
31+ A dict keyed by node DCID, with the values being a list of properties
32+ for the queried node.
33+ """
34+ resp = _post (f'/v1/bulk/properties/{ _get_direction (is_out )} ' , {
35+ 'nodes' : nodes ,
36+ })
37+ result = {}
38+ for item in resp .get ('data' , []):
39+ node , properties = item ['node' ], item .get ('properties' , [])
40+ result [node ] = properties
41+ return result
42+
43+
44+ def property_values (nodes : List [str ],
45+ property : str ,
46+ is_out : bool = True ) -> Dict [str , List [str ]]:
47+ """Retrieves the property values for a list of nodes.
48+ Args:
49+ nodes: List of DCIDs.
50+ property: The property label to query for.
51+ is_out: Whether the property is out going.
52+ Returns:
53+ A dict keyed by node DCID, with the values being a list of values
54+ for the queried property.
55+ """
56+ resp = _post (f'/v1/bulk/property/values/{ _get_direction (is_out )} ' , {
57+ 'nodes' : nodes ,
58+ 'property' : property ,
59+ })
60+ result = {}
61+ for item in resp .get ('data' , []):
62+ node , values = item ['node' ], item .get ('values' , [])
63+ result [node ] = []
64+ for v in values :
65+ if 'dcid' in v :
66+ result [node ].append (v ['dcid' ])
67+ else :
68+ result [node ].append (v ['value' ])
69+ return result
70+
71+
72+ def triples (nodes : List [str ],
73+ is_out : bool = True ) -> Dict [str , Dict [str , List [object ]]]:
74+ """Retrieves the triples for a node.
75+ Args:
76+ nodes: List of DCIDs.
77+ is_out: Whether the returned property is out going for the queried
78+ nodes.
79+ Returns:
80+ A two level dict keyed by node DCID, then by the arc property, with
81+ a list of values or DCIDs.
82+ """
83+ resp = _post (f'/v1/bulk/triples/{ _get_direction (is_out )} ' ,
84+ data = {'nodes' : nodes })
85+ result = {}
86+ for item in resp .get ('data' , []):
87+ node , triples = item ['node' ], item .get ('triples' , {})
88+ result [node ] = {}
89+ for property , other_nodes in triples .items ():
90+ result [node ][property ] = other_nodes .get ('nodes' , [])
91+ return result
0 commit comments