示例#1
0
 def get(self, dataset, concept):
     dataset_id = self._get_id(dataset)
     concept_id = self._get_id(concept)
     r = self._get(self._uri('/{dataset_id}/concepts/{id}', dataset_id=dataset_id, id=concept_id))
     r['dataset_id'] = r.get('dataset_id', dataset_id)
     r['schema'] = self.get_properties(dataset, concept)
     return Model.from_dict(r, api=self.session)
示例#2
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
示例#3
0
 def get_all(self, dataset):
     dataset_id = self._get_id(dataset)
     resp = self._get(self._uri('/{dataset_id}/concepts', dataset_id=dataset_id), stream=True)
     for r in resp:
         r['dataset_id'] = r.get('dataset_id', dataset_id)
         r['schema']     = self.get_properties(dataset, r['id'])
     concepts = [Model.from_dict(r, api=self.session) for r in resp]
     return { c.type: c for c in concepts }
示例#4
0
 def create(self, dataset, concept):
     assert isinstance(concept, Model), "concept must be type Model"
     dataset_id = self._get_id(dataset)
     r = self._post(self._uri('/{dataset_id}/concepts', dataset_id=dataset_id), json=concept.as_dict())
     concept.id = r['id']
     r['dataset_id'] = r.get('dataset_id', dataset_id)
     if concept.schema:
         r['schema'] = self.update_properties(dataset, concept)
     return Model.from_dict(r, api=self.session)
示例#5
0
 def get(self, dataset, concept):
     dataset_id = self._get_id(dataset)
     concept_id = self._get_id(concept)
     r = self._get(
         self._uri("/{dataset_id}/concepts/{id}",
                   dataset_id=dataset_id,
                   id=concept_id))
     r["dataset_id"] = r.get("dataset_id", dataset_id)
     r["schema"] = self.get_properties(dataset, concept)
     r["linked"] = self.get_linked_properties(dataset, concept)
     return Model.from_dict(r, api=self.session)
示例#6
0
 def get_all(self, dataset):
     dataset_id = self._get_id(dataset)
     resp = self._get(self._uri('/{dataset_id}/concepts', dataset_id=dataset_id), stream=True)
     for r in resp:
         r['dataset_id'] = r.get('dataset_id', dataset_id)
         r['schema'] = self.get_properties(dataset, r['id'])
         r['linked'] = self.get_linked_properties(dataset, r['id'])
     concepts = [Model.from_dict(r, api=self.session) for r in resp]
     # for concept in concepts:
     #     concept.linked = {x.name: x for x in self.get_linked_properties(dataset, concept)}
     return { c.type: c for c in concepts }
示例#7
0
 def get_related(self, dataset, concept):
     """ Return all SchemaRelationships and the Concepts they point to """
     dataset_id = self._get_id(dataset)
     concept_id = self._get_id(concept)
     resp = self._get(
         self._uri('/{dataset_id}/concepts/{concept_id}/topology',
                   dataset_id=dataset_id, concept_id=concept_id))
     for r in resp:
         r['dataset_id'] = r.get('dataset_id', dataset_id)
         r['schema'] = self.get_properties(dataset, r['id'])
         r['linked'] = self.get_linked_properties(dataset, r['id'])
     concepts = [Model.from_dict(r, api=self.session) for r in resp]
     return concepts
示例#8
0
 def update(self, dataset, concept):
     assert isinstance(concept, Model), "concept must be type Model"
     data = concept.as_dict()
     data['id'] = concept.id
     dataset_id = self._get_id(dataset)
     r = self._put(self._uri('/{dataset_id}/concepts/{id}', dataset_id=dataset_id, id=concept.id), json=data)
     r['dataset_id'] = r.get('dataset_id', dataset_id)
     if concept.schema:
         r['schema'] = self.update_properties(dataset, concept)
     if concept.linked:
         r['linked'] = {name: self.update_linked_property(dataset, concept, link) for name,link in concept.linked.items()}
     updated = Model.from_dict(r, api=self.session)
     return updated
示例#9
0
    def get_connected(self, dataset, model):
        """ Return a list of concepts related to the given model """
        dataset_id = self._get_id(dataset)
        model_id = self._get_id(model)
        resp = self._get(self._uri('/{dataset_id}/concepts/{model_id}/related',
                                   dataset_id=dataset_id,
                                   model_id=model_id),
                         stream=True)
        for r in resp:
            r['dataset_id'] = r.get('dataset_id', dataset_id)
            r['schema'] = self.get_properties(dataset, r['id'])
            r['linked'] = self.get_linked_properties(dataset, r['id'])

        concepts = [Model.from_dict(r, api=self.session) for r in resp]
        return {c.type: c for c in concepts}
示例#10
0
    def create(self, dataset, concept):
        assert isinstance(concept, Model), "concept must be type Model"
        dataset_id = self._get_id(dataset)
        r = self._post(self._uri('/{dataset_id}/concepts', dataset_id=dataset_id), json=concept.as_dict())
        concept.id = r['id']
        r['dataset_id'] = r.get('dataset_id', dataset_id)

        if concept.schema:
            try:
                r['schema'] = self.update_properties(dataset, concept)
            except requests.exceptions.HTTPError as e:
                if e.response.status_code == 400:
                    self._logger.error(
                        "Model was successfully created, but properties were "
                        "rejected with the following error: {}".format(str(
                            e.response.text.split('\n')[0]
                        ))
                    )
                else:
                    raise
        return Model.from_dict(r, api=self.session)
示例#11
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
示例#12
0
    def create(self, dataset, concept):
        assert isinstance(concept, Model), "concept must be type Model"
        dataset_id = self._get_id(dataset)
        r = self._post(
            self._uri("/{dataset_id}/concepts", dataset_id=dataset_id),
            json=concept.as_dict(),
        )
        concept.id = r["id"]
        r["dataset_id"] = r.get("dataset_id", dataset_id)

        if concept.schema:
            try:
                r["schema"] = self.update_properties(dataset, concept)
            except requests.exceptions.HTTPError as e:

                # If properties can not be created, roll back model creation so
                # the user can try again.
                self.delete(dataset, concept)

                raise Exception("Could not create model properties: {}".format(
                    e.response.json().get("detail", e.response.json())))

        return Model.from_dict(r, api=self.session)