diff --git a/src/qlever/commands/example_queries.py b/src/qlever/commands/example_queries.py index ce7296509..da0cbe4e0 100644 --- a/src/qlever/commands/example_queries.py +++ b/src/qlever/commands/example_queries.py @@ -84,6 +84,11 @@ def additional_arguments(self, subparser) -> None: default=False, help="Remove OFFSET and LIMIT from the query", ) + subparser.add_argument( + "--access-token", + type=str, + help="Access token for privileged requests to the SPARQL endpoint", + ) subparser.add_argument( "--accept", type=str, @@ -100,6 +105,17 @@ def additional_arguments(self, subparser) -> None: "`text/turtle` for CONSTRUCT AND DESCRIBE queries, " "`application/sparql-results+json` for all others", ) + subparser.add_argument( + "--content-type", + type=str, + choices=[ + "application/sparql-query", + "application/sparql-update", + ], + default="application/sparql-query", + help="Content-Type header for the SPARQL query " + "(default: application/sparql-query)", + ) subparser.add_argument( "--clear-cache", choices=["yes", "no"], @@ -245,6 +261,7 @@ def execute(self, args) -> bool: self.show( f"Obtain queries via: {get_queries_cmd}\n" f"SPARQL endpoint: {sparql_endpoint}\n" + f"Content-Type header: {args.content_type}\n" f"Accept header: {args.accept}\n" f"Download result for each query or just count:" f" {args.download_or_count.upper()}" @@ -372,22 +389,20 @@ def execute(self, args) -> bool: # Launch query. try: - curl_cmd = ( - f"curl -s {sparql_endpoint}" - f' -w "HTTP code: %{{http_code}}\\n"' - f' -H "Accept: {accept_header}"' - f" --data-urlencode query={shlex.quote(query)}" - ) - log.debug(curl_cmd) - result_file = ( - f"qlever.example_queries.result." - f"{abs(hash(curl_cmd))}.tmp" - ) + if args.content_type == "application/sparql-query": + params = {"query": query} + else: + params = { + "update": query, + "access-token": args.access_token, + } + unique_id = abs(hash(query)) + result_file = f"qlever.example_queries.result.{unique_id}.tmp" start_time = time.time() http_code = run_curl_command( sparql_endpoint, headers={"Accept": accept_header}, - params={"query": query}, + params=params, result_file=result_file, ).strip() if http_code == "200": diff --git a/src/qlever/util.py b/src/qlever/util.py index af3a25c67..95fe17c73 100644 --- a/src/qlever/util.py +++ b/src/qlever/util.py @@ -85,6 +85,7 @@ def run_curl_command( headers: dict[str, str] = {}, params: dict[str, str] = {}, result_file: Optional[str] = None, + log_debug_curl_cmd: bool = True, ) -> str: """ Run `curl` with the given `url`, `headers`, and `params`. If `result_file` @@ -106,6 +107,7 @@ def run_curl_command( ] ) ) + log.debug(curl_cmd) result = subprocess.run( curl_cmd, shell=True,