def best_match( self, query, block_attrs={}, domain=None, scorer=None, return_canonical=True, ): if domain is None: domain = self.domain if scorer is None: scorer = self.scorer query_field = list(query.keys())[0] data = { "block_attrs": block_attrs, "query_field": query_field, "query_value": query[query_field], "return_canonical": return_canonical, } response = requests.post( urljoin( self.service_address, "domains/{}/entities/best-match/".format(domain), ), headers=self.headers, data=encode(data), ) if response.status_code != requests.codes.ok: raise BadResponse("The service responded with a {}: {}".format( response.status_code, response.content)) data = response.json() if data == {}: return None return EntityObject(response.json(), client=self)
def match_or_create( self, query, block_attrs={}, create_attrs={}, domain=None, return_canonical=True, ): if domain is None: domain = self.domain query_field = list(query.keys())[0] data = { "query_field": query_field, "query_value": query[query_field], "block_attrs": block_attrs, "create_attrs": create_attrs, "return_canonical": return_canonical, } response = requests.post( urljoin( self.service_address, "domains/{}/entities/match-or-create/".format(domain), ), headers=self.headers, data=encode(data), ) if response.status_code == 403: raise UnspecificQueryError( "Query in request matched more than one entity.") if response.status_code != requests.codes.ok: raise BadResponse("The service responded with a {}: {}".format( response.status_code, response.content)) return EntityObject(response.json(), client=self)
def update_by_id(self, uuid, update_attrs): response = requests.patch( urljoin(self.service_address, "entities/{}/".format(uuid)), headers=self.headers, data=encode({"attributes": update_attrs}), ) if response.status_code != requests.codes.ok: raise BadResponse("The service responded with a {}: {}".format( response.status_code, response.content)) return EntityObject({"entity": response.json()}, client=self)
def alias_by_id(self, alias_uuid, alias_for_uuid): response = requests.patch( urljoin(self.service_address, "entities/{}/".format(alias_uuid)), headers=self.headers, data=encode({"alias_for": alias_for_uuid}), ) if response.status_code != requests.codes.ok: raise BadResponse( "The service responded with a {}: {}".format( response.status_code, response.content ) ) return EntityObject({"entity": response.json()}, client=self)
def update_match(self, block_attrs, update_attrs, domain=None): if domain is None: domain = self.domain data = {"block_attrs": block_attrs, "update_attrs": update_attrs} response = requests.post( urljoin( self.service_address, "domains/{}/entities/update-match/".format(domain), ), headers=self.headers, data=encode(data), ) if response.status_code == 403: raise UnspecificQueryError( "Query in request matched more than one entity.") if response.status_code == 400: raise UpdateEntityError(response.content) if response.status_code != requests.codes.ok: raise BadResponse("The service responded with a {}: {}".format( response.status_code, response.content)) return EntityObject(response.json(), client=self)
def delete_match(self, block_attrs, domain=None): if not isinstance(block_attrs, dict): raise BadRequest("block_attrs must be a dictionary of attributes.") if domain is None: domain = self.domain data = block_attrs response = requests.post( urljoin( self.service_address, "domains/{}/entities/delete-match/".format(domain), ), headers=self.headers, data=encode(data), ) if response.status_code == 403: raise UnspecificQueryError( "Query in request matched more than one entity.") if response.status_code != 204: raise BadResponse("The service responded with a {}: {}".format( response.status_code, response.content)) return response.status_code == 204
def create(self, entity, domain=None): if domain is None: domain = self.domain response = requests.post( urljoin( self.service_address, "domains/{}/entities/bulk-create/".format(domain), ), headers=self.headers, data=encode([entity]), ) if response.status_code == 400: raise CreateEntityError("Error creating entity: {}".format( response.content)) if response.status_code != requests.codes.ok: raise BadResponse("The service responded with a {}: {}".format( response.status_code, response.content)) return [ EntityObject(entity, client=self) for entity in response.json()["entities"] ][0]
def alias_or_create( self, query, block_attrs={}, create_attrs={}, domain=None, threshold=None, return_canonical=True, ): if domain is None: domain = self.domain if threshold is None: threshold = self.threshold query_field = list(query.keys())[0] data = { "query_field": query_field, "query_value": query[query_field], "threshold": threshold, "block_attrs": block_attrs, "create_attrs": create_attrs, "return_canonical": return_canonical, } response = requests.post( urljoin( self.service_address, "domains/{}/entities/alias-or-create/".format(domain), ), headers=self.headers, data=encode(data), ) if response.status_code == 404: raise CreateEntityError("Error creating entities: {}".format( response.content)) if response.status_code != requests.codes.ok: raise BadResponse("The service responded with a {}: {}".format( response.status_code, response.content)) return EntityObject(response.json(), client=self)