Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add raise for sparql exceptions #39

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions qwikidata/sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
WIKIDATA_SPARQL_URL = "https://query.wikidata.org/sparql"


class SPARQLResponseNotOk(Exception):
pass


def return_sparql_query_results(
query_string: str, wikidata_sparql_url: str = WIKIDATA_SPARQL_URL
) -> Dict:
Expand All @@ -19,9 +23,17 @@ def return_sparql_query_results(
wikidata_sparql_url: str, optional
wikidata SPARQL endpoint to use
"""
return requests.get(
resp = requests.get(
wikidata_sparql_url, params={"query": query_string, "format": "json"}
).json()
)
if resp.status_code != 200:
raise SPARQLResponseNotOk(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd probably prefer the caller to use a try/except or maybe something like this here (https://docs.python-requests.org/en/latest/user/quickstart/#response-status-codes) ... what do you thing?

"The wikidata SPARQL endpoint returned an error.\n"
"Status code: {}\n"
"Response: {}".format(resp.status_code, resp.text)
)

return resp.json()


def get_subclasses_of_item(item_id: str, return_qids: bool = True) -> Union[List[str], Dict]:
Expand Down