From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: swedebugia Newsgroups: gmane.lisp.guile.user Subject: Re: Trouble parsing a response (Was: Re: New library: guile-wikidata) Date: Wed, 26 Dec 2018 20:49:47 +0100 Message-ID: <3ca9b608-8693-0663-9869-e671ee60201c@riseup.net> References: <5ffe968620f1d2d940b7db2b1900dc43@riseup.net> <9f876db761468c16d1a6e0ee27c30821@riseup.net> <20181209110814.GA20268@tuxteam.de> <87tvjms87o.fsf@web.de> <33b7a105-8529-1043-c5bd-5b7fccefc042@gnu.org> <413312f3b999443b86b604ea9beff618@riseup.net> <7ebe1814-8aff-2d09-dc54-f978b4d16fe0@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------857424BF914F816DD1815C09" X-Trace: blaine.gmane.org 1545853300 5332 195.159.176.226 (26 Dec 2018 19:41:40 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 26 Dec 2018 19:41:40 +0000 (UTC) Cc: guile-user , guile-user@gnu.org To: Roel Janssen Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Wed Dec 26 20:41:35 2018 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gcF3e-0001Id-KX for guile-user@m.gmane.org; Wed, 26 Dec 2018 20:41:35 +0100 Original-Received: from localhost ([127.0.0.1]:47972 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gcF5l-0003mK-Ck for guile-user@m.gmane.org; Wed, 26 Dec 2018 14:43:45 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:47019) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gcF5N-0003mE-VU for guile-user@gnu.org; Wed, 26 Dec 2018 14:43:23 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gcF5M-0003CR-JZ for guile-user@gnu.org; Wed, 26 Dec 2018 14:43:21 -0500 Original-Received: from mx1.riseup.net ([198.252.153.129]:49788) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gcF5H-00038p-N9; Wed, 26 Dec 2018 14:43:15 -0500 Original-Received: from piha.riseup.net (piha-pn.riseup.net [10.0.1.163]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (Client CN "*.riseup.net", Issuer "COMODO RSA Domain Validation Secure Server CA" (verified OK)) by mx1.riseup.net (Postfix) with ESMTPS id BDA571A01D4; Wed, 26 Dec 2018 11:43:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=riseup.net; s=squak; t=1545853394; bh=4uB8IBKyTtFzl2MSf5xnARXxfT6Q/xrZx3xC6dGLPsE=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=X7nONQY1uUGm2n3XCM9M4kgcLmBUwjpRxiL127KsR1MR81z/fYKBZFnDPg0gaQu1h czw1SqnAidnts4A6yugL58MlvUfwHB/n6mkPnuUWpJXkFSrhb6pl/gpvFugwLCKDXS tMk+P0ZHJhPKwyF+CbtgYtCwQVqytWfUWOq68vjo= X-Riseup-User-ID: FF40A5E994A949A8AA9C169BBA3B24DB9EFD50E3CA98FEB58E88746CE3B963B3 Original-Received: from [127.0.0.1] (localhost [127.0.0.1]) by piha.riseup.net with ESMTPSA id D76823002A; Wed, 26 Dec 2018 11:43:10 -0800 (PST) In-Reply-To: <7ebe1814-8aff-2d09-dc54-f978b4d16fe0@gnu.org> Content-Language: en-US X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 198.252.153.129 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:15149 Archived-At: This is a multi-part message in MIME format. --------------857424BF914F816DD1815C09 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable On 2018-12-13 23:03, Roel Janssen wrote: >=20 >=20 > On 13-12-18 17:06, swedebugia@riseup.net wrote: >> On 2018-12-13 16:01, swedebugia@riseup.net wrote: >> snip >> >>> >>> I tried with the file attached but got this because the driver does n= ot >>> support URIs but only host, port, type, token: >> >> Ah, I saw now that you already implemented URI on master :) >> https://github.com/roelj/guile-sparql/blob/master/sparql/driver.scm >> >> When I try calling this >> ;; Example query to wikidata listing cats >> (sparql-query >> =C2=A0 "SELECT ?item >> WHERE >> { >> ?item wdt:P31 wd:Q146. >> } >> LIMIT 10 >> " >> =C2=A0 #:uri "https://query.wikidata.org/sparql" >> =C2=A0 ;; #:port 80 >> =C2=A0 #:type "application/sparql-results+json" >> =C2=A0 ;;=C2=A0 #:token "..." >> =C2=A0 #:store-backend 'blazegraph >> =C2=A0 ) >> >> I get this fine result: >> #< version: (1 . 1) code: 200 reason-phrase: "OK" headers: >> ((date . #> month: 12 year: 2018 zone-offset: 0>) (content-type >> application/sparql-results+json (charset . "utf-8")) (transfer-encodin= g >> (chunked)) (connection close) (server . "nginx/1.13.6") (x-served-by . >> "wdqs1005") (access-control-allow-origin . "*") (cache-control public >> (max-age . 300)) (vary accept accept-encoding) (x-varnish . "644531744= , >> 572094009, 417977651") (via "1.1 varnish (Varnish/5.1)" "1.1 varnish >> (Varnish/5.1)" "1.1 varnish (Varnish/5.1)") (accept-ranges bytes) (age= . >> 0) (x-cache . "cp1079 pass, cp3030 pass, cp3030 pass") (x-cache-status= . >> "pass") (server-timing . "cache;desc=3D\"pass\"") >> (strict-transport-security . "max-age=3D106384710; includeSubDomains; >> preload") (set-cookie . >> "WMF-Last-Access=3D13-Dec-2018;Path=3D/;HttpOnly;secure;Expires=3DMon,= 14 Jan >> 2019 12:00:00 GMT") (set-cookie . >> "WMF-Last-Access-Global=3D13-Dec-2018;Path=3D/;Domain=3D.wikidata.org;= HttpOnly;secure;Expires=3DMon,=20 >> >> 14 Jan 2019 12:00:00 GMT") (x-analytics . "https=3D1;nocookies=3D1") >> (x-client-ip . "83.185.90.53")) port: #> >> >> My problem now is that I don't know how to separate the header from th= e >> port-file. >> >> Ah, reading here >> https://www.gnu.org/software/guile/manual/html_node/Responses.html#Res= ponses=20 >> >> I found (response-port). >> >=20 > Here's what I did in SPARQLing-genomics: > https://github.com/UMCUGenetics/sparqling-genomics/blob/master/web/www/= pages/query-response.scm#L86=20 >=20 >=20 > So basically: >=20 > (use-modules > =C2=A0 (ice-9 receive) > =C2=A0 (ice-9 rdelim) > =C2=A0 (web response)) >=20 > (receive (header port) > =C2=A0 ;; Note: "text/csv" is the only format that is consistent for=20 > multiple SPARQL back-ends (Virtuoso, BlazeGraph, ...) > =C2=A0 (sparql-query ... #:type "text/csv") > =C2=A0 (if (=3D (response-code header) 200) ; This means the query wen= t OK. > =C2=A0=C2=A0=C2=A0 (call-some-function port) > =C2=A0=C2=A0=C2=A0 #f)) ; Deal with errors at the #f. >=20 > (define (call-some-function port) > =C2=A0 (let ((line (read-line port))) > =C2=A0=C2=A0=C2=A0 (if (eof-object? line) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 #t > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (begin > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (format #t "Line: ~a~%" lin= e) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; Tail-recurse until we ha= ve processed each line. > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (call-some-function port)))= )) >=20 > The SPARQLing-genomics code deals with more error codes, and processes=20 > the lines in a more useful way. >=20 >> Unfortunately this only took me one step further as I run into this >> instead when trying to parse the port with (json->scm): >> >> Backtrace: >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 7 (= apply-smob/1 #) >> In ice-9/boot-9.scm: >> =C2=A0=C2=A0=C2=A0=C2=A0 705:2=C2=A0 6 (call-with-prompt _ _ #) >> In ice-9/eval.scm: >> =C2=A0=C2=A0=C2=A0=C2=A0 619:8=C2=A0 5 (_ #(#(#))) >> In ice-9/boot-9.scm: >> =C2=A0=C2=A0=C2=A0 2312:4=C2=A0 4 (save-module-excursion _) >> =C2=A0=C2=A0 3831:12=C2=A0 3 (_) >> In sdb-test.scm: >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 24:1=C2=A0 2 (_) >> In json/parser.scm: >> =C2=A0=C2=A0=C2=A0 311:18=C2=A0 1 (json-read-number _) >> =C2=A0=C2=A0=C2=A0 148:28=C2=A0 0 (read-number _) >> >> json/parser.scm:148:28: In procedure read-number: >> Throw to key `json-invalid' with args `(#> #>)'. >> >> Maybe this is a bug in (json)? >=20 > It looks like the JSON response is not (only) JSON, or simply invalid. > Maybe the "text/xml" or "text/csv" content-type will work better for=20 > you.=C2=A0 I noticed that each back-end provides their own structure fo= r XML=20 > and JSON, so I used the somewhat quirky CSV format as a work-for-all=20 > response type. I tried again today on guile 2.2.4 on parabola with guile installed from=20 guix and got this: sdb@parabola:~/src/guile-sparql$ guile -L . -s test.scm guile: warning: failed to install locale ;;; note: source file /home/egil/src/guile-sparql/test.scm ;;; newer than compiled=20 /home/egil/.cache/guile/ccache/2.2-LE-8-3.A/home/egil/src/guile-sparql/te= st.scm.go ;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=3D0 ;;; or pass the --no-auto-compile argument to disable. ;;; compiling /home/egil/src/guile-sparql/test.scm ;;; compiled=20 /home/egil/.cache/guile/ccache/2.2-LE-8-3.A/home/egil/src/guile-sparql/te= st.scm.go Backtrace: 5 (apply-smob/1 #) In ice-9/boot-9.scm: 705:2 4 (call-with-prompt _ _ #) In ice-9/eval.scm: 619:8 3 (_ #(#(#))) In ice-9/boot-9.scm: 2312:4 2 (save-module-excursion _) 3831:12 1 (_) In test.scm: 8:0 0 (_) test.scm:8:0: Throw to key `vm-error' with args `(vm-run "Wrong number=20 of values returned to continuation (expected ~a)" (2))'. Note: to reproduce you need to run it with guile-sparql from git. Any idea whats wrong? --=20 Cheers Swedebugia --------------857424BF914F816DD1815C09 Content-Type: text/x-scheme; name="test.scm" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="test.scm" ;; Example query to wikidata listing cats (use-modules (sparql driver) (ice-9 receive) (ice-9 rdelim) (web response)) (define q (sparql-query " SELECT ?item WHERE { ?item wdt:P31 wd:Q146. } LIMIT 10 " #:uri "https://query.wikidata.org/sparql" #:type "text/csv" #:store-backend 'blazegraph)) (display q) (receive (header port) ;; Note: "text/csv" is the only format that is consistent for multiple SPARQL back-ends (Virtuoso, BlazeGraph, ...) q (if (= (response-code header) 200) ; This means the query went OK. (call-some-function port) #f)) ; Deal with errors at the #f. (define (call-some-function port) (let ((line (read-line port))) (if (eof-object? line) #t (begin (format #t "Line: ~a~%" line) ;; Tail-recurse until we have processed each line. (call-some-function port))))) --------------857424BF914F816DD1815C09--