;;; (https-get) ;; Copyright (C) 2013, 2014 Aleix Conchillo Flaque ;; Copyright (C) 2016 Amirouche Boubekki ;; ;; This file is based on guile-oauth's (oauth oauth1 utils). ;; ;; https-get is free software; you can redistribute it and/or ;; modify it under the terms of the GNU Lesser General Public ;; License as published by the Free Software Foundation; either ;; version 3 of the License, or (at your option) any later version. ;; ;; https-get is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; Lesser General Public License for more details. ;; ;; You should have received a copy of the GNU Lesser General Public ;; License along with guile-oauth; if not, write to the Free Software ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA ;; 02110-1301 USA ;;; Commentary: ;; https-get for Guile ;;; Code: (define-module (https-get) #:use-module (gnutls) #:use-module (ice-9 binary-ports) #:use-module (ice-9 receive) #:use-module (ice-9 match) #:use-module (rnrs bytevectors) #:use-module (srfi srfi-19) #:use-module (web client) #:use-module (web uri) #:export (https-get http-get*)) ;; ;; HTTP/HTTPS methods ;; (define (https-call https-proc) (lambda* (uri #:key (headers '()) (streaming? #f)) (let* ((socket (open-socket-for-uri uri)) (session (make-session connection-end/client))) ;; (set-log-level! 9) ;; (set-log-procedure! ;; (lambda (level msg) (format #t "|<~d>| ~a" level msg))) ;; Use the file descriptor that underlies SOCKET. (set-session-transport-fd! session (fileno socket)) ;; Use the default settings. (set-session-priorities! session "NORMAL") ;; Create anonymous credentials. (set-session-credentials! session (make-anonymous-client-credentials)) (set-session-credentials! session (make-certificate-credentials)) ;; Perform the TLS handshake with the server. (handshake session) (receive (response body) (https-proc uri #:port (session-record-port session) #:keep-alive? #t #:headers headers #:streaming? streaming?) (bye session close-request/rdwr) (values response body))))) (define https-get (https-call http-get)) (use-modules (web response)) (use-modules (ice-9 receive)) (use-modules (ice-9 rdelim)) (use-modules (ice-9 iconv)) (receive (response body) (https-get "https://hacker-news.firebaseio.com/v0/maxitem.json") (pk (string->number (bytevector->string body "utf-8")))) (receive (response body) (https-get "https://hacker-news.firebaseio.com/v0/maxitem.json" #:streaming? #true) (pk (read-string body)))