diff options
author | Vivien Kraus <vivien@planete-kraus.eu> | 2020-12-06 18:57:33 +0100 |
---|---|---|
committer | Vivien Kraus <vivien@planete-kraus.eu> | 2021-05-11 00:30:49 +0200 |
commit | a2c9200077615032f7658617f0b08cc419689db1 (patch) | |
tree | b51ac1ab2d1b81667fcd2a7a68331220ef4429ec /src/scm/webid-oidc/fetch.scm | |
parent | 2984035f4ffb2a5b0c34e2b177d2406a8876e356 (diff) |
Add a function to fetch linked data.
Diffstat (limited to 'src/scm/webid-oidc/fetch.scm')
-rw-r--r-- | src/scm/webid-oidc/fetch.scm | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/scm/webid-oidc/fetch.scm b/src/scm/webid-oidc/fetch.scm new file mode 100644 index 0000000..b7d7135 --- /dev/null +++ b/src/scm/webid-oidc/fetch.scm @@ -0,0 +1,42 @@ +(define-module (webid-oidc fetch) + #:use-module (webid-oidc errors) + #:use-module (ice-9 optargs) + #:use-module (ice-9 receive) + #:use-module (rnrs bytevectors) + #:use-module (web client) + #:use-module (web request) + #:use-module (web response) + #:use-module (web uri) + #:use-module (turtle tordf)) + +(define*-public (fetch uri #:key (http-get http-get)) + (with-exception-handler + (lambda (error) + (raise-cannot-fetch-linked-data uri error)) + (lambda () + (receive (response response-body) + (http-get uri + #:headers `((accept (text/turtle)))) + (with-exception-handler + (lambda (error) + (raise-unexpected-response response error)) + (lambda () + (unless (eqv? (response-code response) 200) + (raise-request-failed-unexpectedly (response-code response) + (response-reason-phrase response))) + (let ((content-type (response-content-type response))) + (unless (and content-type + (eq? (car content-type) 'text/turtle) + (or (not (assq-ref (cdr content-type) 'charset)) + (equal? (assq-ref (cdr content-type) 'charset) "utf-8"))) + (raise-unexpected-header-value 'content-type content-type))) + (when (bytevector? response-body) + (set! response-body (utf8->string response-body))) + (with-exception-handler + (lambda (rdf-error) + (raise-not-turtle response-body rdf-error)) + (lambda () + (turtle->rdf (string-append + "# This is not a file name\n" + response-body) + (uri->string uri)))))))))) |