Add content negotiation when fetching RDF metadata.
HEAD mainSigned-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
1 files changed, 32 insertions, 21 deletions
diff --git a/web/webserver.scm b/web/webserver.scm
index e2412d2..6a0bd37 100644
--- a/web/webserver.scm
+++ b/web/webserver.scm
@@ -202,6 +202,24 @@ otherwise search for set/group data"
`(("error" . ,key)
("msg" . ,msg)))))))
+(define (render-sparql request prefix val)
+ (let* ((mime (negotiate-mime request))
+ (resp-mime (if (or (string-contains (symbol->string mime) "html")
+ (string-contains (symbol->string mime) "microdata"))
+ 'text/html
+ mime)))
+ (receive (sparql-header sparql-resp)
+ (sparql-http-get
+ (or (getenv "SPARQL-ENDPOINT") "http://localhost:8890/sparql/")
+ (sparql-by-term prefix val)
+ (symbol->string mime))
+ (list `((content-type ,resp-mime))
+ (lambda (port)
+ (let ((resp (if (string? sparql-resp)
+ sparql-resp
+ (utf8->string sparql-resp))))
+ (put-string port resp)))))))
+
(define (invalid-data? data target)
(if (string? (assoc-ref data target))
(if (string-null? (assoc-ref data target))
@@ -244,6 +262,14 @@ otherwise search for set/group data"
`(("error" . ,key)
("msg" . ,msg)))))))
+(define (negotiate-mime request)
+ (let* ((headers (request-headers request))
+ (accept (caar (assoc-ref headers 'accept))))
+ (if (or (eq? (string->symbol "*/*") accept)
+ (eq? (string->symbol "text/html") accept))
+ 'application/x-nice-microdata
+ accept)))
+
(define (controller request body)
(match-lambda
(('GET)
@@ -329,29 +355,14 @@ otherwise search for set/group data"
(rest (render-json "NOP")))))
;; RDF End-points
(('GET "v1" "id" id)
- (receive (sparql-header sparql-resp)
- (sparql-http-get
- (or (getenv "SPARQL-ENDPOINT") "http://localhost:8890/sparql/")
- (sparql-by-term 'gn id))
- (list '((content-type text/html))
- (lambda (port)
- (put-string port sparql-resp)))))
+ (render-sparql request 'gn id))
+
(('GET "v1" "category" category)
- (receive (sparql-header sparql-resp)
- (sparql-http-get
- (or (getenv "SPARQL-ENDPOINT") "http://localhost:8890/sparql/")
- (sparql-by-term 'gnc category))
- (list '((content-type text/html))
- (lambda (port)
- (put-string port sparql-resp)))))
+ (render-sparql request 'gnc category))
+
(('GET "v1" "term" term)
- (receive (sparql-header sparql-resp)
- (sparql-http-get
- (or (getenv "SPARQL-ENDPOINT") "http://localhost:8890/sparql/")
- (sparql-by-term 'gnt term))
- (list '((content-type text/html))
- (lambda (port)
- (put-string port sparql-resp)))))
+ (render-sparql request 'gnt term))
+
(_ (not-found (request-uri request)))))
(define (request-path-components request)
|