def run(self): """ Run the constructed query. Returns: A list of matching Record instances. """ resp = self.query_api._post( self.query_api._uri("/{dataset_id}/query/run", dataset_id=self.dataset_id), json=self._build_query(), ) if resp is None: return [] records = [] for r in resp: # get the target first: target_value = r["targetValue"] target_value["dataset_id"] = self.dataset_id target = Record.from_dict(target_value, api=self.query_api.session) # then any attached records by join type: joined = {} if self._select is not None: for join_key in self._select.join_keys: if join_key in r: join_value = r[join_key] join_value["dataset_id"] = self.dataset_id joined[join_key] = Record.from_dict( join_value, api=self.query_api.session) records.append(QueryResult(self.dataset_id, target, joined)) return records
def relations(self, dataset, instance, related_concept, concept=None): dataset_id = self._get_id(dataset) instance_id = self._get_id(instance) related_concept_type = self._get_id(related_concept) concept_type = self._get_concept_type(concept, instance) res = self._get( self._uri( "/{dataset_id}/concepts/{concept_type}/instances/{id}/relations/{related_concept_type}", dataset_id=dataset_id, concept_type=concept_type, id=instance_id, related_concept_type=related_concept_type, )) relations = [] for r in res: relationship = r[0] concept = r[1] relationship["dataset_id"] = relationship.get( "dataset_id", dataset_id) concept["dataset_id"] = concept.get("dataset_id", dataset_id) relationship = Relationship.from_dict(relationship, api=self.session) concept = Record.from_dict(concept, api=self.session) relations.append((relationship, concept)) return relations
def create(self, dataset, instance): assert isinstance(instance, Record), "instance must be type Record" dataset_id = self._get_id(dataset) r = self._post( self._uri( "/{dataset_id}/concepts/{concept_type}/instances", dataset_id=dataset_id, concept_type=instance.type, ), json=instance.as_dict(), ) r["dataset_id"] = r.get("dataset_id", dataset_id) return Record.from_dict(r, api=self.session)
def get(self, dataset, instance, concept=None): dataset_id = self._get_id(dataset) instance_id = self._get_id(instance) concept_type = self._get_concept_type(concept, instance) r = self._get( self._uri( "/{dataset_id}/concepts/{concept_type}/instances/{id}", dataset_id=dataset_id, concept_type=concept_type, id=instance_id, )) r["dataset_id"] = r.get("dataset_id", dataset_id) return Record.from_dict(r, api=self.session)
def get_all(self, dataset, concept, limit=100, offset=0): dataset_id = self._get_id(dataset) concept_type = self._get_concept_type(concept) resp = self._get( self._uri( "/{dataset_id}/concepts/{concept_type}/instances", dataset_id=dataset_id, concept_type=concept_type, ), params=dict(limit=limit, offset=offset), stream=True, ) for r in resp: r["dataset_id"] = r.get("dataset_id", dataset_id) instances = [Record.from_dict(r, api=self.session) for r in resp] return RecordSet(concept, instances)
def get_all_related_of_type(self, dataset, source_instance, return_type, source_concept=None): """ Return all records of type return_type related to instance. """ dataset_id = self._get_id(dataset) instance_id = self._get_id(source_instance) instance_type = self._get_concept_type(source_concept, source_instance) resp = [] limit = 100 for offset in itertools.count(0, limit): batch = self._get( self._uri( "/{dataset_id}/concepts/{instance_type}/instances/{instance_id}/relations/{return_type}", dataset_id=dataset_id, instance_type=instance_type, instance_id=instance_id, return_type=return_type, ), params={ "limit": limit, "offset": offset }, ) if not batch: break resp += batch for edge, node in resp: node["dataset_id"] = node.get("dataset_id", dataset_id) if not isinstance(return_type, Model): return_type = self.session.concepts.get(dataset, return_type) records = [Record.from_dict(r, api=self.session) for _, r in resp] return RecordSet(return_type, records)
def create_many(self, dataset, concept, *instances): instance_type = instances[0].type for inst in instances: assert isinstance(inst, Record), "instance must be type Record" assert ( inst.type == instance_type ), "Expected instance of type {}, found instance of type {}".format( instance_type, inst.type) dataset_id = self._get_id(dataset) values = [inst.as_dict() for inst in instances] resp = self._post( self._uri( "/{dataset_id}/concepts/{concept_type}/instances/batch", dataset_id=dataset_id, concept_type=instance_type, ), json=values, stream=True, ) for r in resp: r["dataset_id"] = r.get("dataset_id", dataset_id) instances = [Record.from_dict(r, api=self.session) for r in resp] return RecordSet(concept, instances)