Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
 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)
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
 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
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
    def link(self, dataset, relationship, source, destination, values=dict()):
        assert isinstance(source, (Record, DataPackage)), "source must be an object of type Record or DataPackage"
        assert isinstance(destination, (Record, DataPackage)), "destination must be an object of type Record or DataPackage"

        if isinstance(source, DataPackage):
            assert isinstance(destination, Record), "DataPackages can only be linked to Records"
            return self.session.concepts.proxies.create(dataset, source.id, relationship, destination, values, "ToTarget", "package")
        elif isinstance(destination, DataPackage):
            assert isinstance(source, Record), "DataPackages can only be linked to Records"
            return self.session.concepts.proxies.create(dataset, destination.id, relationship, source, values, "FromTarget", "package")
        else:
            dataset_id = self._get_id(dataset)
            relationship_type = self._get_relationship_type(relationship)
            values = [dict(name=k, value=v) for k,v in list(values.items())]
            instance = Relationship(dataset_id=dataset_id, type=relationship_type, source=source, destination=destination, values=values)
            return self.create(dataset, instance)
Exemplo n.º 10
0
 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