def query_sparql_endpoint(self, sparql_query):
    sparql_query_id = self.get_unique_id_from_str(sparql_query)
    if (sparql_query_id in self.entity_cache):
        return self.entity_cache[sparql_query_id]
    else:
        wikidata_sparql_url = 'https://query.wikidata.org/sparql'
        try:
            while True:
                res = requests.get(wikidata_sparql_url, params={'query': sparql_query, 'format': 'json'})
                if (res.status_code in (429, 504)):
                    time.sleep(1)
                    continue
                elif (res.status_code == 200):
                    res = res.json()
                    self.entity_cache[sparql_query_id] = res
                    self.save_entity_cache()
                    return res
                else:
                    print(res.status_code)
                    raise Exception
        except json.JSONDecodeError as e:
            print(res, res.__dict__)
            raise e
def query_sparql_endpoint(self, sparql_query):
    sparql_query_id = self.get_unique_id_from_str(sparql_query)
    if (sparql_query_id in self.entity_cache):
        return self.entity_cache[sparql_query_id]
    else:
        wikidata_sparql_url = 'https://query.wikidata.org/sparql'
        try:
            while True:
                res = requests.get(wikidata_sparql_url, params={'query': sparql_query, 'format': 'json'})
                if (res.status_code in (429, 504)):
                    time.sleep(1)
                    continue
                elif (res.status_code == 200):
                    res = res.json()
                    self.entity_cache[sparql_query_id] = res
                    self.save_entity_cache()
                    return res
                else:
                    print(res.status_code)
                    raise Exception
        except json.JSONDecodeError as e:
            print(res, res.__dict__)
            raise e