This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsparql-http-client.scm
63 lines (58 loc) · 2.76 KB
/
sparql-http-client.scm
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
;;; -*- Mode: Scheme; scheme48-package: sparql-http-client -*-
;;;; Schemantic Web
;;;; SPARQL Client
;;; This code is written by Taylor R. Campbell and placed in the Public
;;; Domain. All warranties are disclaimed.
(define (http-get-sparql service-uri request)
(if (not (eq? 'HTTP (uri-scheme service-uri)))
(error "Non-HTTP service URI for HTTP SPARQL query:" service-uri))
(if (uri-query service-uri)
(error "Service URI must not have a query for HTTP SPARQL query:"
service-uri))
(call-with-http-response 'GET
(make-uri 'HTTP
(uri-authority service-uri)
(uri-path service-uri)
(string-append "query=" (sparql->condensed-string request))
(uri-fragment service-uri))
'() ;No header fields
#f ;No entity body
(lambda (http-response entity-input-port)
(process-sparql-response service-uri http-response entity-input-port))))
(define (http-post-sparql service-uri query)
(if (not (eq? 'HTTP (uri-scheme service-uri)))
(error "Non-HTTP service URI for HTTP SPARQL query:" service-uri))
(if (uri-query service-uri)
(error "Service URI must not have a query for HTTP SPARQL query:"
service-uri))
(call-with-http-response 'POST
service-uri
(list (make-http-header-field 'CONTENT-TYPE
"application/x-www-form-urlencoded"))
(list->string ;++ Lame-o, poor-man's form/URI-
(cdr ;++ encoding. We strip off the leading
(string->list ;++ question mark with the CDR, and
(uri->string ;++ forget about escaping amperands.
(make-uri #f ;++ URI->STRING deals with the percent
#f ;++ encoding.
'()
(string-append "query="
(sparql->condensed-string query))
#f)))))
(lambda (http-response entity-input-port)
(process-sparql-response service-uri http-response entity-input-port))))
(define (process-sparql-response service-uri http-response entity-input-port)
(if (not (eq? 'SUCCESS (http-response/status-type http-response)))
(error "Failure!"
http-response
service-uri
(read-http-entity-body http-response entity-input-port))
(let ((variables #f)
(binding-sets '()))
(parse-sparql-results
entity-input-port
(lambda (vs)
(set! variables vs))
(lambda (binding-set)
(set! binding-sets (cons binding-set binding-sets))))
(values variables (reverse binding-sets)))))