-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (59 loc) · 2.84 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import logging
from fastapi import FastAPI, Request, Response
import httpx
import oai_server
import re
from metadata_provider import BASE_URL
from utils import convert_to_dcat_ap, convert_to_dcat_ap_it, serialize_and_concatenate_graphs
# Logging config
logging.basicConfig(level=logging.DEBUG)
# Initialize a FastAPI app to serve the OAI-PMH endpoint
app = FastAPI()
# Fetch data from an endpoint with httpx (used to get the data from the metadata from the datalake)
def fetch_data(url):
with httpx.Client() as client:
response = client.get(url)
response.raise_for_status()
return response.json()
# Define OAI-PMH endpoint route
@app.get("/oai/{dataset_id}")
@app.post("/oai/{dataset_id}")
def oai(request: Request, dataset_id: str):
params = dict(request.query_params)
# Add dataset_id to the parameters as "set_", which is a parameter from the OAI-PMH protocol
params['set'] = dataset_id
# Making sure it uses the dcat_ap metadata prefix
if 'metadataPrefix' not in params:
params['metadataPrefix'] = 'dcat_ap'
# handleRequest points the request to the appropriate method in metadata_provider.py
response = oai_server.oai_server.handleRequest(params)
logging.debug(f"OAI-PMH Response: {response}")
# Replace date in datestamp by empty string
response = re.sub(b'<datestamp>.*</datestamp>', b'', response)
return Response(content=response, media_type="text/xml")
# Define an endpoint for getting all the datasets
@app.get("/oai")
@app.post("/oai")
def oai_all_datasets(request: Request):
params = dict(request.query_params)
# Making sure it uses the dcat_ap metadata prefix
if 'metadataPrefix' not in params:
params['metadataPrefix'] = 'dcat_ap'
# handleRequest points the request to the appropriate method in metadata_provider.py
response = oai_server.oai_server.handleRequest(params)
logging.debug(f"OAI-PMH Response: {response}")
# Replace date in datestamp by empty string
response = re.sub(b'<datestamp>.*</datestamp>', b'', response)
return Response(content=response, media_type="text/xml")
# Endpoint for generating DCAT-AP IT catalog
@app.get("/dcatapit")
def dcatapit(request: Request):
data = fetch_data(BASE_URL)
#dcatap_graph = convert_to_dcat_ap(data, BASE_URL)
#dcatapit_graph = convert_to_dcat_ap_it(data, BASE_URL)
catalog_graph, datasets_graph, distributions_graph, vcard_graph = convert_to_dcat_ap_it(data, BASE_URL)
#response = dcatapit_graph.serialize(format='pretty-xml')
response = serialize_and_concatenate_graphs(catalog_graph, datasets_graph, distributions_graph, vcard_graph)
return Response(content=response, media_type="application/rdf+xml")
# To run, use uvicorn main:app --reload
# To use the OAI-PMH listRecords method, send a request to the following URL: http://localhost:8000/oai?verb=ListRecords