unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Roel Janssen <roel@gnu.org>
To: swedebugia@riseup.net
Cc: guile-user <guile-user-bounces+swedebugia=riseup.net@gnu.org>,
	guile-user@gnu.org
Subject: Re: Trouble parsing a response
Date: Fri, 14 Dec 2018 16:59:12 +0100	[thread overview]
Message-ID: <13c596d2-b842-1b5b-06fd-07692b98e9af@gnu.org> (raw)
In-Reply-To: <06322d83d00ad20fd7e536051589c9ed@riseup.net>



On 13-12-18 23:29, swedebugia@riseup.net wrote:
> On 2018-12-13 23:03, Roel Janssen wrote:
>> 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 not
>>>> 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
>>>    "SELECT ?item
>>>                                          WHERE
>>>                                          {
>>>                                          ?item wdt:P31 wd:Q146.
>>>                                          }
>>>                                          LIMIT 10
>>>                                          "
>>>    #:uri "https://query.wikidata.org/sparql"
>>>    ;; #:port 80
>>>                                            #:type "application/sparql-results+json"
>>>    ;;  #:token "..."
>>>                                            #:store-backend 'blazegraph
>>>    )
>>>
>>> I get this fine result:
>>> #<<response> version: (1 . 1) code: 200 reason-phrase: "OK" headers:
>>> ((date . #<date nanosecond: 0 second: 12 minute: 32 hour: 15 day: 13
>>> month: 12 year: 2018 zone-offset: 0>) (content-type
>>> application/sparql-results+json (charset . "utf-8")) (transfer-encoding
>>> (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=\"pass\"")
>>> (strict-transport-security . "max-age=106384710; includeSubDomains;
>>> preload") (set-cookie .
>>> "WMF-Last-Access=13-Dec-2018;Path=/;HttpOnly;secure;Expires=Mon, 14 Jan
>>> 2019 12:00:00 GMT") (set-cookie .
>>> "WMF-Last-Access-Global=13-Dec-2018;Path=/;Domain=.wikidata.org;HttpOnly;secure;Expires=Mon,
>>> 14 Jan 2019 12:00:00 GMT") (x-analytics . "https=1;nocookies=1")
>>> (x-client-ip . "83.185.90.53")) port: #<input-output: file 8c37e70>>
>>>
>>> My problem now is that I don't know how to separate the header from the
>>> port-file.
>>>
>>> Ah, reading here
>>> https://www.gnu.org/software/guile/manual/html_node/Responses.html#Responses
>>> I found (response-port).
>>>
>>
>> Here's what I did in SPARQLing-genomics:
>> https://github.com/UMCUGenetics/sparqling-genomics/blob/master/web/www/pages/query-response.scm#L86
>>
>> So basically:
>>
>> (use-modules
>>    (ice-9 receive)
>>    (ice-9 rdelim)
>>    (web response))
>>
>> (receive (header port)
>>    ;; Note: "text/csv" is the only format that is consistent for
>> multiple SPARQL back-ends (Virtuoso, BlazeGraph, ...)
>>    (sparql-query ... #:type "text/csv")
>>    (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)))))
> 
> This would be a really nice addition to the guile manual. Can I submit
> it as a patch crediting you?

Sure!

>>
>> The SPARQLing-genomics code deals with more error codes, and processes
>> the lines in a more useful way.
>>
>>> Unfortunately this only took me one step further as I run into this
>>> instead when trying to parse the port with (json->scm):
>>>
>>> Backtrace:
>>>              7 (apply-smob/1 #<catch-closure 9769550>)
>>> In ice-9/boot-9.scm:
>>>       705:2  6 (call-with-prompt _ _ #<procedure default-prompt-handle…>)
>>> In ice-9/eval.scm:
>>>       619:8  5 (_ #(#(#<directory (guile-user) 9759910>)))
>>> In ice-9/boot-9.scm:
>>>      2312:4  4 (save-module-excursion _)
>>>     3831:12  3 (_)
>>> In sdb-test.scm:
>>>        24:1  2 (_)
>>> In json/parser.scm:
>>>      311:18  1 (json-read-number _)
>>>      148:28  0 (read-number _)
>>>
>>> json/parser.scm:148:28: In procedure read-number:
>>> Throw to key `json-invalid' with args `(#<json-parser port:
>>> #<input-output: string 98dea80>>)'.
>>>
>>> Maybe this is a bug in (json)?
>>
>> 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
>> you.  I noticed that each back-end provides their own structure for
>> XML and JSON, so I used the somewhat quirky CSV format as a
>> work-for-all response type.
> 
> Ok, good to know. Is this documented in your guile-sparql docs?

No.  I think what SPARQL endpoints actually return is beyond the scope 
of guile-sparql, and may change in the future anyway.

>>
>> I hope this helps.
> 
> Thanks a lot for taking the time to write this. :)
> I already met receive in the npm importer but I did not really learn to
> use it yet. So whenever I see multiple objects returned I can part them
> with receive! Nice.

Exactly.  I'm glad my snippet helped.

Kind regards,
Roel Janssen



  reply	other threads:[~2018-12-14 15:59 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <5ffe968620f1d2d940b7db2b1900dc43@riseup.net>
2018-12-09  9:11 ` New library: guile-wikidata swedebugia
2018-12-09 11:08   ` tomas
2018-12-09 21:26     ` Arne Babenhauserheide
2018-12-11  0:32       ` swedebugia
2018-12-11 10:29         ` Roel Janssen
2018-12-13 15:01           ` swedebugia
2018-12-13 16:06             ` Trouble parsing a response (Was: Re: New library: guile-wikidata) swedebugia
2018-12-13 22:03               ` Roel Janssen
2018-12-13 22:29                 ` Trouble parsing a response swedebugia
2018-12-14 15:59                   ` Roel Janssen [this message]
2018-12-26 19:49                 ` Trouble parsing a response (Was: Re: New library: guile-wikidata) swedebugia
2019-01-03 12:25                 ` swedebugia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=13c596d2-b842-1b5b-06fd-07692b98e9af@gnu.org \
    --to=roel@gnu.org \
    --cc=guile-user-bounces+swedebugia=riseup.net@gnu.org \
    --cc=guile-user@gnu.org \
    --cc=swedebugia@riseup.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).