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

Implement SPARQL POST in remote_sparql repo #290

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Alternatively, set them directly in the environment from which Prez is run.
- **`sparql_endpoint`**: Read-only SPARQL endpoint for Prez. Default is `None`.
- **`sparql_username`**: A username for the Prez SPARQL endpoint, if required by the RDF DB. Default is `None`.
- **`sparql_password`**: A password for the Prez SPARQL endpoint, if required by the RDF DB. Default is `None`.
- **`enable_sparql_endpoint`**: Whether to enable the SPARQL endpoint. I.e. whether prez exposes the remote repository's SPARQL endpoint (typically a triplestore). Default is `False`. NB the SPARQL endpoint when enabled supports POST requests. Prez itself does not make any updates to the remote repository (e.g. the remote Triplestore), however, if the remote SPARQL endpoint is enabled it is then possible that users can make updates to the remote repository using the SPARQL endpoint.

#### Network Configuration

Expand Down Expand Up @@ -193,10 +194,6 @@ Used in conjunction with the Pyoxigraph repo. Specifies a directory (from the re
- **`listing_count_limit`**: The maximum number of items to count for a listing endpoint. Counts greater than this limit will be returned as ">N" where N is the limit. Default is `100`.
- **`search_count_limit`**: The maximum number of items to return in a search result. Default is `10`.

#### SPARQL Endpoint

- **`enable_sparql_endpoint`**: Whether to enable the SPARQL endpoint. Default is `False`.

#### Temporal Configuration

- **`temporal_predicate`**: The predicate used for temporal properties. Default is `sdo:temporal`.
Expand Down
17 changes: 11 additions & 6 deletions prez/repositories/remote_sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,21 @@ async def sparql(
):
"""Sends a starlette Request object (containing a SPARQL query in the URL parameters) to a proxied SPARQL
endpoint."""
# TODO: This only supports SPARQL GET requests because the query is sent as a query parameter.

query_escaped_as_bytes = f"query={quote_plus(query)}".encode("utf-8")

# TODO: Global app settings should be passed in as a function argument.
url = httpx.URL(url=settings.sparql_endpoint, query=query_escaped_as_bytes)
headers = []
for header in raw_headers:
if header[0] != b"host":
headers.append(header)

# TODO: Global app settings should be passed in as a function argument.
if method == 'GET':
query_escaped_as_bytes = f"query={quote_plus(query)}".encode("utf-8")
url = httpx.URL(url=settings.sparql_endpoint, query=query_escaped_as_bytes)
rp_req = self.async_client.build_request(method, url, headers=headers)
else:
url = httpx.URL(url=settings.sparql_endpoint)
rp_req = self.async_client.build_request(method, url, headers=headers, data={'query': query})

headers.append((b"host", str(url.host).encode("utf-8")))
rp_req = self.async_client.build_request(method, url, headers=headers)

return await self.async_client.send(rp_req, stream=True)