|
17 | 17 | # |
18 | 18 | # |
19 | 19 | # FAIR Data Point (FDP) exposes the following endpoints (URL paths): |
20 | | -# [ /, /doc, /doc/ ] = Redirect to the API documentation (Swagger UI) |
21 | | -# /fdp = returns FDP metadata |
22 | | -# /catalog/{catalogID} = returns catalog metadata (default: catalog-01) |
23 | | -# /dataset/{datasetID} = returns dataset metadata (default: breedb) |
24 | | -# /distribution/{distributionID} = returns distribution metadata (default: breedb-sparql) |
| 20 | +# [ /, /doc, /doc/ ] = Redirects to the API documentation |
| 21 | +# /fdp = Returns FDP metadata |
| 22 | +# /catalog/{catalogID} = Returns catalog metadata (default: catalog-01) |
| 23 | +# /dataset/{datasetID} = Returns dataset metadata (default: breedb) |
| 24 | +# /distribution/{distributionID} = Returns distribution metadata |
| 25 | +# (default: breedb-sparql) |
25 | 26 | # |
26 | 27 | # This services makes use of: |
27 | | -# Data Catalog Vocabulary (DCAT, http://www.w3.org/TR/vocab-dcat/) |
28 | | -# Dublin Core Metadata Terms (DCMI, http://dublincore.org/documents/dcmi-terms/) |
29 | | -# DBpedia (DBPEDIA, http://dbpedia.org/resource/) |
| 28 | +# Data Catalog Vocabulary, http://www.w3.org/TR/vocab-dcat/ |
| 29 | +# Dublin Core Metadata Terms, http://dublincore.org/documents/dcmi-terms/ |
| 30 | +# DBpedia, http://dbpedia.org/resource/) |
30 | 31 | # |
31 | 32 |
|
32 | | -__author__ = 'Arnold Kuzniar' |
| 33 | +__author__ = 'Arnold Kuzniar' |
33 | 34 | __version__ = '0.5.0' |
34 | | -__status__ = 'beta' |
| 35 | +__status__ = 'beta' |
35 | 36 | __license__ = 'Apache License, Version 2.0' |
36 | 37 |
|
37 | 38 |
|
38 | | -from bottle import get, run, static_file, redirect, response, request, opt, install |
| 39 | +from bottle import get, run, static_file, redirect, response, request, opt, \ |
| 40 | + install |
39 | 41 | from metadata import FAIRConfigReader, FAIRGraph, FDPath |
40 | 42 | from datetime import datetime |
41 | 43 | from functools import wraps |
42 | 44 | from logging import getLogger, FileHandler, INFO |
43 | 45 |
|
44 | 46 |
|
45 | | -doc_dir = 'doc' # Swagger UI files |
46 | | - |
47 | | -# log HTTP requests into file in Common Log Format |
48 | | -log_file = 'access.log' |
| 47 | +# log HTTP requests |
49 | 48 | logger = getLogger(__name__) |
50 | 49 | logger.setLevel(INFO) |
51 | | -fh = FileHandler(log_file) |
| 50 | +fh = FileHandler('access.log') |
52 | 51 | fh.setLevel(INFO) |
53 | 52 | logger.addHandler(fh) |
54 | 53 |
|
55 | 54 | def logHttpRequests(fn): |
| 55 | + """Log HTTP requests into log file using Common Log Format""" |
56 | 56 | @wraps(fn) |
57 | 57 | def _log_to_logger(*args, **kwargs): |
58 | 58 | request_time = datetime.now().strftime("%d/%b/%Y %H:%M:%S") |
59 | | - logger.info('%s - - [%s] "%s %s %s" %d' % (request.remote_addr, |
60 | | - request_time, |
61 | | - request.method, |
62 | | - request.urlparts.path, |
63 | | - request.get('SERVER_PROTOCOL'), |
64 | | - response.status_code)) |
| 59 | + logger.info('%s - - [%s] "%s %s %s" %d' % ( |
| 60 | + request.remote_addr, |
| 61 | + request_time, |
| 62 | + request.method, |
| 63 | + request.urlparts.path, |
| 64 | + request.get('SERVER_PROTOCOL'), |
| 65 | + response.status_code)) |
65 | 66 | return fn(*args, **kwargs) |
66 | 67 | return _log_to_logger |
67 | 68 |
|
68 | 69 | install(logHttpRequests) |
69 | 70 |
|
70 | | -# populate FAIR metadata from default config file |
| 71 | +# populate FAIR metadata from config file |
71 | 72 | reader = FAIRConfigReader() |
72 | 73 | scheme = 'http' |
73 | 74 | host = opt.bind # pass host:[port] through the command-line -b option |
74 | | -base_uri = '%s://%s' % (scheme, host) |
| 75 | +base_uri = '{}://{}'.format(scheme, host) |
75 | 76 | g = FAIRGraph(base_uri) |
76 | 77 |
|
77 | 78 | for triple in reader.getTriples(): |
78 | | - g.setMetadata(triple) |
| 79 | + g.setMetadata(triple) |
79 | 80 |
|
80 | 81 |
|
81 | | -# HTTP response: FAIR metadata in RDF and JSON-LD formats |
82 | 82 | def httpResponse(graph, uri): |
83 | | - accept_header = request.headers.get('Accept') |
84 | | - fmt = 'turtle' # default RDF serialization |
85 | | - mime_types = { |
86 | | - 'text/turtle' : 'turtle', |
87 | | - 'application/rdf+xml' : 'xml', |
88 | | - 'application/ld+json' : 'json-ld', |
89 | | - 'application/n-triples' : 'nt' |
90 | | - } |
91 | | - |
92 | | - if accept_header in mime_types: |
| 83 | + """HTTP response: FAIR metadata in RDF and JSON-LD formats""" |
| 84 | + accept_header = request.headers.get('Accept') |
| 85 | + fmt = 'turtle' # default RDF serialization |
| 86 | + mime_types = { |
| 87 | + 'text/turtle' : 'turtle', |
| 88 | + 'application/rdf+xml' : 'xml', |
| 89 | + 'application/ld+json' : 'json-ld', |
| 90 | + 'application/n-triples' : 'nt' |
| 91 | + } |
| 92 | + |
| 93 | + if accept_header in mime_types: |
93 | 94 | fmt = mime_types[accept_header] |
94 | 95 |
|
95 | | - serialized_graph = graph.serialize(uri, fmt) |
| 96 | + serialized_graph = graph.serialize(uri, fmt) |
96 | 97 |
|
97 | | - if serialized_graph is None: |
98 | | - response.status = 404 # web resource not found |
99 | | - return |
| 98 | + if serialized_graph is None: |
| 99 | + response.status = 404 # web resource not found |
| 100 | + return |
100 | 101 |
|
101 | | - response.content_type = 'text/plain' |
102 | | - response.set_header('Allow', 'GET') |
| 102 | + response.content_type = 'text/plain' |
| 103 | + response.set_header('Allow', 'GET') |
103 | 104 |
|
104 | | - return serialized_graph |
| 105 | + return serialized_graph |
105 | 106 |
|
106 | 107 |
|
107 | 108 | # HTTP request handlers |
108 | 109 | @get(['/', '/doc', '/doc/']) |
109 | 110 | def defaultPage(): |
110 | | - redirect('/doc/index.html') |
| 111 | + redirect('/doc/index.html') |
111 | 112 |
|
112 | 113 | @get(FDPath('doc', '<fname:path>')) |
113 | 114 | def sourceDocFiles(fname): |
114 | | - return static_file(fname, root=doc_dir) |
| 115 | + return static_file(fname, root='doc') |
115 | 116 |
|
116 | 117 | @get(FDPath('fdp')) |
117 | 118 | def getFdpMetadata(graph=g): |
118 | | - return httpResponse(graph, graph.fdpURI()) |
| 119 | + return httpResponse(graph, graph.fdpURI()) |
119 | 120 |
|
120 | 121 | @get(FDPath('cat', '<catalog_id>')) |
121 | 122 | def getCatalogMetadata(catalog_id, graph=g): |
122 | | - return httpResponse(graph, graph.catURI(catalog_id)) |
| 123 | + return httpResponse(graph, graph.catURI(catalog_id)) |
123 | 124 |
|
124 | 125 | @get(FDPath('dat', '<dataset_id>')) |
125 | 126 | def getDatasetMetadata(dataset_id, graph=g): |
126 | | - return httpResponse(graph, graph.datURI(dataset_id)) |
| 127 | + return httpResponse(graph, graph.datURI(dataset_id)) |
127 | 128 |
|
128 | 129 | @get(FDPath('dist', '<distribution_id>')) |
129 | 130 | def getDistributionMetadata(distribution_id, graph=g): |
130 | | - return httpResponse(graph, graph.distURI(distribution_id)) |
| 131 | + return httpResponse(graph, graph.distURI(distribution_id)) |
131 | 132 |
|
132 | 133 |
|
133 | 134 | if __name__ == '__main__': |
134 | | - run(server='wsgiref') |
135 | | - |
| 135 | + run(server='wsgiref') |
0 commit comments