def get(self, dataset, instance, relationship=None): dataset_id = self._get_id(dataset) instance_id = self._get_id(instance) relationship_type = self._get_relationship_type(relationship, instance) r = self._get( self._uri('/{dataset_id}/relationships/{r_type}/instances/{id}', dataset_id=dataset_id, r_type=relationship_type, id=instance_id)) r['dataset_id'] = r.get('dataset_id', dataset_id) return Relationship.from_dict(r, api=self.session)
def create(self, dataset, external_id, relationship, concept_instance, values, direction = "ToModel", proxy_type = "package", concept = None): assert proxy_type in self.proxy_types, "proxy_type must be one of {}".format(self.proxy_types) assert direction in self.direction_types, "direction must be one of {}".format(self.direction_types) dataset_id = self._get_id(dataset) concept_instance_id = self._get_id(concept_instance) concept_type = self._get_concept_type(concept, concept_instance) relationship_type = self._get_relationship_type(relationship) relationshipData = [dict(name=k, value=v) for k,v in list(values.items())] request = {} request['externalId'] = external_id request['conceptType'] = concept_type request['conceptInstanceId'] = concept_instance_id request['targets'] = [ { 'direction': direction, 'linkTarget': { "ConceptInstance": { 'id': concept_instance_id } }, 'relationshipType': relationship_type, 'relationshipData': relationshipData } ] r = self._post(self._uri('/{dataset_id}/proxy/{p_type}/instances', dataset_id=dataset_id, p_type=proxy_type), json=request) instance = r[0]['relationshipInstance'] instance['dataset_id'] = instance.get('dataset_id', dataset_id) return Relationship.from_dict(instance, api=self.session)
def create(self, dataset, instance): assert isinstance(instance, Relationship), "instance must be of type Relationship" dataset_id = self._get_id(dataset) resp = self._post( self._uri('/{dataset_id}/relationships/{r_type}/instances', dataset_id=dataset_id, r_type=instance.type), json=instance.as_dict()) r = resp[0] # responds with list r['dataset_id'] = r.get('dataset_id', dataset_id) return Relationship.from_dict(r, api=self.session)
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 get_topology(self, dataset): dataset_id = self._get_id(dataset) resp = self._get( self._uri('/{dataset_id}/concepts/schema/graph', dataset_id=dataset_id)) # What is returned is a list mixing results = { 'models': [], 'relationships': [], 'linked_properties': [] } for r in resp: r['dataset_id'] = r.get('dataset_id', dataset_id) if r.get('type') == 'schemaRelationship': # This is a relationship results['relationships'].append( Relationship.from_dict(r, api=self.session)) elif r.get('type') == 'schemaLinkedProperty': # This is a linked property type results['linked_properties'].append( LinkedModelProperty.from_dict(r)) else: # This is a model r['schema'] = self.get_properties(dataset, r['id']) r['linked'] = self.get_linked_properties(dataset, r['id']) results['models'].append(Model.from_dict(r, api=self.session)) return results
def get_all(self, dataset, relationship): dataset_id = self._get_id(dataset) relationship_id = self._get_id(relationship) resp = self._get(self._uri('/{dataset_id}/relationships/{r_id}/instances', dataset_id=dataset_id, r_id=relationship_id), stream=True) for r in resp: r['dataset_id'] = r.get('dataset_id', dataset_id) instances = [Relationship.from_dict(r, api=self.session) for r in resp] return RelationshipSet(relationship, instances)
def create_many(self, dataset, relationship, *instances): assert all([isinstance(i, Relationship) for i in instances]), "instances must be of type Relationship" instance_type = instances[0].type dataset_id = self._get_id(dataset) values = [inst.as_dict() for inst in instances] resp = self._post( self._uri('/{dataset_id}/relationships/{r_type}/instances/batch', dataset_id=dataset_id, r_type=instance_type), json=values) for r in resp: r[0]['dataset_id'] = r[0].get('dataset_id', dataset_id) instances = [Relationship.from_dict(r[0], api=self.session) for r in resp] return RelationshipSet(relationship, instances)
def create( self, dataset, external_id, relationship, concept_instance, values, direction="ToTarget", proxy_type="package", concept=None, ): assert proxy_type in self.proxy_types, "proxy_type must be one of {}".format( self.proxy_types) assert direction in self.direction_types, "direction must be one of {}".format( self.direction_types) dataset_id = self._get_id(dataset) concept_instance_id = self._get_id(concept_instance) concept_type = self._get_concept_type(concept, concept_instance) relationship_type = self._get_relationship_type(relationship) relationshipData = [dict(name=k, value=v) for k, v in values.items()] request = {} request["externalId"] = external_id request["conceptType"] = concept_type request["conceptInstanceId"] = concept_instance_id request["targets"] = [{ "direction": direction, "linkTarget": { "ConceptInstance": { "id": concept_instance_id } }, "relationshipType": relationship_type, "relationshipData": relationshipData, }] r = self._post( self._uri( "/{dataset_id}/proxy/{p_type}/instances", dataset_id=dataset_id, p_type=proxy_type, ), json=request, ) instance = r[0]["relationshipInstance"] instance["dataset_id"] = instance.get("dataset_id", dataset_id) return Relationship.from_dict(instance, api=self.session)
def get_topology(self, dataset): dataset_id = self._get_id(dataset) resp = self._get( self._uri("/{dataset_id}/concepts/schema/graph", dataset_id=dataset_id)) # What is returned is a list mixing results = {"models": [], "relationships": [], "linked_properties": []} for r in resp: r["dataset_id"] = r.get("dataset_id", dataset_id) if r.get("type") == "schemaRelationship": # This is a relationship results["relationships"].append( Relationship.from_dict(r, api=self.session)) elif r.get("type") == "schemaLinkedProperty": # This is a linked property type results["linked_properties"].append( LinkedModelProperty.from_dict(r)) else: # This is a model r["schema"] = self.get_properties(dataset, r["id"]) r["linked"] = self.get_linked_properties(dataset, r["id"]) results["models"].append(Model.from_dict(r, api=self.session)) return results